简体   繁体   English

未检测到 Discord js 反应

[英]Discord js reaction not detected

There's something I don't understand with discordjs.我对 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 + "\n";
        }
    })
    msg.edit(messageBot + "\n" + listparticipant);
    console.log(listparticipant);
}
client.login(auth.token);

Discord changed how much they emit events, make sure to put he proper intents on your bot and try again! Discord 更改了它们发出事件的数量,请确保将正确的意图放在您的机器人上,然后再试一次!

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
        ]
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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