如何修复:discord.js中的“ TypeError:无法读取未定义的属性'tag'”错误

时间:2019-04-14 19:13:01

标签: javascript discord.js

我正在制作discord.js机器人,并在其中加入了排行榜命令。

    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});
}

但是当我运行命令时,我在命令行/日志中得到了它:


    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)

我已经将机器人程序从我的电脑转移到glitch.com,这似乎已经修复了一段时间,但现在问题又回来了。

2 个答案:

答案 0 :(得分:0)

该错误表示client.users.get(data.user)返回的是未定义的,因此不能有属性tag。大概这意味着数据中指定的用户不存在。

在直接尝试访问该属性之前,应添加一些防御性代码,以确保get()调用返回的内容不是未定义的内容。

例如

const user = client.users.get(data.user);
if (user && user.tag) {
  // code here
} else {
  // user does not exist..
}

答案 1 :(得分:0)

client.users.get(data.user)未定义。根据{{​​3}},client#users是...

  

已在任何位置缓存的所有User对象(按其ID映射)

因此,您的问题很可能是该漫游器尚未缓存用户。

解决方案:改用client.fetchUser(data.user.id)