javascript - 在创建不和谐机器人时遇到 .disconnect() 问题

标签 javascript bots discord discord.js

基本上在音乐结束后,我希望机器人与 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/

相关文章:

javascript - 解析 JSON 时出现意外标记

c# - 将电报机器人添加到组

java - 线程 "main"java.lang.NoClassDefFoundError : com/google/inject/Module 中出现异常

python - 如何在 discord.py 中安排一个函数在每天的特定时间运行?

javascript - 如何将用户转移到另一个 channel ?

javascript - Chrome headless 渲染完全错误到 pdf

javascript - 如何在 CSS 上为图像添加播放图标

javascript - 如何从来自服务器的日期中以 dd/mm/yyyy 格式仅提取月份和年份(作为字符串)并将其存储在变量中?

robots.txt - 如何禁止来自单个页面或文件的机器人

javascript - 根据 promise 结果运行逻辑