Discord.js - TypeError: message.guild.fetchMember is not a function

Viewed 4311

I have a little problem with my code. Here below you see the piece of code that gives me problems.

client.on('messageReactionAdd', (reaction, user) => {
  if(user.bot) return;
        let message = reaction.message, emoji = reaction.emoji;

        if (emoji.name == '✅') {
                message.guild.fetchMember(user.id).then(member => {
                        member.addRole('759729116931948573');
                });
        }

        else if (emoji.name === '') {
                message.guild.fetchMember(user.id).then(member => {
                        member.addRole('759768642580906026');
                });
        }

        // Remove the user's reaction
        reaction.remove(user);
});

When I execute my Discord bot and, in this case, react to a message, I get this error.

TypeError: message.guild.fetchMember is not a function
0|index  |     at Client.client.on (/home/pi/discordbot/index.js:52:31)
0|index  |     at Client.emit (events.js:193:13)
0|index  |     at MessageReactionAdd.handle (/home/pi/node_modules/discord.js/src/client/actions/MessageReactionAdd.js:46:17)
0|index  |     at Object.module.exports [as MESSAGE_REACTION_ADD] (/home/pi/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_REACTION_ADD.js:4:37)
0|index  |     at WebSocketManager.handlePacket (/home/pi/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
0|index  |     at WebSocketShard.onPacket (/home/pi/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
0|index  |     at WebSocketShard.onMessage (/home/pi/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
0|index  |     at WebSocket.onMessage (/home/pi/node_modules/discord.js/node_modules/ws/lib/event-target.js:125:16)
0|index  |     at WebSocket.emit (events.js:193:13)
0|index  |     at Receiver.receiverOnMessage (/home/pi/node_modules/discord.js/node_modules/ws/lib/websocket.js:797:20)

Now I remember: I did "sudo apt-get update" and "sudo apt-get upgrade", so probably it upgraded the version of node.js to v12. I don't remember which version I had (probably I installed node.js in early/mid-2019). I tried to install v11, but the same problem appears. Does anyone know in which version this line of code works?

P.S: Also message.member.roles.find gave me this error: message.member.roles.find is not a function, but I googled it and found a solution. I changed that line to message.member.roles.cache.some.

Thanks in advance.

1 Answers
Related