【发布时间】:2018-08-24 16:52:52
【问题描述】:
当我在 discord 中运行以下脚本(cmd 中的“node musicbot.js”)和“!play ytlink”时,机器人会加入语音通道并在控制台中记录命令和链接。然而,音乐并没有开始播放。我已经安装了 ffmpeg、ytdl-core 和 discord.js。
有人可以帮帮我吗?我不知道是哪个部分搞砸了。
const Discord = require("discord.js");
const ytdl = require("ytdl-core");
const config = require("./config.json");
const bot = new Discord.Client();
let queue = [];
function play(connection, message) {
let audio = ytdl(queue[0], {filter: "audioonly"});
let dispatcher = connection.playStream(audio);
dispatcher.on("end", function() {
queue.shift();
if (queue[0]) play(connection, message);
else {
connection.disconnect();
message.channel.send("The queue has ended");
}
});
}
bot.on("message", function(message) {
if (message.channel.type === "dm") return;
if (!message.content.startsWith(config.prefix) || message.author.bot)
return;
let arguments = message.content.split(" ");
let command = arguments[0].toLowerCase();
arguments.shift();
console.log(command);
console.log(arguments);
if (command == "!play") {
if (!arguments[0]) {
message.channel.send("Please provide a YouTube link!");
message.delete();
return;
}
if (!message.member.voiceChannel) {
message.channel.send("Please join a Voice Channel first!");
message.delete();
return;
}
queue.push(arguments[0]);
message.member.voiceChannel.join()
.then(connection => {
play(connection, message);
});
}
});
bot.on("ready", function() {
console.log("Ready");
});
bot.login(config.token);
【问题讨论】:
标签: bots discord discord.js