繁体   English   中英

Discord.js 音乐机器人出错

[英]Discord.js music bot getting errors

我的音乐机器人有这段代码:
我的代码
它运行正常,但播放 5-6 首歌曲后出现此错误:

 TypeError: Cannot read property 'connection' of undefined at play (C:\Users\User\Desktop\test bot\index.js:201:36) at execute (C:\Users\User\Desktop\test bot\index.js:162:9) at processTicksAndRejections (internal/process/task_queues.js:93:5) (node:6152) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message at RequestHandler.execute (C:\Users\User\Desktop\test bot\node_modules\discord.js\src\rest\RequestHandler.js:170:25) at processTicksAndRejections (internal/process/task_queues.js:93:5) (Use `node --trace-warnings...` to show where the warning was created) (node:6152) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:6152) [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.

它停止播放。 我该如何解决这个问题提前谢谢!

我相信这部分代码(在execute方法中)是导致问题的原因:

try {
    var connection = await voiceChannel.join();
    queueContruct.connection = connection;
    play(message.guild, queueContruct.songs[0]);
  } catch (err) {
    console.log(err);
    queue.delete(message.guild.id);
    return message.channel.send(err);
  }

正在发生的事情是在try块中发生错误。 我不知道是什么原因导致该错误,这可能是机器人尝试加入语音频道时发生的错误。 但是当发生该错误时,将执行catch块,并且catch块会删除公会的队列并尝试将错误作为消息发送。 现在这实际上会导致您遇到的两个错误。 play方法无法读取未定义的属性connection ,因为它正在尝试执行serverQueue.connection ,但您的catch块已删除serverQueue DiscordAPI 错误(“无法发送空消息”)是尝试发送包含err object 的消息的结果,它是 object 而不是字符串(从而导致 Discord 尝试发送空消息)。

要解决第一个问题,您可能需要更改几项内容。 首先,确保使用if语句检查您的机器人是否已经在try块中的语音通道(或它试图加入的语音通道)中。 如果您的机器人在语音通道中已经存在时尝试加入语音通道,则会发生错误,从而导致出现您的问题。 其次,不要只是删除catch块中的队列。 您可能应该让机器人首先离开它当前所在的语音通道,并结束当前行会中执行play方法的任何实例,这样问题就不会重复出现。 要解决第二个问题,请尝试执行message.channel.send(err.stack)而不是执行message.channel.send(err) ) 。 这应该确保它是一个字符串,而不是作为消息发送到通道的 Object。

您可能需要自己进行一些额外的调试,以找出在try块中实际导致错误的原因。 完成此操作后,您需要做的是防止递归play方法继续播放到 function,即使在删除队列后也是如此。 如果可以在返回 Promise 的特定操作(例如加入语音频道)上使用.catch()方法(我不知道是否voiceChannel.join()返回 Promise 但如果返回,您可能应该使用.catch() )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM