i have a little Problem with the discord.js messageReactionAdd event. So i read that this event only triggers to feteched messeages but i need to trigger this event also to older messages. So i tried it with raw events but this didnt worked out so i used partials.
But this dont work out either so can anybody help me with this?
There is like no error message or something like that. Just nothing happend
const { Console } = require("console")
const { Client, SystemChannelFlags, Guild, Message } = require("discord.js")
const { MessageEmbed } = require("discord.js")
const client = new Client({intents: ['GUILDS','DIRECT_MESSAGES', 'DIRECT_MESSAGE_TYPING', 'GUILD_MESSAGE_REACTIONS']}, { partials: ['MESSAGE', 'CHANNEL', 'REACTION'] })
const prefix = "!"
var fs = require('fs')
client.once("ready", () => {
console.log(`Ready! ${client.user.tag} ist jetzt Bereit...`)
client.user.setActivity({name: "!info - For more Information!", type: "PLAYING"})
client.on('messageReactionAdd', async (reaction, user, channel) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
return;
}
}
if(reaction.message.channel.id == channelID) {
if(reaction.emoji.name === '✅' ) {
console.log(`[${hours}:${minutes}:${seconds}]${user.tag}: ist nun online!`)
const role = reaction.message.guild.roles.cache.find(r => r.id === '889124672875159582')
const roleremove = reaction.message.guild.roles.cache.find(r => r.id === '889124737958170634')
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id)
member.roles.add(role)
member.roles.remove(roleremove)
}
}
You need to fetch the messages once the ready event is getting fired. You can do this by saving the id's of the messages and the id's of the channels where the messages are sent. Once the ready event is being fired you'll need to fetch the messages in the channels where they are sent. So for example you have saved this json file with the message id's and channel id's:
[{
"messageid": "1234567890",
"channelid": "1234567890"
}, {
"messageid": "0987654321",
"channelid": "1234567890"
}]
So for example you want to fetch the messages with the id 1234567890 and 0987654321. Then what you'll need to do is import this json file in your main file and go through all the data. Here is an example:
const data = require('./path-to-your-json-file.json');
const { Client } = require('discord.js');
const client = new Client(); // Add here your intents etc.
const wait = (ms) => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}; // A wait function to make sure that you won't get a rate-limit while fetching the messages
client.once('ready', async () => {
for(var i = 0; i < data.length; i++){
const messageInfo = data[i];
var channel = client.channels.cache.get(messageInfo.channelid);
if(!channel) return;
await wait(200);
try{
await channel.messages.fetch(messageInfo.messageid);
} catch(error){
console.log(`Error while fetching message with id ${messageInfo.messageid}`, error);
}
}
});
Now you've fetched all the messages and these messages should be able to detect a reaction on these messages.