【问题标题】:How to queue music in a discord.js bot?如何在 discord.js 机器人中排队音乐?
【发布时间】:2018-07-18 13:48:38
【问题描述】:

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

下面是播放命令的文件。

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;

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

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

    祝你好运!

    【讨论】:

    • 感谢您的回答。你有和我类似的设置吗? (即不同文件夹/文件中的命令)
    • 我更喜欢将所有代码放在一个文件中,虽然我的机器人非常基础,但对我来说感觉更有条理。我选择使用我的 npm 音乐机器人插件并使用一组基本命令和其他东西,例如服务器级别和欢迎消息。我希望我的回答对你有所帮助:)
    猜你喜欢
    • 2021-02-07
    • 2020-09-22
    • 2020-08-04
    • 2020-12-01
    • 2021-11-29
    • 2020-09-23
    • 1970-01-01
    • 2020-06-27
    • 2021-05-24
    相关资源
    最近更新 更多