You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写Discord info命令时无法获取用户joinedAt属性值求助

你代码的问题出在公会成员实例的获取路径和调用方法不对,以下是具体问题和修复方案:

  • 取值路径错误:斜杠命令的交互对象不存在interaction.message属性,当前所在公会实例可直接通过interaction.guild获取
  • API方法已弃用:discord.js v13及更高版本中,Guild.member()方法已被移除,需使用异步方法Guild.members.fetch()传入用户ID拉取对应公会成员实例
  • 缺少异步等待:拉取公会成员属于网络请求操作,必须添加await关键字等待返回结果,否则拿到的是Promise对象无法读取属性
  • 缺少场景判断:若命令在私信频道触发,不存在公会实例,建议先加判断避免报错

修复后可正常运行的代码如下:

// 先判断命令是否在公会内触发
if (!interaction.inGuild()) return interaction.reply({content: '该命令仅可在服务器内使用', ephemeral: true})

const user = await interaction.options.getUser("user").fetch(true);
// 拉取对应公会成员实例
const guildMember = await interaction.guild.members.fetch(user.id);

embed.setTitle(`User info for ${user.username}`);
embed.addFields(
  { name: "用户标签:", value: `${user.tag}`, inline: true },
  { name: "用户ID:", value: `${user.id}`, inline: true },
  { name: "是否为机器人:", value: `${user.bot}`, inline: true },
  {
    name: "账号创建时间:",
    value: `${user.createdAt}`,
    inline: false
  },
  {
    name: "加入服务器时间:",
    value: `${guildMember.joinedAt}`,
    inline: false
  }
);

内容的提问来源于stack exchange,提问作者Koto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 20:36:03