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

如何在discord.js交互中设置Embed作者为交互发起者?

问题解决:Embed作者区域显示交互发起用户

错误原因

你代码里的interaction.author是错误用法,在Discord.js的交互对象(比如Slash命令交互)中,发起交互的用户信息存储在interaction.user属性里,interaction.author不存在,因此会抛出Cannot read properties of undefined (reading 'username')错误。

修正方案

将interaction.author替换为interaction.user,另外EmbedBuilder.setAuthor支持传入包含用户名和头像的配置对象,能让作者区域显示更完整:

修正后的代码

if (interaction.options.getString("favourite-pet").toLowerCase() === 'dog') {
  const embed = new EmbedBuilder()
    .setTitle('favourite pet')
    .setDescription('my favourite pet is also a dog')
    .setColor('Orange')
    .setAuthor({
      name: interaction.user.username,
      iconURL: interaction.user.displayAvatarURL() // 可选:显示用户头像
    });
  // 记得发送这个embed,示例:
  await interaction.reply({ embeds: [embed] });
}

补充说明

  • 如果仅需显示用户名,也可以简化为.setAuthor(interaction.user.username),但推荐使用对象参数的方式,视觉体验更完整
  • 不要遗漏发送Embed的代码(如interaction.reply),否则Embed不会被实际发送

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 08:29:50