0

I'm making a discord.js bot and I've put a leaderboard command in it.

    const Discord = require("discord.js");
  // Get a filtered list (for this guild only), and convert to an array while we're at it.
  const filtered = client.points.filter( p => p.guild === message.guild.id ).array();

  // Sort it to get the top results... well... at the top. Y'know.
  const sorted = filtered.sort((a, b) => b.points - a.points);

  // Slice it, dice it, get the top 10 of it!
  const top10 = sorted.splice(0, 10);

  // Now shake it and show it! (as a nice embed, too!)
  const embed = new Discord.RichEmbed()
    .setTitle("Leaderboard")
    .setAuthor(client.user.username, client.user.avatarURL)
    .setDescription("Our top 10 points leaders!")
    .setColor(0xff0000);
  for(const data of top10) {
    embed.addField(client.users.get(data.user).tag, `${data.points} points (level ${data.level})`);
  }
  return message.channel.send({embed});
}

But when I run the command I get this in the command line/logs:


    embed.addField(client.users.get(data.user).tag, `${data.points} points (level ${data.level})`);

                                              ^


TypeError: Cannot read property 'tag' of undefined

at Object.exports.run (/app/commands/leaderboard.js:19:47)

at module.exports (/app/events/message.js:19:7)

    at emitOne (events.js:121:20)

    at Client.emit (events.js:211:7)

    at MessageCreateHandler.handle (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/discord.js/11.4.2/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)

    at WebSocketPacketManager.handle (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/discord.js/11.4.2/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:103:65)

    at WebSocketConnection.onPacket (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/discord.js/11.4.2/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)

    at WebSocketConnection.onMessage (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/discord.js/11.4.2/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)

    at WebSocket.onMessage (/rbd/pnpm-volume/461f582b-2c68-44a2-912c-88cd1fefb0fa/node_modules/.registry.npmjs.org/ws/4.1.0/node_modules/ws/lib/event-target.js:120:16)

    at emitOne (events.js:116:13)

I've transferred the bot from my pc to glitch.com which seemed to fix it for a while, but now the problem is back.

Shahmeer Naqvi
  • 102
  • 1
  • 7

2 Answers2

1

The error means client.users.get(data.user) is returning undefined, hence there cannot be a property tag on it. Presumably this means the user specified in data does not exist.

You should add some defensive code before directly trying to access the property to ensure that the get() call returns something rather than undefined.

For example

const user = client.users.get(data.user);
if (user && user.tag) {
  // code here
} else {
  // user does not exist..
}
Matt
  • 2,063
  • 1
  • 14
  • 35
  • Although it's good practice to make sure something is returned, using [`client#users`](https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=users) to find a user is not. It only contains previously cached users. The proper way to find a Discord user is [`client#fetchUser()`](https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=fetchUser). – slothiful Apr 14 '19 at 22:25
1

client.users.get(data.user) is undefined in your code. Straight from the Discord.js docs, Client.users is...

All of the User objects that have been cached at any point, mapped by their IDs

Instead of relying solely on the client's cache, you can retrieve the user from the Discord API using the Client.fetchUser() method. Keep in mind, it returns a Promise.

// async context required to use 'await'
await client.fetchUser(data.user);
slothiful
  • 5,548
  • 3
  • 12
  • 36