Skip to content
Advertisement

Discord user roles are undefined TypeError: Cannot read property ‘cache’ of undefined

I added this new command in my bot, when members execute the command ;;time, the bot goes through the roles and gives them the claim time. I have given my code along with the error. Any possible solution to fix the error?

module.exports = {
  name: 'time',
  description: 'claim time for a user',
  execute(message, args){
    
    let winner;

    if (message.mentions.members.first()) {
      winner = message.mentions.members.first();

    } else {
      winner = message.author;
    }
    
    var time = 0;

    const allowedRole = (winner.roles.cache.find(r => r.name === "・booster") || winner.roles.cache.find(r => r.name === "・mvp+・") || winner.roles.cache.find(r => r.name === "・donator") || winner.roles.cache.find(r => r.name === "・mvp・") || winner.roles.cache.find(r => r.name === "・supporter") || winner.roles.cache.find(r => r.name === "・vip+・") || winner.roles.cache.find(r => r.name === "・vip・"));

    if (!allowedRole) {
      message.channel.send('Your total claim time : **10 seconds**!')
      return;
    }

    switch (allowedRole.name) {
      
      case "・vip・":
      time = 12000;

      break;

      case "・vip+・":
      time = 14000;

      break;

      case "・supporter":
      time = 15000;

      break;

      case "・mvp・":
      time = 19000;

      break;

      case "・donator":
      time = 20000;

      break;

      case "・mvp+・":
      time = 22000;

      break;

      case "・booster":
      time = 900000;

      break;

    }
    if (time === 900000) {
      message.channel.send('Your total claim time is : **15 Minutes**!')
    } else {
    message.channel.send(`Your total claim time is : **${time / 1000} seconds**!`)
    }

  }
}

And here is the error :

/home/runner/New-Night/commands/second.js:15
    const allowedRole = (winner.roles.cache.find(r => r.name === "・booster") || winner.roles.cache.find(r => r.name === "・mvp+・") || winner.roles.cache.find(r => r.name === "・donator") || winner.roles.cache.find(r => r.name === "・mvp・") || winner.roles.cache.find(r => r.name === "・supporter") || winner.roles.cache.find(r => r.name === "・vip+・") || winner.roles.cache.find(r => r.name === "・vip・"));
                                      ^

TypeError: Cannot read property 'cache' of undefined
    at Object.execute (/home/runner/New-Night/commands/second.js:15:39)
    at Client.<anonymous> (/home/runner/New-Night/index.js:72:33)
    at Client.emit (events.js:314:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at MessageCreateAction.handle (/home/runner/New-Night/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/New-Night/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/New-Night/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/New-Night/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/New-Night/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/New-Night/node_modules/ws/lib/event-target.js:132:16)

I searched on google and stack overflow for answers but couldn’t find one for this situation. I tried changing the message.mentions.users.first() to message.mentions.members.first() but it showed the same error.

Advertisement

Answer

The problem is that message.author returns a User and only GuildMembers have roles. You should use message.member instead that returns the author of the message as a GuildMember, so you can use its roles property.

You could also simplify that if else statement using an OR operator. If there is no member mentioned the value of winner will be the author of the message:

execute(message, args) {
  const winner = message.mentions.members.first() || message.member;
  let time = 0;

  // ... 
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement