我对编码还比较陌生,目前正在尝试制作一个简单的音乐机器人。要求很高的功能之一是“现在播放”嵌入,每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);
});
感谢您的帮助!
答案 0 :(得分:1)
错误消息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!