我正在尝试制作一个具有队列功能的不和谐音乐机器人,但不断出现以下错误。 如何定义连接?
我尝试添加行
const connection = await message.member.voice.channel.join();
定义连接,但是当我添加连接时,出现另一个错误,说明连接已经声明。
(node:14724) UnhandledPromiseRejectionWarning: ReferenceError: connection is not defined
at Client.<anonymous> (E:\Crashbot\index.js:493:14)
at Client.emit (events.js:327:22)
at MessageCreateAction.handle (E:\Crashbot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (E:\Crashbot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\Crashbot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (E:\Crashbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (E:\Crashbot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (E:\Crashbot\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (E:\Crashbot\node_modules\ws\lib\websocket.js:797:20)
(node:14724) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14724) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
var servers = {};
function play(connection, message) {
var server = servers[message.guild.id];
//const connection = await message.member.voice.channel.join();
const dispatcher = connection.play(ytdl(server.queue[0], { filter:
"audioonly" }));
server.dispatcher = connection.play(ytdl(server.queue[0], { filter:
"audioonly" })).then(()=> {
playing = true;
});
dispatcher.on('error', console.error);
server.dispatcher.setVolume(0.2);
server.dispatcher.on("end", function () {
server.queue.shift();
if (server.queue[0]) play(connection, message);
else connection.disconnect().then(()=> {
playing = false;
});
});
}
Crashbot.on('message', async message =>{
//let args = message.content.substring(PREFIX.length).split(" ");
let args = message.content.toString().substring(PREFIX.length).split(" ");
switch (args[0]) {
case "play":
if (!args[1]) {
message.reply(`You need to put a link after ${PREFIX}play to add a song to the queue`)
return;
}
if (!message.member.voice.channel) {
message.channel.send("You must be in a voice channel to use this command")
return;
}
if (!servers[message.guild.id]) servers[message.guild.id] = {
queue: []
}
var server = servers[message.guild.id];
server.queue.push(args[1]);
play(connection, message);
if (!message.guild.voiceConnection) message.member.voice.channel.join().then(function (connection) {
play(connection, message);
});
break;
case "skip":
var server = servers[message.guild.id];
if (server.dispatcher) server.dispatcher.end();
break;
case "queue":
message.channel.send(queue);
break;
case "stop":
var server = servers[message.guild.id];
if (message.guild.voiceConnection) message.guild.voiceConnection.disconnect().then(()=> {
playing = false;
});
break;
}
})