我正在使用node.js在JavaScript中创建Discord机器人 我正在尝试使机器人与语音聊天断开连接,并出现以下错误。
(node:7324) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'then' of undefined
at Object.module.exports.run (C:\Users\Edd\Desktop\BlueHoleBot\commands\leavevc.js:10:11)
at Client.bot.on (C:\Users\Edd\Desktop\BlueHoleBot\BHB.js:44:33)
at Client.emit (events.js:189:13)
at MessageCreateHandler.handle (C:\Users\Edd\Desktop\BlueHoleBot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Users\Edd\Desktop\BlueHoleBot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\Users\Edd\Desktop\BlueHoleBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\Users\Edd\Desktop\BlueHoleBot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\Users\Edd\Desktop\BlueHoleBot\node_modules\ws\lib\event-target.js:120:16)
at WebSocket.emit (events.js:189:13)
at Receiver._receiver.onmessage (C:\Users\Edd\Desktop\BlueHoleBot\node_modules\ws\lib\websocket.js:137:47)
(node:7324) 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(). (rejection id: 1)
(node:7324) [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.
我尝试多次重写代码,删除文件,在discord.js网站上查找引用,无论我仍然写什么,都会出现相同的错误。
const Discord = require ("discord.js")
module.exports.run = async (bot, message, args) => {
if (!message.guild) return;
// Only try to join the sender's voice channel if they are in one themselves
if (message.member.voiceChannel) {
if (message.member.voiceChannel) {
message.member.voiceChannel.leave()
.then(connection => { // Connection is an instance of VoiceConnection
message.reply('I have successfully disconnected to the channel!');
console.log("disconnected.")
})
.catch(console.log);
} else {
message.reply("Not connected to any voice channel.");
}
}}
module.exports.help = {
name: "leave"
}