簡體   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