Skip to content

Commit

Permalink
feat: allow manage messages perm to delete
Browse files Browse the repository at this point in the history
  • Loading branch information
everettsouthwick committed Aug 26, 2023
1 parent 3e9b34e commit c0f2e42
Showing 1 changed file with 22 additions and 44 deletions.
66 changes: 22 additions & 44 deletions src/utils/handleLinkResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,36 @@ async function sendReplaceModeMessage(message, fullMessage, embeds) {

async function sendReplyModeMessage(messageOrInteraction, links, embeds) {
const isInteraction = messageOrInteraction.commandId ? true : false;
const userId = isInteraction ? messageOrInteraction.user.id : messageOrInteraction.author.id;

const confirm = new ButtonBuilder()
.setCustomId('keep')
.setLabel('Keep')
.setStyle(ButtonStyle.Primary);
const confirm = new ButtonBuilder().setCustomId('keep').setLabel('Keep').setStyle(ButtonStyle.Primary);
const cancel = new ButtonBuilder().setCustomId('delete').setLabel('Delete').setStyle(ButtonStyle.Danger);
const row = new ActionRowBuilder().addComponents(confirm, cancel);

const cancel = new ButtonBuilder()
.setCustomId('delete')
.setLabel('Delete')
.setStyle(ButtonStyle.Danger);
const options = embeds.length > 0 ? { embeds, components: [row] } : { content: links.join('\n') };
const response = await messageOrInteraction.reply(options);

const row = new ActionRowBuilder()
.addComponents(confirm, cancel);
if (!isInteraction && !(await checkForEmbed(response))) return await response.delete();

let response;
if (embeds.length > 0) {
response = await messageOrInteraction.reply({ embeds: embeds, components: [row] });
}
else {
response = await messageOrInteraction.reply({ content: links.join('\n') });
}
if (!isInteraction) await response.edit({ components: [row] });

if (!isInteraction) {
try {
await checkForEmbed(response);
await response.edit({ components: [row] });
}
catch {
await response.delete();
}
}

const collector = await response.createMessageComponentCollector({ componentType: ComponentType.Button, time: 60000 });
const collector = await response.createMessageComponentCollector({ componentType: ComponentType.Button, time: 180000 });

collector.on('collect', async (i) => {
if (i.customId === 'keep') {
if (!isInteraction) {
await messageOrInteraction.suppressEmbeds(true);
}
if (i.customId !== 'keep' && i.customId !== 'delete') return;

if (i.customId === 'keep') {
if (!isInteraction) await messageOrInteraction.suppressEmbeds(true);
await collector.stop();
await response.edit({ components: [] });
return await response.edit({ components: [] });
}
else if (i.customId === 'delete') {
const userId = isInteraction ? messageOrInteraction.user.id : messageOrInteraction.author.id;
if (i.user.id === userId) {
await collector.stop();
await response.delete();
}
else {
await i.reply({ content: 'Only the original author can choose to delete this message.', ephemeral: true });
}

if (i.user.id !== userId && !i.member.permissions.has('MANAGE_MESSAGES')) {
return await i.reply({ content: 'You are not authorized to delete this message.', ephemeral: true });
}

await collector.stop();
await response.delete();
});

collector.on('end', async () => {
Expand All @@ -79,6 +56,7 @@ async function sendReplyModeMessage(messageOrInteraction, links, embeds) {
});
}


async function sendAskModeMessage(message, links, embeds) {
const confirm = new ButtonBuilder()
.setCustomId('yes')
Expand All @@ -95,7 +73,7 @@ async function sendAskModeMessage(message, links, embeds) {

const response = await message.reply({ content: 'Would you like to embed this link?', components: [row] });

const collector = response.createMessageComponentCollector({ componentType: ComponentType.Button, time: 60000 });
const collector = response.createMessageComponentCollector({ componentType: ComponentType.Button, time: 180000 });

collector.on('collect', async (i) => {
if (i.customId === 'yes') {
Expand Down Expand Up @@ -131,7 +109,7 @@ async function checkForEmbed(message) {
clearInterval(embedCheck);
resolve(true);
}
else if (Date.now() - startTime > 5000) {
else if (Date.now() - startTime > 10000) {
clearInterval(embedCheck);
reject(false);
}
Expand Down

0 comments on commit c0f2e42

Please sign in to comment.