From 1e673a39691188898faeca8a7f07883e9816d305 Mon Sep 17 00:00:00 2001 From: WatDuhHekBro Date: Tue, 18 May 2021 14:13:41 -0500 Subject: [PATCH] Added DM channel purge --- src/commands/system/admin.ts | 24 ------------------- src/commands/utility/purge.ts | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 src/commands/utility/purge.ts diff --git a/src/commands/system/admin.ts b/src/commands/system/admin.ts index 7345ec1..399c5a1 100644 --- a/src/commands/system/admin.ts +++ b/src/commands/system/admin.ts @@ -284,30 +284,6 @@ export default new NamedCommand({ } }) }), - purge: new NamedCommand({ - description: "Purges the bot's own messages.", - permission: PERMISSIONS.BOT_SUPPORT, - channelType: CHANNEL_TYPE.GUILD, - async run({send, message, channel, guild, client}) { - // It's probably better to go through the bot's own messages instead of calling bulkDelete which requires MANAGE_MESSAGES. - if (guild!.me?.hasPermission(Permissions.FLAGS.MANAGE_MESSAGES)) { - message.delete(); - const msgs = await channel.messages.fetch({ - limit: 100 - }); - const travMessages = msgs.filter((m) => m.author.id === client.user?.id); - - await send(`Found ${travMessages.size} messages to delete.`).then((m) => - m.delete({ - timeout: 5000 - }) - ); - await (channel as TextChannel).bulkDelete(travMessages); - } else { - send("This command must be executed in a guild where I have the `MANAGE_MESSAGES` permission."); - } - } - }), clear: new NamedCommand({ description: "Clears a given amount of messages.", usage: "", diff --git a/src/commands/utility/purge.ts b/src/commands/utility/purge.ts new file mode 100644 index 0000000..5c55551 --- /dev/null +++ b/src/commands/utility/purge.ts @@ -0,0 +1,45 @@ +import {NamedCommand, getPermissionLevel, getPermissionName, hasPermission} from "onion-lasers"; +import {DMChannel, Permissions} from "discord.js"; + +export default new NamedCommand({ + description: + "Purges the bot's messages in either a guild channel (requiring the BOT_SUPPORT permission level) or a DM channel (no permission required). Limited to the last 100 messages.", + async run({send, message, channel, guild, client, author, member}) { + if (channel instanceof DMChannel) { + const messages = await channel.messages.fetch({ + limit: 100 + }); + + for (const message of messages.values()) { + if (message.author.id === client.user!.id) { + message.delete(); + } + } + } else if (hasPermission(author, member, PERMISSIONS.BOT_SUPPORT)) { + if (guild!.me?.hasPermission(Permissions.FLAGS.MANAGE_MESSAGES)) message.delete(); + + const messages = await channel.messages.fetch({ + limit: 100 + }); + const travMessages = messages.filter((msg) => msg.author.id === client.user!.id); + + send(`Found ${travMessages.size} messages to delete.`).then((msg) => msg.delete({timeout: 5000})); + + // It's better to go through the bot's own messages instead of calling bulkDelete which requires MANAGE_MESSAGES. + for (const message of messages.values()) { + if (message.author.id === client.user!.id) { + message.delete(); + } + } + } else { + const userPermLevel = getPermissionLevel(author, member); + send( + `You don't have access to this command! Your permission level is \`${getPermissionName( + userPermLevel + )}\` (${userPermLevel}), but this command requires a permission level of \`${getPermissionName( + PERMISSIONS.BOT_SUPPORT + )}\` (${PERMISSIONS.BOT_SUPPORT}).` + ); + } + } +});