如何让我的不和谐机器人不要从一个人那里获得多张选票

时间:2017-11-06 01:26:06

标签: 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;

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

1 个答案:

答案 0 :(得分:2)

存储已投票的人的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;