javascript - 我怎样才能让我的不和谐机器人不从一个人那里获得多票

标签 javascript node.js vote

我正在制作一个具有音乐跳过功能的不和谐机器人,但我不知道如何让某人无法多次投票跳过。到目前为止,这是我的代码:

case "skip":
        var server = servers[message.guild.id];
        if(message.guild.members.get(message.author.id).roles.exists('name','Super Skip')){ // checks if the member has the super skip role
            skipvotes = 0; // sets skipvotes back to 0
            messagesend("Skipping Song")
            server.dispatcher.end(); // skips song
            message.delete(); // deletes message that sent the commmand
            return;
        }
        skipvotes++ // increases the skipvotes
        if(skipvotes != 5) { // checks if the number of skips is 5 or not
            message.delete();
            messagesend(5-skipvotes + " more vote(s) to skip the song") // sends the message saying how many more votes to skip the song
        } else if(skipvotes === 5){
            skipvotes = 0; // sets the number of skipvotes back to 0
            if(server.dispatcher){
                message.delete();
                messagesend("Skipping song")
                server.dispatcher.end(); // skips the song
            }
        }
        break;

如果你们能帮助我,那就太好了

最佳答案

将已经投票的人的 ID 存储在一个数组中。对于每次投票,检查该 id 是否已存在于数组中,如果存在,则忽略该投票。

您需要在将在 http 请求之外持续存在的范围内声明数组。 (我假设这是服务器代码,我不熟悉 discord 机器人的工作原理)可能不值得将它存储在数据库中。

var voters = []

用数组的长度替换你的整数计数器。

case "skip":
        var server = servers[message.guild.id];
        if(message.guild.members.get(message.author.id).roles.exists('name','Super Skip')){ // checks if the member has the super skip role
            voters = []
            messagesend("Skipping Song")
            server.dispatcher.end(); // skips song
            message.delete(); // deletes message that sent the commmand
            return;
        }

        if(voters.find(id=>id == message.author.id))
            return;

        voters.push(message.author.id)

        if(voters.length != 5) { // checks if the number of skips is 5 or not
            message.delete();
            messagesend(5-voters.length + " more vote(s) to skip the song") // sends the message saying how many more votes to skip the song
        } else if(voters.length === 5){
            voters = []
            if(server.dispatcher){
                message.delete();
                messagesend("Skipping song")
                server.dispatcher.end(); // skips the song
            }
        }
        break;

关于javascript - 我怎样才能让我的不和谐机器人不从一个人那里获得多票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128483/

相关文章:

javascript - 即使在 e.preventDefault() jquery 之后页面也会刷新

ruby-on-rails - Rails,实现投票系统,每个用户只允许 1 个赞成票/反对票

javascript - 希望在单击幻灯片上的下一个/上一个按钮后立即在图像上显示文本

javascript - 如何通过 id 将数组元素与指令中的 dom 类元素绑定(bind)?

node.js - 使用 webpack 导入 monaco-editor 时找不到依赖项

node.js - 比较 Mongoose _id和字符串

javascript - 线接触或矩形或圆形

survey - 验证用户投票一次的民意调查

web-services - 网络语言中允许非注册用户投票的一般技术是什么?

javascript - 如何等待消息框的返回值