简体   繁体   中英

How to collect 8 reactions on bot's message and then bot send another message

I coded a bot that's sending a message every friday on my server, and puts 8 emoji reactions on it.

This a message that let the member register to an event (1 per day of the week) reacting to this message (one reaction per day). He just has to chose 1 or several reactions to register him to 1 or several day-event.

Here is the code:

const { Client, Intents } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

let currentMtgFormat = "VOW";

const messageDate = new Date();

Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

function getTimestampSeconds(daysFromToday) {
    let ms= messageDate.addDays(daysFromToday);
    let sec= Math.floor((ms/1000));
    return sec;

};

let mondayDate = "<t:" + getTimestampSeconds(3) + ":D>";
let mondayDateShort = "<t:" + getTimestampSeconds(3) + ":d>";
let tuesdayDate = "<t:" + getTimestampSeconds(4) + ":D>";
let wednesdayDate = "<t:" + getTimestampSeconds(5) + ":D>";
let thursdayDate = "<t:" + getTimestampSeconds(6) + ":D>";
let fridayDate = "<t:" + getTimestampSeconds(7) + ":D>";
let saturdayDate = "<t:" + getTimestampSeconds(8) + ":D>";
let sundayDate = "<t:" + getTimestampSeconds(9) + ":D>";
let sundayDateShort = "<t:" + getTimestampSeconds(9) + ":d>";

const msg = "**- Ouverture des inscriptions pour la semaine du " + mondayDateShort + " au " + sundayDateShort + "  -**\n\nPour vous inscrire réagissez à ce poste avec vos jours de disponibilité : \n\n:regional_indicator_l: : Lundi " + mondayDate + " (20h) - Draft " + currentMtgFormat + "\n:regional_indicator_m: : Mardi " + tuesdayDate + " (20h) - Draft " + currentMtgFormat + "\n:regional_indicator_w: : Mercredi " + wednesdayDate + " (20h) - Draft " + currentMtgFormat + "\n:regional_indicator_j: : Jeudi " + thursdayDate + " (20h) - Draft " + currentMtgFormat + "\n:regional_indicator_v: : Vendredi " + fridayDate + " (20h30) - Draft " + currentMtgFormat + "\n:regional_indicator_s: : Samedi " + saturdayDate + " (20h30) - Draft " + currentMtgFormat + "\n:regional_indicator_d: : Dimanche " + sundayDate + " (20h) - Draft " + currentMtgFormat + "\n:alarm_clock:  : Dimanche " + sundayDate + " : Draft Asynchrone (21h) - " + currentMtgFormat + "\n\nDès lors qu'une personne est le 8ème inscrit sur un créneau (ou 16ème pour un éventuel 2ème draft au même horaire), cette dernière DOIT lancer le message de Check-in dans le salon approprié selon le modèle indiqué dans <#911268701528002590> (ou prévenir un modérateur).\n\nLes joueurs inscrits supplémentaires (mais en nombre insuffisant pour constituer une POD) sont considérés comme prioritaires sur les remplacements éventuels (absence de check-in, désistement de dernière minute etc...).";

client.once('ready', () => {
    console.log('IM SUPER READY YEA!')
})

const cron = require('cron');

// client.on('message', ...);

let scheduledPodsMessage = new cron.CronJob('* * * * * *', () => { 
    // for Cron : every " * " above mean one parameter, 
    // from left to right : second 0-59, minute 0-59, hour 0-23, day of month 1-31, month 0-11, day of week 0-6
    // You can use "*" to don't use the parameter

    const channel = client.channels.cache.get("910686979828633611");
    const guild = client.guilds.cache.get("910603170336624640");
    channel.send(msg)
    .then(sentMessage => {
    sentMessage.react(guild.emojis.cache.get('911267403072167966'))
    sentMessage.react(guild.emojis.cache.get('911267403046998016'))
    sentMessage.react(guild.emojis.cache.get('911267403084730418'))
    sentMessage.react(guild.emojis.cache.get('911267403046985738'))
    sentMessage.react(guild.emojis.cache.get('911268283901177876'))
    sentMessage.react(guild.emojis.cache.get('911267403034415114'))
    sentMessage.react(guild.emojis.cache.get('911267403109912606'))
    sentMessage.react('⏰');
    });

    
    console.log('Scheduling cron message just worked')
});

scheduledPodsMessage.start()

client.login('OTEwNjE2NjUxODQ0NzEwNDEy.YZVb2A.yqyQFx9S1JULqOwMpQW2DPjfuq0');

and the message result:

discord screenshot

My question is: How to? :

  • let my bot detect if there is 8 other reactions on 1 react slot (for exemple on the:alarm_clock: reaction),
  • then it sends a message on another channel tagging the 8 members who reacted
  • then delete the 8 reactions from these member
  • knowing that it can take several days to have these 8 reactions on the message

I was thinking about the Message#awaitReactions(), but i'm not sure to understand it.

You should make a collector. Using.then to get the sent message.

.then(msg => {
const collector = msg.createReactionCollector();
    collector.on('collect', i => {
        if (i.users.length === 8){...}
        // Or other code to run... can also call a proper function rather then this arrowed one 
    })
})

Ps: Read the documentation for a full breakdown of the collector interaction payload object. i.users is a list of users that have also reacted to the reaction that has just been added

This will stop collecting if the code stops but you can find the message and recreate the collector again. But the collector's "collect" function will be called on each reaction with a "MessageReaction" Object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM