Reputation: 23
I'm trying to make discord bot to add a role to user who reacted to message in channel. That is my code.
`const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
client.once('ready', () => {
console.log("Готов!");
});
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.channel.id === '1030100801747701760') {
console.log("Сработала реакция1");
if (reaction.emoji.name === '😄') {
const member = reaction.options.getMember(user.id);
const role = reaction.options.getRole('1030138804830478387');
member.roles.add(role);
}
}
});`
TypeError Cannot read properties of undefined (reading 'getMember'). - Error
I am a total newbie in discord.js and I do not quite understand how to get user and use it to give him a role. Thanks in advance!
Upvotes: 1
Views: 1762
Reputation: 154
There is no options
on a MessageReaction
. That means that the function reaction.options.getMember()
will return an error.
To fix this problem you need to search for the Member in the guild:
reaction.message.guild.members.cache.get(user.id);
Also you can't use reaction.options.getRole()
to get a Role from the Guild. To fix this you need to get it from the Guild again with reaction.message.guild.roles.cache.get('1030138804830478387')
Working Code:
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
client.once('ready', () => {
console.log("Готов!");
});
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.channel.id === '1030100801747701760') {
console.log("Сработала реакция1");
if (reaction.emoji.name === '😄') {
const member = reaction.message.guild.members.cache.get(user.id);
const role = reaction.message.guild.roles.cache.get('1030138804830478387');
member.roles.add(role);
}
}
});
Upvotes: 1