Discordjs How do I when finishing an audio it plays another

Asked

Viewed 94 times

0

My doubt is this, I have a code that he bot connects to the voice channel of Discord when user command "!! play url_video_youtube". When again typed command is incremented the array "listMusic". My doubt is how I do when finishing current music it advances to next.

In "Dispatcher.on('Finish') I can know when you finished the song, I just don’t know how to play next song according to array item.

async onPlayMusic (message:Message, url:string){
    listMusic.push(url);

    const voiceChannel = message?.member?.voice.channel;

        if (!voiceChannel) {
            return message.reply('precisa está em canal de voz!');
    }
    
    [0,1]

    voiceChannel.join().then(connection => {
            const stream = ytdl(url, { filter: 'audioonly' });
            const dispatcher = connection.play(stream);

            dispatcher.on('finish', () => {
        
        let musics = listMusic.length;
        
        if(musics > 1){
          listMusic[listMusic.length-1];
        }
        //voiceChannel.leave()
      });
    });
  },

1 answer

0


Basically you have to create a loop that checks how many links there are in your array, and that when finished playing it passes to the next item of array. It would be something like this:

let url = ["www.musica.com", "www.proxima-musica.com"];// não sei como você cria o array com os link de reprodução, mas deve ficar asim imagino.
let index = 0;

function playMusic()//crisse uma função para que seja chamada novamente dentro de "on.finish"
{
    if ( url.length >= index )
    {
        voiceChannel.join().then(connection => 
        {   
            const stream = ytdl(url[index], { filter: 'audioonly' });
            const dispatcher = connection.play(stream);

            dispatcher.on('finish', () => 
            {
                let musics = listMusic.length;
                
                if(musics > 1)
                {
                  listMusic[listMusic.length-1];
                }

                index++//quando terminar de reproduzir ele acrecentar +1 no index;
                playMusic();//callback, que força um loop
            });
        });
    }else{
        //não á mais links no array para serem reproduzidas.
    }
}
  • Thanks. I’m going to a study in callback found very interesting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.