第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

錯(cuò)誤:無(wú)法讀取未定義的屬性“歌曲”-discord.js

錯(cuò)誤:無(wú)法讀取未定義的屬性“歌曲”-discord.js

繁星淼淼 2023-06-09 17:45:21
我對(duì)編碼比較陌生,目前正在嘗試制作一個(gè)簡(jiǎn)單的音樂(lè)機(jī)器人。要求很高的功能之一是每 5 秒更新一次的“正在播放”嵌入。幾天前,我在 Stack Overflow 上的其他人的幫助下嘗試實(shí)現(xiàn)這個(gè),但我收到這個(gè)錯(cuò)誤,我不知道如何修復(fù),因?yàn)槲椰F(xiàn)在只做了幾個(gè)月的 discord.js。我將向您展示有效的代碼和無(wú)效的代碼,在此先感謝您的幫助!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貢獻(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!


查看完整回答
反對(duì) 回復(fù) 2023-06-09
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)