Sends a message to Discord if the number of reactions on a message pass a certain threshold and has extra checks to ensure this only happens once.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const { message_id, channel_id } = context.params.event
// ** Set your config here ***
const emoji= '👍';
const triggerCount = 3;
const triggeredMessage = `This message was triggered after ${triggerCount} ${emoji} reactions`
// Is this the right emoji?
if (context.params.event.emoji.name !== emoji) return
const botId = (await lib.discord.users['@0.1.4'].me.list()).id;
// We use the bot's reaction to prevent mutliple triggers.
// Get the existing reactions and check if the bot has reacted.
let reactions = await lib.discord.channels['@0.1.2'].messages.reactions.list({
emoji, message_id, channel_id,
});
const botHasReacted = !!reactions.find(x => x.id === botId)
if (!botHasReacted && reactions.length >= triggerCount)
{
// React with emoji so we know the lock the trigger
await lib.discord.channels['@0.1.2'].messages.reactions.create({
emoji, message_id, channel_id,
});
// Send triggered message
await lib.discord.channels['@0.1.2'].messages.create({
channel_id, content: triggeredMessage,
});
}