I am attempting to make a simple starboard system on discord.js, and the deleting a message part is stumping me. Currently, if a message gets a star, an embed is created in a starboard channel, and, critically, the footer is the original message's id. I want to make the inverse function (when the star is removed, the embed is deleted) work by finding the embed with that footer and then deleting that embed, but it's not working for some reason. The error I keep getting is "UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined." How do I property define this variable?
if (reaction.emoji.name === `⭐`) {
const guild = reaction.message.guild;
await reaction.fetch();
const stars = reaction.count;
if (stars === 0) {
const embed = guild.messageEmbed.cache.find(embed => embed.footer === reaction.message.id);
embed.delete
}
}
}
You have to find the embed from the starboard channel, not from the entire guild. You can use Channel.messages.fetch
and Channel.messages.cache.find
to find a specific message. Try this:
if (reaction.emoji.name === `⭐`) {
const guild = reaction.message.guild;
await reaction.fetch();
const stars = reaction.count;
if (stars === 0) {
guild.channels.cache.get('starboard-channel-id').messages.fetch().then(fetchedMsgs => {
fetchedMsgs.find(x => x.embeds[0].footer.text == reaction.message.id).delete();
})
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments