赠品命令抛出 无法读取未定义的属性'cache'



我试着为我的机器人做一个赠品命令,但它抛出一个错误。我正在努力解决这个问题,但我不知道是什么问题。

var embedSent = new Discord.MessageEmbed()
.setTitle("Giveaway Ended!")
.setDescription(`React with :tada: to enter the giveaway!nHosted By: **${message.author}**nTime: **${time}**nPrize: **${prize}**`)
.setTimestamp(Date.now + ms(args[1]))
.setColor(3447003)
channel.send(embed).then(newMessage => {
newMessage.react('🎉')
}).catch(e => {
message.channel.send(`An error occurred while running the command You shouldnt ever receive an error like this Please contact <@390690088348024843> in this server  **${item}** : ` + "`" + e + "`")
return;
})
setTimeout(async() => {
try {
const peopleReactedBot = embedSent.reactions.cache.get("🎉").users.fetch();
var peopleReacted = peopleReactedBot.array().filter(u => u.id !== client.user.id);
} catch (e) {
return message.channel.send(`An unknown error happened during th draw of the giveaway **${item}** : ` + "`" + e + "`")
}

抛出以下错误:

An unknown error happened during th draw of the giveaway test : TypeError: Cannot read property 'cache' of undefined

embedSent只是嵌入在消息中,它不是您可以响应的发送消息。一旦它被发送,返回值(newMessage)是你可以得到的反应。您正确地使用了newMessage.react,但您也应该使用newMessage.reactions而不是embedSent.reactions,因为embedSent仍然只是一个嵌入,您可以在消息中发送。

要访问setTimeout中的newMessage,您要么需要将其移动到.then()方法中,要么可以将消息await

另外,fetch()返回一个承诺,所以不要忘记在users.fetch()之前使用await。看看下面的代码片段如何组织你的代码:

const embed = new MessageEmbed()
.setTitle('Giveaway Ended!')
.setDescription(
`React with :tada: to enter the giveaway!nHosted By: **${message.author}**nTime: **${time}**nPrize: **${prize}**`
)
.setTimestamp(Date.now + ms(args[1]))
.setColor(3447003);
try {
const sent = await message.channel.send(embed);
sent.react('🎉');
setTimeout(async () => {
try {
const peopleReactedBot = await sent.reactions.cache
.get('🎉')
.users.fetch();
const peopleReacted = peopleReactedBot
.array()
.filter((u) => u.id !== client.user.id);
console.log({ peopleReacted });
} catch (e) {
message.channel.send(
`An unknown error happened during the draw of the giveaway **${item}**: `${e}``
);
}
}, timeout);
} catch (e) {
message.channel.send(
`An error occurred while running the command You shouldn't ever receive an error like this Please contact <@390690088348024843> in this server  **${item}**: `${e}``
);
}

你应该看看反应收集器。它可以很好地工作与赠送命令。discordjs.guide.

中有一个基本的收集器示例。

最新更新