1
votes

I have created a discord.js bot that plays music using ytdl-core and it works well. However, when the same command is entered during the first song playing, the bot will stop playing music and leave. From extensive research conducted prior to this question being asked, I have discovered that I made my bot in a slightly different way which makes it difficult to find help. So my question is how do I add subsequent requests to a queue to be played.

Below is the file for the command play.

const Commando = require('discord.js-commando');
const yt = require('ytdl-core');
const Bot = new Commando.Client();

class play extends Commando.Command {

    constructor(client){
        super(client, {
            name:'play',
            group:'play',
            memberName:'play',
            description:'plays videos from youtube'
        });
    }

    async run(message, args){

        const voiceChannel = message.member.voiceChannel;
        if (!voiceChannel){
            return message.channel.sendMessage("you must be in a voice channel to request me");
        }

        if (args === ""){
            message.reply('i need a link to play a youtube video');
            console.log('message didnt send');
        }
        else {
            if (message.content.includes("http://") || message.content.includes("https://")) {
                if (message.content.includes("youtube") || message.content.includes("youtu.be")) {

                    message.channel.sendMessage(":white_check_mark: **connected**");
                    voiceChannel.join()
                    .then(connection => {
                        const args = message.content.split(" ").slice(1);
                        let stream = yt(args.join(" "));
                        yt.getInfo(args.join(" "), function(err, info) {
                            const title = info.title
                            message.channel.sendMessage(`this song was requested \`${title}\`.)
                        })
                        const dispatcher = connection.playStream(stream, {audioonly: true});
                        dispatcher.on('end', () => {

                            voiceChannel.leave();
                            message.channel.sendMessage('song finished')
                        }).catch(e =>{
                            console.error(e);
                        });
                    })
                } else {
                    message.reply('only youtube links are allowed');
                }
            } else {
                message.reply('only youtube links are allowed');
            }
        }
    }
}
module.exports = play;
1

1 Answers

1
votes

You may want to consider this if you want an easy music function, it works quite well. although if you still want to make your own here is a Youtube video that helped me with making a queue.

Good luck!