0

As I showed before, this code is supposed to give an role to whoever reacts with the specified emoji, my trouble now, is to make the Reaction Collector to undestand who reacted and give him the role. I was looking at the guide, tried the solutions they give, but the first doesn't do what I needed, and the second don't bring the user, and I don't know how to fix it.

module.exports = {
    name: 'cargo',
    description: 'Give a role to an user by reaction',
    execute(message, args) {
        function getRoleFromMention(mention) {
            if (!mention)
                console.log('No role was passed');

            if (mention.startsWith('<@&') && mention.endsWith('>')) {
                mention = mention.slice(3, -1);
                return message.guild.roles.cache.get(mention);
            }
        }

        const messageId = args.slice(0, 1);
        const roleEmoji = args.slice(1, 2);
        const roleMention = args.slice(2, 3);
        const roleToGive = getRoleFromMention(`${roleMention}`);

        //Bot will react the specified message with the given emoji
        console.log('\x1b[35m%s\x1b[0m', 'messageId:', messageId, 'roleEmoji:', roleEmoji, 'roleMention', roleMention); // show each argument on terminal
        message.channel.messages.fetch(`${messageId}`).then(msg => msg.react(`${roleEmoji}`));

        //Reaction Collector filter
        const filter = (reaction, user) => { return reaction.emoji.name === `${roleEmoji}` /*&& user.id === message.author.id*/; };

        //Reaction Collector
        message.channel.messages.fetch(`${messageId}`)
            .then(msg => msg.awaitReactions(filter, { max: 4, time: 20000, errors: ['time'] })
                .then(collected => console.log(collected.size))
                .catch(collected => {
                    console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
                }));
    }
}
2
  • What version of Discord.js are you using? Aug 8, 2020 at 18:51
  • Currently using v12
    – SrAranha
    Aug 8, 2020 at 19:01

2 Answers 2

0

You can use Message.createReactionCollector() to collect the messages, and then get the member that reacted by comparing the ID of the user that reacted with member IDs.

//Reaction Collector
message.channel.messages.fetch(`${messageId}`)
    .then(msg => {
        const collector = msg.createReactionCollector((reaction, user) => !user.bot);

        const reactions = {
            '👍': 'arole',
            '👎': 'anotherrole'
        };

        collector.on('collect', (reaction, user) => {
            if (!reaction.emoji.name in reactions) return;

            const role = reaction.message.guild.roles.cache.find(role => role.name === reactions[reaction.emoji.name]);
            const member = reaction.message.guild.members.cache.find(member => member.id === user.id);

            member.roles.add(role);
        });
    });
6
  • TypeError: reaction.message.guild.members.find is not a function and crashes, I tried to switch to fetch and TypeError: Cannot read property 'add' of undefined at member.roles.add('arole')
    – SrAranha
    Aug 8, 2020 at 19:23
  • @SrAranha Oops, forgot cache. I have edited it now. Aug 8, 2020 at 19:25
  • Did the changes, but now it sends this UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not an Role, Snowflake or Array or Collection of Roles or Snowflakes., i've tried with the role id and the role name, same error twice
    – SrAranha
    Aug 8, 2020 at 19:34
  • @SrAranha Try using the role name now. Aug 8, 2020 at 19:39
  • Worked fine, I only changed member.roles.add(reaction.message.guild.roles.cache.find(role => role.name === 'arole')); to use the role mentioned when command is called, and continue working. Thanks a lot, I was trying to fix it the whole week.
    – SrAranha
    Aug 8, 2020 at 19:51
0

Look the others topics and you will have all you need. https://discordjs.guide/popular-topics/common-questions.html#how-do-i-add-a-role-to-a-guild-member

4
  • Ok but, how do I catch which member reacted?
    – SrAranha
    Aug 8, 2020 at 19:00
  • Look at the discord.js documentation : discord.js.org/#/docs/main/stable/class/… and see that there are two parameters in the reaction collector's event.
    – ations
    Aug 8, 2020 at 19:12
  • @ations Just want to mention that the User object that's returned is different to a GuildMember object, so you have to match the ID of the two objects if you want to get the member that the returned user object is for. Aug 8, 2020 at 19:18
  • Just use the function <Guild>.member(User).
    – ations
    Aug 9, 2020 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.