简体   繁体   English

如何解决:“TypeError:无法读取discord.js中未定义的属性'标签'错误

[英]How to fix: “TypeError: Cannot read property 'tag' of undefined” error in discord.js

I'm making a discord.js bot and I've put a leaderboard command in it. 我正在制作一个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});
}

But when I run the command I get this in the command line/logs: 但是当我运行命令时,我在命令行/ 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. 我已将机器人从我的电脑转移到glitch.com,这似乎已经解决了一段时间,但现在问题又回来了。

The error means client.users.get(data.user) is returning undefined, hence there cannot be a property tag on it. 该错误意味着client.users.get(data.user)返回undefined,因此不能有属性tag 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. 在直接尝试访问属性之前,您应该添加一些防御性代码,以确保get()调用返回的内容而不是undefined。

For example 例如

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

client.users.get(data.user) is undefined. client.users.get(data.user)未定义。 According to the Discord.js docs , client#users is... 根据Discord.js文档client#users是......

All of the User objects that have been cached at any point, mapped by their IDs 所有已在任何点缓存的用户对象,由其ID映射

So, your problem is likely that the bot hasn't cached the user yet. 所以,你的问题可能是机器人还没有缓存用户。

Solution: Use client.fetchUser(data.user.id) instead. 解决方案:改为使用client.fetchUser(data.user.id)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Discord.js 错误 - 类型错误:无法读取未定义的属性“执行” - Discord.js Error - TypeError: Cannot read property 'execute' of undefined discord.js错误TypeError:无法读取未定义的属性“ voiceChannel” - discord.js Error TypeError: Cannot read property 'voiceChannel' of undefined 类型错误:无法读取未定义的 Discord.js 错误的属性“get” - TypeError: Cannot read property 'get' of undefined Discord.js Error “TypeError:无法读取未定义的属性‘id’”Discord.js 错误 - "TypeError: Cannot read property 'id' of undefined" Discord.js error 如何修复“TypeError:无法读取未定义的属性 'toString'” | discord.js - How to fix “TypeError: Cannot read property 'toString' of undefined” | discord.js 如何解决discord.js中的“无法读取未定义的属性'反应'” - How to fix “Cannot read property 'react' of undefined” in discord.js 在 Discord.js 中获取带有用户 ID 的用户名 TypeError:无法读取未定义的属性“标签” - Getting username with user id in Discord.js TypeError: Cannot read property 'tag' of undefined 类型错误:无法读取未定义 Discord.js javascript 的属性“添加” - TypeError: Cannot read property 'add' of undefined Discord.js javascript 类型错误:无法读取未定义的属性 'has' // Discord.js - TypeError: Cannot read property 'has' of undefined // Discord.js 遇到 TypeError:无法读取 Discord.JS 中未定义的属性“0” - Encountering TypeError: Cannot read property '0' of undefined in Discord.JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM