我创建了一个使用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;
如果你想要一个简单的音乐功能,你可以考虑这个,它工作得很好。尽管如果你仍然想自己做,这里有一个YouTube的视频,帮助我排队。
祝你好运!