message on reaction add event v14.3.0

client.rest.on(Events.MessageReactionAdd, async (reaction, user) => { Hello, is anyone knows how i can get the reaction that are added from a specific message in 14.3.0 ?
24 Replies
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs•3y ago
Suggestion for @hugo4774:guide Popular Topics: Reaction collectors read more Suggestion for @hugo4774:guide Popular Topics: Awaiting reactions read moreguide Popular Topics: Listening for reactions on old messages read more
Squid
Squid•3y ago
1) You don't need to go through .rest; just client.on(...) is correct for almost every event 2) Compare reaction.message.id to the id of the specific message 3) reaction.emoji is the emoji that was added
Hugo4774
Hugo4774OP•3y ago
that's what i've done and i'm getting
client.rest.on(Events.MessageReactionAdd, async (reaction, user) => {
^

TypeError: Cannot read properties of undefined (reading 'on')
client.rest.on(Events.MessageReactionAdd, async (reaction, user) => {
^

TypeError: Cannot read properties of undefined (reading 'on')
deminearchiver
deminearchiver•3y ago
From the guide:
const { Client, Events, GatewayIntentBits, Partials } = require('discord.js');

const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});

client.on(Events.MessageReactionAdd, async (reaction, user) => {
// When a reaction is received, check if the structure is partial
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}

// Now the message has been cached and is fully available
console.log(`${reaction.message.author}'s message "${reaction.message.content}" gained a reaction!`);
// The reaction is now also fully available and the properties will be reflected accurately:
console.log(`${reaction.count} user(s) have given the same reaction to this message!`);
});
const { Client, Events, GatewayIntentBits, Partials } = require('discord.js');

const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});

client.on(Events.MessageReactionAdd, async (reaction, user) => {
// When a reaction is received, check if the structure is partial
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message:', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}

// Now the message has been cached and is fully available
console.log(`${reaction.message.author}'s message "${reaction.message.content}" gained a reaction!`);
// The reaction is now also fully available and the properties will be reflected accurately:
console.log(`${reaction.count} user(s) have given the same reaction to this message!`);
});
Hugo4774
Hugo4774OP•3y ago
well, i logged client and got that :
{
BaseClient: [class BaseClient extends EventEmitter],
Client: [class Client extends BaseClient],
Shard: [class Shard extends EventEmitter],
ShardClientUtil: [class ShardClientUtil],
ShardingManager: [class ShardingManager extends EventEmitter],
WebhookClient: [class WebhookClient extends BaseClient],`
...
{
BaseClient: [class BaseClient extends EventEmitter],
Client: [class Client extends BaseClient],
Shard: [class Shard extends EventEmitter],
ShardClientUtil: [class ShardClientUtil],
ShardingManager: [class ShardingManager extends EventEmitter],
WebhookClient: [class WebhookClient extends BaseClient],`
...
Squid
Squid•3y ago
That looks like all of discord.js' exports, not a Client instance
Hugo4774
Hugo4774OP•3y ago
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
`
client.on('messageCreate', message => {
...

command.execute(message, args, Discord, client);
...
});
client.on('messageCreate', message => {
...

command.execute(message, args, Discord, client);
...
});
index.js and then my command i go in the execute function nope
Squid
Squid•3y ago
The messageReactionAdd event isn't a command though...
Hugo4774
Hugo4774OP•3y ago
or maybe is it this ?
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { Client, GatewayIntentBits, Partials } = require('discord.js');
yes he's in the execute of a command 🤨 šŸ˜• why ?
Squid
Squid•3y ago
the first time you create the command, you make one messageReactionAdd event listener 1 total the second time you create the command, you make one messageReactionAdd event listener 2 total etc.
d.js docs
d.js docs•3y ago
guide Popular Topics: Reaction collectors read more
Squid
Squid•3y ago
These are designed for temporary use, and you can create them to only listen on specific messages
Hugo4774
Hugo4774OP•3y ago
so i should pass with a collector of reaction on my specific message ?
Squid
Squid•3y ago
what do you mean by "pass"
Hugo4774
Hugo4774OP•3y ago
i should go and change my code for...
Squid
Squid•3y ago
That's what I would do since you're not using a persistent database for each message that you care about
Hugo4774
Hugo4774OP•3y ago
but i don't understand the utility of Events.MessageReactionAdd then, in which case should we use it ? yes
Squid
Squid•3y ago
If you care about listening to reactions as soon as the bot starts, use the client event If you care about listening to reactions when a command is used, use collectors
Hugo4774
Hugo4774OP•3y ago
in my case it's when i do my command, i send a message with reactions, i wait for a certain time to catch all reactions to then do something so it's case 2 ?
Squid
Squid•3y ago
Correct Use the collector's end event whose first parameter is a Collection of each reaction added since the collector was created
Hugo4774
Hugo4774OP•3y ago
end is when the filter is "true" ? mmh and what's interesting is that in the filter i can check roles of user who react to collect it or no ?
Squid
Squid•3y ago
yep
Hugo4774
Hugo4774OP•3y ago
I have this filter condition, i enter to my collector.on() when i react to A, but when i react to B, i still enter to my collector.on()
const filterReactionCollector = (reaction, user) => {
return [emojiA, emojiB, emojiC, emojiD].includes(reaction._emoji.id)
&& usersReaction[user] == undefined
&& reaction.message.channel.id == channel;
};

const collector = theMessage.createReactionCollector({ filterReactionCollector, time: time1 * 1000 });

collector.on('collect', (reaction, user) => {
console.log([emojiA, emojiB, emojiC, emojiD].includes(reaction._emoji.id)
&& usersReaction[user] == undefined
&& reaction.message.channel.id == channel);
const filterReactionCollector = (reaction, user) => {
return [emojiA, emojiB, emojiC, emojiD].includes(reaction._emoji.id)
&& usersReaction[user] == undefined
&& reaction.message.channel.id == channel;
};

const collector = theMessage.createReactionCollector({ filterReactionCollector, time: time1 * 1000 });

collector.on('collect', (reaction, user) => {
console.log([emojiA, emojiB, emojiC, emojiD].includes(reaction._emoji.id)
&& usersReaction[user] == undefined
&& reaction.message.channel.id == channel);
Does anyone knows what happens in my filter ? it logs true then false but still enter in it šŸ¤” i must rename filterReactionCollector to filter ? that's a good answer ! ty !

Did you find this page helpful?