如何在discord.js机器人中排队音乐?

时间:2018-02-08 05:02:58

标签: javascript node.js discord discord.js

我创建了一个discord.js机器人,它使用ytdl-core播放音乐,效果很好。但是,当在第一首歌曲播放期间输入相同的命令时,机器人将停止播放音乐并离开。通过在提出这个问题之前进行的广泛研究,我发现我以稍微不同的方式制作我的机器人,这使得很难找到帮助。所以我的问题是如何将后续请求添加到要播放的队列中。

以下是命令播放的文件。

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 个答案:

答案 0 :(得分:0)

如果您想要一个简单的音乐功能,您可能需要考虑this,它可以很好地运作。虽然如果你仍然想制作自己的here是Youtube视频,它帮助我排队。

祝你好运!