I'm attempting to construct a react command that just reacts to a stated message using ID, with the emoji stated after the message, but the problem is that it fails to verify whether the message is valid, and I can't get it to check if the emoji is valid as well. Is there any way around this?
The code:
const { MessageEmbed } = require('discord.js')
module.exports = {
name: 'react',
category: 'Staff',
aliases: [],
description: 'Reacts to a message.',
usage: 'react <messageID> <emoji>',
userperms: [],
botperms: [],
run: async (client, message, args) => {
if (message.author.bot) return;
let emoji = args[1];
let messageID = args[0];
if (!messageID) return message.reply('Please state the messageID!')
if (!emoji) return message.reply('Please state the emoji')
if (messageID && emoji) {
/*try {
message.channel.messages.fetch(messageID).then(m => m.react(emoji));
message.channel.send('Reacted!')
} catch {
message.channel.send('This message is not valid or it isn\'t in the same channel!')
}*/
try {
console.log('s')
await Promise.all([message.channel.messages.fetch(_messageID)])
} catch (error) {
if (error.code == 10008) {
message.channel.send('Failed to find the message!');
return
}
}
try {
message.channel.messages.fetch(_messageID).react(emoji)
} catch (error) {
message.channel.send('Please use a valid emoji!')
}
}
}
}
So I try your code in my bot. And yes it doesn't work with the try function. What you can do is to catch the code instantly when it have the error.
Here is the example (with a little bit improvement. You can remove some)
if (messageID && emoji) {
// your commented code
message.channel.messages.fetch(messageID)
.catch(err => {
// The message is not found.
if (err.code === 10008){
message.channel.send('Failed to find the message!');
return;
}
// The ID that entered is not a Message ID (Basically not a number)
if (err.code === 50035){
message.channel.send('You enter a Invalid type of Message ID!');
return;
}
});
}
Edit : For the emoji one. It will return <pending> because it try to react message that still not fetched yet. Sorry, my bad. This should work as I have try it now :
message.channel.messages.fetch(messageID)
.then(msg => {
msg.react(emoji)
.catch(err => {
message.channel.send('Please use a valid emoji!');
return;
}
});
I don't know exactly why this happen. I think it because the code already break before the catch function called (?)
Promise.all and await will also works.
await Promise.all([message.channel.messages.fetch(messageID)]).catch(...)
Tell me if its works or not now.