Discord JS: TypeError: Cannot read property 'id' of undefined

Viewed 3099

I'm making an order command for my food delivery bot. An error in the console occures when the command is used in outside of the main server, otherwise the command works inside the main channel.

The error is TypeError: Cannot read property 'id' of undefined code is

api.commands.add("claim", (msg) => {
        let employeeRole = msg.guild.roles.get("745410836901789749");
        if(msg.member.roles.has(employeeRole.id)) {
            if(msg.channel.id == 746423099871985755) {
                        api.client.channels.get("746423099871985755").fetchMessages({
                            around: order.ticketChannelMessageID,
                            limit: 1
                        }).then(messages => {
                            const fetchedMsg = messages.first();

                            fetchedMsg.edit({embed: {
                                color: 0xFFFFFF,
                                title: api.client.users.get(order.userID).username,
                                fields: [{
                                    name: "Order Description",
                                    value: order,
                                }, {
                                    name: "Order ID",
                                    value: ticketID,
                                }, {
                                    name: "Order Status",
                                    value: "claimed",
                                }],
                                timestamp: new Date(),
                                footer: {
                                    text: "Taco Bot"
                                }
                            }}).then((m) => {
                                m = m.id;
        
                                // Update Status
                                delete orderDB[ticketID];

                                orderDB[ticketID] = {
                                    "orderID": order.orderID,
                                    "userID": order.userID,
                                    "guildID": order.guildID,
                                    "channelID": order.channelID,
                                    "order": order.order,
                                    "status": "Claimed",
                                    "ticketChannelMessageID": m,
                                    "chef": msg.author.id
                                };                          
2 Answers

the line bellow resolves to undefined, hence if you try to access a property on employeeRole you'll get that error.

let employeeRole = msg.guild.roles.get("745410836901789749");

In your main server you may have a role with id 745410836901789749, however in other servers the id of the role will be different. If you wish to check by role name, you should fetch all roles, iterate through them and find the one you need by name.

// Fetch all the roles as per discordjs docs
await message.guild.roles.fetch()

The line above should fetch you all the roles

The error probably occurs because you are trying to get the emploeeRole. Roles are unique. On other servers, there will not be a role with this id so it will return undefined.

To avoid this problem you should check if the command executor on this server has a role with this id. (probably no because already mentioned: roles are unique).

if (msg.member.roles.cache.find(r => r.id === '745410836901789749')) {
  // The user has the requierd role! Execute command
} else {
  // Role wasn't found! Do something else
}

The problem with this code is that the command will only work on your server. To get around that you can check the name of the role instead of the id.

 if (msg.member.roles.cache.find(r => r.id === 'MY ROLE NAME')) {
  // The user has the requierd role! Execute command
} else {
  // Role wasn't found! Do something else
}

Now only users with a role named MY ROLE NAME can execute the command. Of course, you can enter your own role name instead of using MY ROLE NAME. ~ヾ(・ω・)

Related