未检测到 Discord js 反应

2022-01-10 00:00:00 javascript discord.js

discordjs 有一些我不明白的地方.我想制作一个机器人,在人们对消息做出反应时列出他们.它部分有效,当输入命令的人(或之前输入过命令的人)对机器人的消息做出反应时,消息会立即编辑.但是当它是从未输入过命令的人时,它并没有更新.

There's something I don't understand with discordjs. I want to make a bot which lists people when they react on a message. It partially works, when the guy who enter the commands (or who has enter a command before) reacts to the bot's message, the message edits immediately. But when it's someone who has never entered command, it didn't update.

const Discord = require('discord.js');
const client = new Discord.Client();
var auth = require('./auth.json');

const regexTime = new RegExp('^(0[0-9]|1[0-9]|2[0-3]|[0-9])([:|h])([0-5][0-9])?$');

var messageBot = "";
var time;
var timer;
var commandeValide=false;
var date = new Date;
var heureMs;

client.on('ready', () => {
    console.log(client.guilds.fetch())
});

client.on('message', msg => {
    
    if (msg.content.substring(0,1) === '!') {

        var args = msg.content.substring(1).split(' ');
        var cmd = args[0];

        switch(cmd){
            case 'amongus':

                dateGame = args[1];
                time = args[2];
                messageBot = '@everyone Est-ce que des personne veulent faire un Among us à '+ time + ' le '+ dateGame;

                if ( dateGame != undefined && time != undefined ){

                    var heure = time.split('h')[0] * 3600000;
                    var minute = time.split('h')[1] * 60000;
                    var temps = heure + minute;
                    heureMs = date.getHours() * 3600000 + date.getMinutes() * 60000;

                    if(regexTime.test(time) && isDate(dateGame)){

                        if(temps>heureMs){

                            commandeValide=true;
                            msg.channel.send(messageBot).then(sendMessage => {
                                sendMessage.react('✅')
                            });
                            timer = temps - heureMs;
                        }
                    }else{
                        msg.reply("Veuillez rentrer une heure ou une date valide!");
                        commandeValide=false;
                    }
                }else{
                    msg.reply("Veuillez rentrer une heure et/ou une date s'il vous plaît! (exemple: !amongus 19/04 20h)");
                    commandeValide=false;
                } 
        }
    }

    if(commandeValide){

        const filter = (reaction, user) => {
            console.log(client.users.cache);
            //return ['✅'].includes(reaction.emoji.name);
            return reaction.emoji.name === '✅' && user.id !== msg.author.id;
        };
    
        const collector = msg.createReactionCollector(filter, { dispose: true, time: timer }); //dispose: true permet d'utiliser remove
    
        collector.on('collect', (reaction, user) => {
            reaction.users.fetch().then((user) => {
                updateMessage(user.array(),msg);
            });
        });
    
        collector.on('remove', (reaction, user) => {
            reaction.users.fetch().then((user) => {
                updateMessage(user.array(),msg);
            });
        });
    
        collector.on('end', collected => {
            console.log(`Collected ${collected.size} items`);
        });

    }
    

});

function updateMessage(tab, msg){
    var listparticipant = "";
    tab.forEach(user => {
        if(user.id !== auth.server_id){
            listparticipant += "- " + user.username + "
";
        }
    })
    msg.edit(messageBot + "
" + listparticipant);
    console.log(listparticipant);
}
client.login(auth.token);

推荐答案

Discord 改变了它们发出事件的数量,请确保将正确的意图放在您的机器人上,然后重试!

Discord changed how much they emit events, make sure to put he proper intents on your bot and try again!

const client = new Discord.Client({
    ws : {
        intents: [
            'GUILD_MEMBERS', 
            'GUILD_MESSAGES',
            'GUILD_MESSAGE_REACTIONS' //<--- the intent you need to detect reactions on messages in a guild
        ]
    }
});

相关文章