创建discord机器人时,.disconnect()遇到问题

时间:2019-07-27 19:21:48

标签: javascript bots discord discord.js

基本上,音乐播放完毕后,我希望漫游器与频道断开连接。这就是我所拥有的:

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:无法读取null的属性“ disconnect”

第一次使用discord.js,似乎无法理解正在发生的事情。

2 个答案:

答案 0 :(得分:0)

您不必断开voiceConnection的连接。

像错误一样:property 'disconnect' of nullmessage.guild.voiceConnectionnull

只需获取漫游器所在的语音通道,然后将其保留即可:

// client is here your Discord.Client();
client.guilds.get("YOUR_GUILD_ID").members.get(client.user.id).voiceChannel.leave();

或者如果您已经有一个message对象,则可以跳过公会路径:

// client is here your Discord.Client();
message.guild.members.get(client.user.id).voiceChannel.leave();

答案 1 :(得分:0)

您的错误是由以下原因引起的:机器人不再处于行会的语音通道中,并且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.