I have array which I'm cycling through via reactions on an embed. When a reaction is pressed, the message should be deleted and the function loops again. At the second reaction press, I'm getting this error:
(node:12908) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message
At each reaction press after the first, the amount of this error printed is doubled.
Code:
function movieChoiceEmbed(movies, message, count)
{
pagecount = count;
message.channel.send({embed: {
color: '#D733FF',
title: 'Choose movie',
fields: [
{
name: movies[pagecount].title,
value: movies[pagecount].description,
inline: true,
},
{
name: movies[pagecount+1].title,
value: movies[pagecount+1].description,
inline: true,
},
{
name: '\u200b',
value: '\u200b',
inline: false,
},
{
name: movies[pagecount+2].title,
value: movies[pagecount+2].description,
inline: true,
},
{
name: movies[pagecount+3].title,
value: movies[pagecount+3].description,
inline: true,
},
],
timestamp: new Date(),
footer: {
icon_url: client.user.displayAvatarURL(),
text: 'Andrea Gafa'
}
}})
.then(sentEmbed => {
sentEmbed.react("⏪");
sentEmbed.react("⏩");
})
.catch();
client.on("messageReactionAdd", async (reaction, user) => { // When a reaction is added
if(user.bot) return;
if(reaction.emoji.name == "⏪")
{
await reaction.message.delete();
count -= 4;
movieChoiceEmbed(movies, message, count);
}else if(reaction.emoji.name == "⏩")
{
await reaction.message.delete();
count += 4;
movieChoiceEmbed(movies, message, count);
}
return;
});
}
I have tried deleting the last message instead with the same result:
await message.channel.lastMessage.delete();
Any clue? I'm pretty lost here.
Edit:
The messagereactionadd event being inside the function seems to be the problem. Putting it outside it fixes the issue.
your error is because you're getting a promise that isn't caught. You have several calls that are async and returning promises, one or more of them is causing an error. I would add catch blocks to them, and then this will help you isolate where your problem lies. For example:
await reaction.message.delete();
could be changed to:
await reaction.message.delete()
.then(() { /* do something if interested */ })
.catch(err => { console.error(err); });
add this to all your promised functions and you'll know where you're having issues.
Now as for the error itself, you're trying to delete a message, potentially, that has already been deleted. Did you want to delete a reaction, or a message? If you've deleted a message, and then try to do something with the message in the calls subsequently, then that's the problem.