Creates a prefix command `!burn` that sets fire to recent messages before burning them to a crisp (aka an alternate purge command). Recommended to set the max timeout to 30s.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const {channel_id, content} = context.params.event;
// ** DEV CONFIG **
const command = '!burn';
const limitDefault = 10;
// Is this our command?
if (!content.startsWith(command)) return
// Other config
const limitMax = 25; // Discord rate limit us so it's ~1 message per second
const burnDelay = 537;
const destroyDelay = 462;
// Helper functions
const sleep = ms => new Promise(r => setTimeout(r, ms));
const shuffle = array => array
.map((value) => ({value, sort: Math.random()}))
.sort((a, b) => a.sort - b.sort)
.map(({value}) => value);
// Get the messages
const limit = Math.min(limitMax, parseInt(content.replace(command, '').trim()) || limitDefault);
const messages = await lib.discord.channels['@0.1.1'].messages.list({ channel_id, limit });
// Handle messages in a queue to allow for retries
const queueBurn = shuffle(messages);
const queueDestroy = []
/** Processes the 'burn' queue which adds the fire emoji then queues the message for destruction. */
async function burn() {
while (queueBurn.length > 0) {
const message = queueBurn.shift();
const message_id = message.id;
await lib.discord.channels['@0.2.0'].messages.reactions
.create({ channel_id, message_id, emoji: `π₯` })
.then(() => queueDestroy.push(message))
.then(() => sleep(burnDelay))
.catch(() => {
console.log(`** Burn rate limit`);
queueBurn.push(message)
return sleep(200)
})
}
}
/** Processes the 'destroy' queue which destroys the messages. */
async function destroy() {
await sleep(4000)
while (queueBurn.length > 0 || queueDestroy.length > 0) {
const message = queueDestroy.shift();
const message_id = message.id;
await lib.discord.channels['@0.2.0'].messages.destroy({ channel_id, message_id })
.then(() => sleep(destroyDelay))
.catch(() => {
console.log(`** Destroy rate limit`);
queueDestroy.push(message)
return sleep(200)
})
}
}
// Process messages until everything is deleted
await Promise.allSettled([
burn().catch(e => console.log),
destroy().catch(e => console.log),
]);