Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
discord.js music bot dispatcher.on("finish") logic runs multiple times removing songs from queue and then disconnecting bot

I set up a discord bot for taking youtube url's and playing them in the current voice channel the user who initiated the bot command is in. The bot can create a queue of multiple youtube links but when the first song in queue finishes playing the following code below is giving me an issue where it seems that it will run the logic as many times as there are songs in the queue. For example, if there are 6 songs in queue, the server.dispatcher.on(finish) logic executes 6 times in a row removing the songs in queue one by one until there are no songs left in queue and then the bot disconnects from the voice channel.

function playF(connectionF, message){
        console.log("starting the play function ");
        var server = servers[message.guild.id];

        if(!playingMusic){
            console.log("not playing music, running this logic ");
            server.dispatcher = connectionF.play(ytdl(server.queue[0], {filter: "audioonly"}));
            server.queue.shift();
            playingMusic = true;
        }

        
        server.dispatcher.on("finish", () => {
            console.log("running the finish logic "); 
            console.log("heres your queue length in finish " + server.queue.length); 
            playingMusic = false;
            if(server.queue[0]){
                playF(connectionF, message);
            } else {
                connectionF.disconnect();
            }
        });
        
    }
    ```
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So I figured out that I needed to restructure a bit to add the .on("finish") to the server.dispatcher on line 8. The following code is what fixed my issue.

function playF(connectionF, message){
        console.log("starting the play function ");
        var server = servers[message.guild.id];

        if(!playingMusic){
            playingMusic = true;
            console.log("not playing music, running this logic ");
            server.dispatcher = connectionF.play(ytdl(server.queue[0], {filter: "audioonly"})).on("finish", () =>{
                playingMusic = false;
                if(server.queue[0]){
                    playF(connectionF, message);
                } else {
                     connectionF.disconnect();
                }
            });

            server.queue.shift();
            
        }
        
    }
    ```
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda