【问题标题】:How do i see if a user has voted already, using reactions, and if they have; stop the vote from counting? discord.js我如何查看用户是否已经投票、使用反应以及是否已经投票;停止计票?不和谐.js
【发布时间】:2020-03-29 09:26:24
【问题描述】:

我正在尝试为我朋友的一个服务器编写一个不和谐机器人。他希望禁止命令成为多数投票的事情,如果用户试图禁止另一个用户,机器人会添加对消息的反应,当时间限制到时,机器人会根据是否有用户决定禁止用户5 票赞成。否则,禁令不会通过。

但是,我的问题是,一个用户可能会反复对勾号表情做出反应,这增加了越来越多的赞成票。我正在寻找一种方法,我可以查看用户是否已经投票,如果他们已经投票,则让他们的其他投票不计入分数。

我的代码如下所示:

        const user = msg.mentions.users.first();
        if(!user) return msg.reply("You need to @ someone you want me to ban");
        await msg.react('✅');
        await msg.react('❌');

        const filter = (reaction, user) => reaction.emoji.name === '✅' && msg.author.id;
        const collector = msg.createReactionCollector(filter, { time: 15000 });
        let yesvotes = 0
        collector.on('collect', collected => {
            console.log(`Collected ${collected.emoji.name}`)
            yesvotes += 1;
        });
        collector.on('end', collected => {
            msg.channel.send(`Collected ${yesvotes} votes`)
            if(yesvotes >= 5) {
                // ban the user
            }
        });```

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    我修复了一个小问题第 5 行,只有消息作者能够做出反应。 我将反应用户存储到一个数组中,如果列表中没有,则仅将它们添加到计数中。 ^^

    const user = msg.mentions.users.first();
    if(!user) return msg.reply("You need to @ someone you want me to ban");
    await msg.react('✅');
    await msg.react('❌');
    
    const filter = (reaction, user) => reaction.emoji.name === '✅';
    const collector = msg.createReactionCollector(filter, { time: 15000 });
    const usersStored = [];
    let yesvotes = 0
    collector.on('collect', (collected, user) => {
        console.log(`Collected ${collected.emoji.name}`)
        if(!usersStored.includes(user.id)){
            yesvotes += 1;
            usersStored.push(user.id)
        }
    });
    collector.on('end', collected => {
        msg.channel.send(`Collected ${yesvotes} votes`)
        if(yesvotes >= 5) {
            // ban the user
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      相关资源
      最近更新 更多