1 回答

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
錯(cuò)誤消息Cannot read property 'songs' of undefined表明之前的變量.songs未定義。在這種情況下,queue是未定義的。
在有效的代碼中,您可以正確處理它:queue在訪問(wèn)其.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];
但是,在導(dǎo)致錯(cuò)誤的代碼中,您沒(méi)有在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!
要修復(fù)錯(cuò)誤,您需要做的就是像處理有效代碼一樣處理未定義的情況。例如:
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!
添加回答
舉報(bào)