CIA
CIA

Reputation: 67

Fix discord music bot queue

The video i watched on this was made around 9 months ago and the queue isn't working anymore. All it does is play a new song instead of queuing it The stop command also isn't working. Im really new to Javascript and am unable to locate any problems but im sure its something that doesnt give off any errors.

I imagine most of the problems are due to outdated code but im not sure. if someone can help that would be amazing.

bot.on('message', message => {

    if (!message.content.startsWith(PREFIX)) return;
    let args = message.content.substring(PREFIX.length).split(" ");


    switch(args[0]) {



        case 'play':

                if(usedCommandRecently4.has(message.author.id)){
                    message.reply("Your using this command to fast!");
                } else{
                    function play(connection, message){
                        var server = servers[message.guild.id];

                        server.dispatcher = connection.play(ytdl(server.queue[0], {filter: "audioonly"}));

                        server.queue.shift();

                        message.channel.send("``Music Bot v1.2`` \n Adding song to queue!");

                        server.dispatcher.on("end", function(){
                            if(server.queue[0]){
                                play(connection, message);
                            }else {
                                connection.disconnect();
                            }
                        })
                    }

                    if(!args[1]){
                        message.channel.send("``Music Bot v1.2`` \n you need to provide a link!!");
                        return;
                    }

                    if(!message.member.voice.channel){
                        message.channel.send("``Music Bot v1.2`` \n You must be in a voice channel to play music!");
                        return;
                    }

                    if(!servers[message.guild.id]) servers[message.guild.id] = {
                        queue: []
                    }

                    var server = servers[message.guild.id];

                    server.queue.push(args[1]);

                    if(!message.guild.voiceConnection)message.member.voice.channel.join().then(function(connection){

                        play(connection, message);

                    })

                    usedCommandRecently4.add(message.author.id);
                    setTimeout(() => {
                        usedCommandRecently4.delete(message.author.id)
                    }, 10000);
                }

            break;

            case 'skip':

                if(usedCommandRecently3.has(message.author.id)){
                    message.reply("Your using this command to fast!");
                } else{
                    var server = servers[message.guild.id];
                    if(server.dispatcher) server.dispatcher.end();
                    message.channel.send("``Music Bot v1.2`` \n Skipping the current song!")

                    usedCommandRecently3.add(message.author.id);
                    setTimeout(() => {
                        usedCommandRecently3.delete(message.author.id)
                    }, 3000);
                }

            break;

            case 'stop':
                var server = servers[message.guild.id];
                if(message.guild.voiceConnection){
                    for(var i = server.queue.length -1; i >=0; i--){
                        server.queue.splice(i, 1);
                    }

                    server.dispatcher.end();
                    message.channel.send("``Music Bot v1.2`` \n Ending the queue and Leaving the voice channel! \n This bot is in early development! \n if you have any problems with it dm Justice#6770!")
                    console.log('stopped the queue')
                }
            }

            if(message.guild.connection) message.guild.voiceConnection.disconnect();

})

Upvotes: 2

Views: 7894

Answers (2)

Seiya.Exe
Seiya.Exe

Reputation: 36

I solved the queue problem with this order, the problem with this code is whe you add a song to the queue, the first song start again from the beginning, i dont know how solve this little problem

case 'play':

        function play(connection, message){
            var server = servers[message.guild.id];

            server.dispatcher = connection.play(ytdl(server.queue[0],{filter: "audio"}));



            server.dispatcher.on("finish", function(){
               server.queue.shift();
               if(server.queue[0]){
                   play(connection, message);
               }else {
                   connection.disconnect();
               }
            })

        }         

Upvotes: 2

Tenclea
Tenclea

Reputation: 1464

The stop command might be broken because the message.guild.voiceConnection does not exist anymore.

You'll need to use message.guild.voice.connection instead.

Regarding the queue problem, it seems that your play function is always shifting the queue and playing the new song in any case. You should try to move the

server.queue.shift();
server.dispatcher = connection.play(ytdl(server.queue[0], {filter: "audioonly"}));

in this exact order, inside of the server.dispatcher.on("end", function(){ //... event to see if this changes anything :)

Upvotes: 0

Related Questions