This code allows you to automatically purge or delete messages on multiple channels all at once. Just change channel_ids to your desired channels. I tested it on up to nine channels, pretty good for spam messages or discord nitro links popping up in multiple channels at once. You can also delete messages by user, this string is optional. Snippet assistance from MEIRABA and Kangabru. To set this command up you need to go to the slash command builder (https://autocode.com/tools/discord/command-builder/), link your bot and create a slash command named "purge" with two options, 1st option should be integer type and is the number of messages to clear, and the 2nd option should be user type (make the second option optional). Save and happy hacking!!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const channel_ids = [
'channel_id1',
'channel_id2',
'channel_id3',
'channel_id4',
'channel_id5',
'channel_id6',
'channel_id7',
'channel_id8',
'channel_id9',
];
if (!context.params.event.member.permission_names.includes('MANAGE_MESSAGES')) {
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `You are not authorized to use this command, <@${context.params.event.member.user.id}>.`,
});
return;
}
let amount = context.params.event.data.options[0].value;
let userID = context.params.event.data.options[1]
? context.params.event.data.options[1].value
: null;
for (let channel_id of channel_ids) {
let messages = await lib.discord.channels['@0.1.1'].messages.list({
channel_id: channel_id,
limit: 100,
});
let messages_to_delete = messages.map((m) => m.id).slice(0, amount);
if (userID) {
messages_to_delete = messages
.filter((m) => m.author.id === userID)
.map((m) => m.id)
.slice(0, amount);
}
if (amount <= 1) {
await lib.discord.channels['@0.2.0'].messages.destroy({
channel_id,
message_id: messages_to_delete[0],
});
} else if (amount >= 1) {
await lib.discord.channels['@0.2.0'].messages.bulkDelete({
channel_id,
messages: messages_to_delete,
});
}
}