如何在 discord.js 机器人中对音乐进行排队?
How to queue music in a discord.js bot?
我创建了一个 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;
我创建了一个 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;