1 回答

TA貢獻1765條經驗 獲得超5個贊
錯誤消息Cannot read property 'songs' of undefined表明之前的變量.songs未定義。在這種情況下,queue是未定義的。
在有效的代碼中,您可以正確處理它:queue在訪問其.songs.
if (!queue) // handle if queue is undefined
return message.channel.send({
embed: { color: 'ff0000', description: `Nothing's playing right now.` },
});
// queue is guaranteed to be not undefined here
const song = queue.songs[0];
但是,在導致錯誤的代碼中,您沒有在setInterval處理程序中處理它。
/** in setInterval() **/
const queue = message.client.queue.get(message.guild.id);
// queue may be undefined!
const song = queue.songs[0]; // error occurs if queue is undefined!
要修復錯誤,您需要做的就是像處理有效代碼一樣處理未定義的情況。例如:
const queue = message.client.queue.get(message.guild.id);
if (!queue) return clearInterval(interval); // when queue is gone, stop editing the embed message
// queue is guaranteed to be not undefined here!
const song = queue.songs[0]; // OK!
添加回答
舉報