javascript - Node.js 和 Discord.js 中 react 的多个输出

标签 javascript node.js discord.js

我希望机器人对有两种选择的民意调查消息使用react,并让用户对提供的表情使用react,但除此之外,我还想获取用户 ID 并知道谁投票了。 我现在遇到的问题是:第一次民意调查的输出是:

2x the bots id (for both choices)
1x the users id (if they only voted for one choice)
this is how it should be.

但是当我创建另一个民意调查后,输出是:

4x the bots id 
2x the users id 

之后进行第三次民意调查并打印出:

6x the bots id 
3x the users id 
and so on..

因此,在 3 次民意调查之后,我的控制台输出总共是 12 倍的机器人 id 和 6 倍的用户 id,而实际上应该只是 6 倍和 3 倍。我需要在每次轮询后重新启动机器人,以便它打印出正确的输出。 这里究竟发生了什么以及如何解决它?

index.js:

const Discord = require("discord.js");
const tokenfile = require("./token.json");
const bot = new Discord.Client({disableEveryone: true});
bot.commands = new Discord.Collection();
const fs = require("fs");

fs.readdir("./Commands/",(err,files)=>{
    if(err) console.log(err);
    let jsfile = files.filter(f=>f.split(".").pop()==="js")
    if(jsfile.length <= 0){
        console.log("Couldnt find commands.");
        return;
    }
    jsfile.forEach((f, i)=>{
        let props = require(`./Commands/${f}`);
        console.log(`${f} loaded!`);
        bot.commands.set(props.help.name, props);
    });
});

bot.on("ready", async () =>{
    console.log(`${bot.user.username} is online!`);
});

bot.on("message", async message =>{
    if(message.author.bot) return;
    if(message.channel.type === "dm") return;

    let prefix = tokenfile.prefix;
    let messageArray = message.content.split(" ");
    let cmd = messageArray[0];
    let args = messageArray.slice(1);
    let commandfile = bot.commands.get(cmd.slice(prefix.length));
    if(commandfile) await commandfile.run(bot, message, args);
});

bot.login(tokenfile.token); 

poll.js:


module.exports.run = async(bot, message, args)=> {
    let poll = message.content.match(/"(.+?)"/g);
    let bicon = bot.user.displayAvatarURL;
    let botembed = new Discord.RichEmbed()
        .setThumbnail(bicon)
        .setColor("#15f153")
        .addField("Created by:", message.author.username)
        .addField("Poll:", poll);
    message.channel.send(botembed)
        .then(async (pollMessage) => {
            await pollMessage.react('✅');
            await pollMessage.react('❌')
        });
    bot.on('messageReactionAdd', (reaction, user) => {
        if (reaction.emoji.name === "❌") {
                console.log(`${user.id}`);
        }

        if (reaction.emoji.name === "✅") {
            console.log(`${user.id}`);
        }
    });
};

module.exports.help = {
    name: "poll"
}; 

最佳答案

您遇到的行为是因为您将嵌套监听器附加到 messageReactionAdd 事件。每次机器人看到此后添加到任何消息上的 react 时,它都会调用您的监听器函数。这意味着在民意调查后,机器人仍在听取 react ,并将继续吸引更多听众。事实上,在附加一定数量的监听器后,您可能会在控制台中看到有关可能存在内存泄漏的警告。

考虑使用Message.awaitReactions()或直接利用 ReactionCollector 。有关详细信息和示例用法,请参阅超链接文档。

关于javascript - Node.js 和 Discord.js 中 react 的多个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60180248/

相关文章:

javascript - 如何在 JavaScript 中显示 SweetAlert

javascript - 基于JSON排序div

javascript - 使用 PHP 的 HTML5 通知

node.js - Dokku找不到 'webpack'

javascript - Node.js 不同函数之间的全局变量

javascript - Discord.js:是否有一个函数可以检查用户是否具有排名数组中的排名

javascript - 如何使 Dropzone.js 看起来像网站上的主题?

javascript - Replit Discord 机器人不断关闭

javascript - discord.js 中的字体生成器

node.js - 我们如何使用 Angular 按时间戳数据类型对 Firebase Firestore 数据进行排序?