javascriptnode.jsvote

How can I get my discord bot not to take multiple votes from a single person


I'm making a discord bot that has a skip feature for the music, but I can't figure out how to make someone not be able to vote to skip multiple times. So far here's my code:

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;

If you guys could help me out it would be great


Solution

  • Store the id's of people who have already voted in an array. For each vote, check if that id already exists in the array, if it does, ignore the vote.

    You need to declare the array in a scope that will persist outside of the http request. (I'm assuming this is server code, i'm not familiar with how discord bots work) It's probably not worth storing it in a database.

    var voters = []
    

    Replace your integer counter with the length of the array.

    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;