【问题标题】:Error: Cannot read property 'songs' of undefined - discord.js错误:无法读取未定义的属性“歌曲”-discord.js
【发布时间】:2021-02-06 17:41:54
【问题描述】:

我对编码比较陌生,目前正在尝试制作一个简单的音乐机器人。高度要求的功能之一是每 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);

然后是出现上述错误的代码:

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()
 .setDescription(`Now Playing:\n**${song.title}**`)
 .setColor('#ff0000')
 .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
 );

message.channel.send(nowPlaying).then((message) => {
 var countdown = song.duration;

 const interval = setInterval(() => {
  const queue = message.client.queue.get(message.guild.id);
  const song = queue.songs[0];
  const seek =
   (queue.connection.dispatcher.streamTime -
    queue.connection.dispatcher.pausedTime) /
   1000;
  const left = song.duration - seek;

  let nowPlayingTest = new MessageEmbed()
   .setDescription(`Now Playing:\n**${song.title}**`)
   .setColor('#ff0000')
   .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 (countdown < 0) clearInterval(interval);

  message.edit(nowPlayingTest);
 }, 5000);
});

感谢您的帮助!

【问题讨论】:

  • 注意:我在我的主脚本中使用了一个错误处理程序,我也尝试在 try{} 语句中使用它。这是来自 module.exports 命令文件,只是为了让您知道。
  • queue 未定义。 message.client.queue Collection/Map 没有键为 message.guild.id 的元素

标签: javascript discord discord.js


【解决方案1】:

错误信息Cannot read property 'songs' of undefined表示.songs之前的变量未定义。在这种情况下,queue 未定义。

在有效的代码中,您可以正确处理它:在访问 .songs 之前检查 queue 是否未定义。

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!

【讨论】:

    猜你喜欢
    • 2021-07-10
    • 2020-10-01
    • 1970-01-01
    • 2017-07-06
    • 2021-04-19
    • 2021-07-27
    • 1970-01-01
    • 2022-06-10
    • 2021-06-27
    相关资源
    最近更新 更多