Skip to content
Advertisement

(Discord.js) TypeError: Cannot read property ‘add’ of undefined

So here’s the code:

const mutedRole = message.guild.roles.cache.find(role => role.name === "Muted");
        
        if (!mutedUser) return message.channel.send()
        if (!message.author) return;
   
        if (!mutedRole) {
            message.guild.roles.create({
                data: {
                  name: 'Muted',
                  color: '#6C877C',
                  permissions: ["CREATE_INSTANT_INVITE", "VIEW_CHANNEL"],
                },
              })
                .catch(console.error);
        }
        mutedUser.roles.add(mutedRole).catch(console.error);

When I run the bot, I get the error:

2020-08-14T05:22:08.731213+00:00 app[Worker.1]: TypeError: Cannot read property 'add' of undefined
2020-08-14T05:22:08.731236+00:00 app[Worker.1]:     at Object.execute (/app/commands/mute.js:69:29)
2020-08-14T05:22:08.731236+00:00 app[Worker.1]:     at Client.<anonymous> (/app/bread.js:111:14)
2020-08-14T05:22:08.731238+00:00 app[Worker.1]:     at Client.emit (events.js:315:20)
2020-08-14T05:22:08.731239+00:00 app[Worker.1]:     at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
2020-08-14T05:22:08.731241+00:00 app[Worker.1]:     at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
2020-08-14T05:22:08.731242+00:00 app[Worker.1]:     at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
2020-08-14T05:22:08.731242+00:00 app[Worker.1]:     at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
2020-08-14T05:22:08.731242+00:00 app[Worker.1]:     at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
2020-08-14T05:22:08.731243+00:00 app[Worker.1]:     at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:125:16)
2020-08-14T05:22:08.731244+00:00 app[Worker.1]:     at WebSocket.emit (events.js:315:20)

As seen on the code, I add the part where the bot would make a role as when the role is unavailable. But still, it thinks as that the role never exist.

Before this was copied, I aslo wrote the code like this:

            if (!mutedRole) {
                guild.roles.create({
                    data: {
                      name: 'Muted',
                      color: '#6C877C',
                      permissions: ["CREATE_INSTANT_INVITE", "VIEW_CHANNEL"],
                    },
                  })
                    .catch(console.error);
            }

And the result is still the same. I also put the bot’s role on the most top list of all roles on my test server. Can anyone find out why?

I’m currently using discord.js v12 and stable version of node.

Note: full code here: https://github.com/centralomd/breadbot/blob/master/commands/mute.js

Advertisement

Answer

The error comes from line 14:

const mutedUser = message.mentions.users.first() || message.guild.members.get(args[0])

If there are no mentions in the message.mentions.users Collection, you are trying to get a GuildMember using message.guild.members.get().

Discord JS v12 introduces the concept of managers, you will no longer be able to directly use Collection methods such as Collection.get() on data structures like Client.users and Guild.members.

You’ll have to change it with:

const mutedUser = message.mentions.users.first() || message.guild.members.cache.get(args[0])
if (!mutedUser) return message.channel.send("Invalid user.");

Note: message.mentions.users will get users in other guilds as well. I recommend you to use message.mentions.members.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement