A slash command that delete the last # messages in the channel the command is run it. It only works for admins and the command should have a required 'Integer' option.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const event = context.params.event;
const channel_id = event.channel_id;
// Only allow admins to delete messages
if (!event.member.permission_names.includes('ADMINISTRATOR'))
return await lib.discord.channels['@0.1.2'].messages.create({
channel_id, content: `You are not allowed to delete messages <@!${event.member.user.id}> `
});
// Get the last # messages
const limit = event.data.options[0].value;
const messages = await lib.discord.channels['@0.1.2'].messages.list({ channel_id, limit });
// Bulk delete the messages
try {
await lib.discord.channels['@0.1.2'].messages.bulkDelete({
channel_id, messages: messages.map(m => m.id)
});
} catch (e) {
await lib.discord.channels['@0.1.2'].messages.create({
channel_id, content: `Could not delete the last ${limit} messages. Make sure all messages are less than 2 weeks old.`
});
}