基本上在音乐结束后,我希望机器人与 channel 断开连接。这是我的:
const leave = message => {
return message.guild.voiceConnection
.disconnect()
}
dispatcher.on('end', async reason => {
// The song has finished
console.log('Song has ended', reason);
return leave(message);
});
我收到以下错误,每次离开时机器人都会崩溃:
TypeError: Cannot read property 'disconnect' of null
第一次使用 discord.js,似乎无法理解发生了什么。
最佳答案
您的错误是由于机器人不再位于行会内的语音 channel 中,并且 message.guild.voiceConnection
随后返回 null
。这可能出于您无法控制的各种原因。
在尝试使用 VoiceConnection 之前, 确保它存在。例如:
const leave = message => {
const conn = message.guild.voiceConnection;
if (conn) conn.disconnect();
}; // Semi-colon is not a mistake; it's the end of the assignment.
关于javascript - 在创建不和谐机器人时遇到 .disconnect() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57235370/