亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤:無法讀取未定義的屬性“歌曲”-discord.js

錯誤:無法讀取未定義的屬性“歌曲”-discord.js

繁星淼淼 2023-06-09 17:45:21
我對編碼比較陌生,目前正在嘗試制作一個簡單的音樂機器人。要求很高的功能之一是每 5 秒更新一次的“正在播放”嵌入。幾天前,我在 Stack Overflow 上的其他人的幫助下嘗試實現這個,但我收到這個錯誤,我不知道如何修復,因為我現在只做了幾個月的 discord.js。我將向您展示有效的代碼和無效的代碼,在此先感謝您的幫助!const queue = message.client.queue.get(message.guild.id);if (!queue) return message.channel.send({  embed: { color: 'ff0000', description: `Nothing's playing right now.` }, });const song = queue.songs[0];const seek = (queue.connection.dispatcher.streamTime -  queue.connection.dispatcher.pausedTime) / 1000;const left = song.duration - seek;let nowPlaying = new MessageEmbed() .setTitle('Now playing:') .setDescription(`${song.title}`) .setColor('#ff0000') .setThumbnail('https://img.icons8.com/clouds/2x/play.png') .addField(  '\u200b',  new Date(seek * 1000).toISOString().substr(11, 8) +   '[ ' +   createBar(song.duration == 0 ? seek : song.duration, seek, 10)[0] +   '] ' +   (song.duration == 0    ? ' ? LIVE'    : new Date(song.duration * 1000).toISOString().substr(11, 8)),  false );if (song.duration > 0) nowPlaying.setFooter(  'Time Remaining: ' + new Date(left * 1000).toISOString().substr(11, 8) );message.channel.send(nowPlaying);
查看完整描述

1 回答

?
POPMUISE

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!


查看完整回答
反對 回復 2023-06-09
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號