Skip to content

Commit

Permalink
fix: add user mention and ID when deleting message
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Nov 15, 2023
1 parent 4f2c783 commit 2911923
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/commands/context-menu/deleteMsg.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ApplicationCommandType, CacheType, ContextMenuCommandInteraction, RESTPostAPIApplicationCommandsJSONBody } from 'discord.js';
import {
ApplicationCommandType,
CacheType,
ContextMenuCommandInteraction,
RESTPostAPIApplicationCommandsJSONBody,
} from 'discord.js';
import BaseCommand from '../BaseCommand.js';
import db from '../../utils/Db.js';
import { checkIfStaff } from '../../utils/Utils.js';
import { emojis } from '../../utils/Constants.js';
import db from '../../utils/Db.js';

export default class DeleteMessage extends BaseCommand {
readonly data: RESTPostAPIApplicationCommandsJSONBody = {
Expand All @@ -19,16 +24,21 @@ export default class DeleteMessage extends BaseCommand {
include: { hub: true },
});

if (!messageInDb) return await interaction.editReply('Unknown Message. If it has been sent in the past minute, please wait few more seconds and try again.');
if (!messageInDb) {
return await interaction.editReply(
'Unknown Message. If it has been sent in the past minute, please wait few more seconds and try again.',
);
}

const interchatStaff = checkIfStaff(interaction.user.id);
if (
!interchatStaff &&
!messageInDb.hub?.moderators.find((m) => m.userId === interaction.user.id) &&
messageInDb.hub?.ownerId !== interaction.user.id &&
interaction.user.id !== messageInDb.authorId
) return await interaction.editReply(`${emojis.no} You are not the author of this message.`);

messageInDb.hub?.ownerId !== interaction.user.id &&
interaction.user.id !== messageInDb.authorId
) {
return await interaction.editReply(`${emojis.no} You are not the author of this message.`);
}

// find all the messages through the network
const channelSettingsArr = await db.connectedList.findMany({
Expand All @@ -39,22 +49,28 @@ export default class DeleteMessage extends BaseCommand {
if (!connection) return false;

const webhookURL = connection.webhookURL.split('/');
const webhook = await interaction.client.fetchWebhook(webhookURL[webhookURL.length - 2])?.catch(() => null);
const webhook = await interaction.client
.fetchWebhook(webhookURL[webhookURL.length - 2])
?.catch(() => null);

if (webhook?.owner?.id !== interaction.client.user?.id) return false;

// finally, delete the message
return await webhook?.deleteMessage(element.messageId, connection.parentId ? connection.channelId : undefined)
return await webhook
?.deleteMessage(element.messageId, connection.parentId ? connection.channelId : undefined)
.then(() => true)
.catch(() => false);
});

const resultsArray = await Promise.all(results);
const deleted = resultsArray.reduce((acc, cur) => acc + (cur ? 1 : 0), 0);
await interaction.editReply(`${emojis.yes} Your message has been deleted from __**${deleted}/${resultsArray.length}**__ servers.`).catch(() => null);
await interaction
.editReply(
`${emojis.yes} Message by **<@${messageInDb.authorId}> (${messageInDb.authorId})** has been deleted from __**${deleted}/${resultsArray.length}**__ servers.`,
)
.catch(() => null);

// log the deleted message for moderation purposes TODO
// if (interaction.inCachedGuild()) networkMessageDelete(interaction.member, interaction.targetMessage);
}

}
}

0 comments on commit 2911923

Please sign in to comment.