Search code examples
javascriptdiscord.jsytdl

How do I stop a ytdl stream?


How do I stop a ytdl stream if someone types -skip or something like that? I start the stream like this :

search(searchfor, opts, function(err, results) {
    var rasss = results[0].link;
    const streamOptions = { seek: 0, volume: 1 };
    var voiceChannel = msg.member.voiceChannel;
    voiceChannel.join().then(connection => {
        const stream = ytdl(rasss, { filter : 'audioonly' });
        const dispatcher = connection.playStream(stream, streamOptions);

Solution

  • You could do

    dispatcher.destroy()
    

    To stop the song, then you can play the next song in the queue. To do this, however, you need to define dispatcher outside of your function as null so you can use the dispatcher variable everywhere.

    let dispatcher;
    

    And when actually defining dispatcher,

    dispatcher = connection.playStream(strea, streamOptions); // remove the keyword "const"