Checking to make sure a message still exist (Unknown Message Errors)

How can I check if a message still exists after I've gotten the message? I keep getting Unknown Message errors because I'm trying to react to a message that no longer exists. The problem is that my bot thinks it does exist cause it has the message stored in its cache as a variable. What should I do? Is there a way to verify that the message still exists before I try to react 🤷‍♂️
6 Replies
d.js toolkit
d.js toolkit•14mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
sexy dark chocolate
sexy dark chocolate•14mo ago
Try to react to the message and catch the rejected. If you want, you can fetch the message in the catch to see if it's truly deleted
dzlandis
dzlandis•14mo ago
Well this won't resolve the problem because even if I do catch it, I'll still get an error because its a Discord API error. I was hoping more for a way to just stop the error from happening in the first place.
Tomáš
Tomáš•14mo ago
I think he meant that
try {
....
}
catch (err) {
....
}
try {
....
}
catch (err) {
....
}
the catch does't do anything in this case and it still throws new DiscordAPIError. I encountered this recently for similar problems, but I found other solutions I believe not applicable here, sorry.
sexy dark chocolate
sexy dark chocolate•14mo ago
Well, do you add await before it? A try catch block will catch rejected promises and errors
Tomáš
Tomáš•14mo ago
well, the best solution I got for those kinds of issues is
const someMessage = ...;
if ( (await someMessage.channel.messages.fetch({ limit: 1, cache: false, around: someMessage.id })).has(someMessage.id) ) someMessage.react(...);
const someMessage = ...;
if ( (await someMessage.channel.messages.fetch({ limit: 1, cache: false, around: someMessage.id })).has(someMessage.id) ) someMessage.react(...);
this fetches message from around the message you are trying to react to, and if it still exist, the collection contains the message so you can react. However, there can still be some timing edge cases where the fetched data will already be outdated which will cause errors like Qjuh said.