Added DM channel purge

This commit is contained in:
WatDuhHekBro 2021-05-18 14:13:41 -05:00
parent 180acb318c
commit 1e673a3969
No known key found for this signature in database
GPG Key ID: E128514902DF8A05
2 changed files with 45 additions and 24 deletions

View File

@ -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: "<amount>",

View File

@ -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}).`
);
}
}
});