Skip to content

Commit

Permalink
fix(deletemsg): fix interaction failure
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Jan 13, 2023
1 parent 44b2fa8 commit 567efa7
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/Commands/Network/deleteMsg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ContextMenuCommandBuilder, ApplicationCommandType, MessageContextMenuCommandInteraction, TextChannel } from 'discord.js';
import { getDb, checkIfStaff } from '../../Utils/functions/utils';
import { stripIndents } from 'common-tags';
import logger from '../../Utils/logger';
import { networkMessageDelete } from '../../Scripts/networkLogs/networkMsgDelete';

Expand All @@ -9,39 +8,42 @@ export default {
.setName('Delete Message')
.setType(ApplicationCommandType.Message),
async execute(interaction: MessageContextMenuCommandInteraction) {
const db = getDb();
const target = interaction.targetMessage;
const staffUser = await checkIfStaff(interaction.user);

const db = getDb();
const messageInDb = await db?.messageData.findFirst({
where: { channelAndMessageIds: { some: { messageId: { equals: target.id } } } },
});
const emojis = interaction.client.emoji.normal;

if (!messageInDb || (messageInDb?.expired && staffUser === false)) {
return interaction.reply({ content: 'This message has expired.', ephemeral: true });
}

if (!messageInDb || (messageInDb?.expired && staffUser === false)) {return interaction.reply({ content: 'This message has expired.', ephemeral: true });}
// if the user tries to edit someone else' message and they arent staff
if (!staffUser && interaction.user.id !== messageInDb.authorId) {
return interaction.reply({ content: `${emojis.no} You are not the author of this message.`, ephemeral: true });
}

if (staffUser || interaction.user.id === messageInDb?.authorId) {
messageInDb?.channelAndMessageIds.forEach((element) => {
if (!element) return;
await interaction.reply({ content: `${emojis.yes} Deletion in progess! This may take a couple of seconds.`, ephemeral: true }).catch(() => null);

interaction.client.channels
.fetch(element.channelId)
.then((channel) => {
(channel as TextChannel).messages
.fetch(element.messageId)
.then((message) => message.delete())
.catch((e) => logger.error('Delete Message:', e));
})
.catch(logger.error);
});
messageInDb.channelAndMessageIds.forEach((element) => {
if (!element) return;

interaction.inCachedGuild() ? networkMessageDelete(interaction.member, target) : null;
interaction.reply({ content: 'Message deleted.', ephemeral: true });
}
else {
interaction.reply({
content: stripIndents`${interaction.client.emoji.normal.no} Unable to delete message.\nCommon Reasons: Message not sent by you, Message not sent in network.`,
ephemeral: true,
});
}
interaction.client.channels
.fetch(element.channelId)
.then((channel) => {
(channel as TextChannel).messages
.fetch(element.messageId)
.then((message) => message.delete())
.catch((e) => logger.error('Delete Message:', e));
})
.catch(logger.error);
});

// log the deleted message for moderation purposes
interaction.inCachedGuild()
? networkMessageDelete(interaction.member, target)
: null;
},
};

0 comments on commit 567efa7

Please sign in to comment.