【问题标题】:having issues with .disconnect() whilst creating a discord bot在创建不和谐机器人时遇到 .disconnect() 问题
【发布时间】:2019-12-05 16:53:15
【问题描述】:

基本上在音乐结束后,我希望机器人从频道断开连接。这就是我所拥有的:

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 的属性“断开连接”

第一次使用 discord.js,似乎无法理解发生了什么。

【问题讨论】:

    标签: javascript bots discord discord.js


    【解决方案1】:

    您的错误是由于该机器人不再位于公会内的语音频道中,而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.
    

    【讨论】:

      【解决方案2】:

      您不必断开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();
      

      【讨论】:

      • 如果message.guild.voiceConnectionnullmessage.guild.members.get(client.user.id).voiceChannel 也将返回null。这无法解决问题。
      猜你喜欢
      • 2022-11-10
      • 2019-02-26
      • 2017-12-29
      • 2020-07-20
      • 2020-06-08
      • 2019-08-03
      • 2020-12-25
      • 1970-01-01
      • 2021-03-04
      相关资源
      最近更新 更多