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

如何读取并打印Discord embed中的author.name与description内容

你遇到的Uncaught ReferenceError: author is not defined报错,原因是代码中直接调用了未声明的author变量,你要获取的.setAuthor和.setDescription设置的内容,需要从对应的MessageEmbed实例对象上读取属性,按两种常见使用场景分别实现如下:

场景1:读取自己构造的embed属性

如果embed是你自己在当前代码中创建的实例,直接读取实例对应的属性即可:

  • 读取author名称:embed.author.name
  • 读取description内容:embed.description

示例代码:

const embed = new MessageEmbed()
    .setAuthor('Juan')
    .setColor('RANDOM')
    .setDescription("text")

// 读取属性
const authorName = embed.author.name
const description = embed.description

message.channel.send(`${authorName} !!!\n${description}`);
console.log(`${authorName} !!!\n${description}`);

场景2:读取频道接收消息中的embed属性

如果你是监听频道消息,需要读取其他用户/机器人发送的embed内容,要先从消息的embeds数组中获取embed实例:
示例代码:

// 消息监听事件示例
client.on('messageCreate', message => {
  // 先判断当前消息是否包含embed
  if (message.embeds.length < 1) return
  // 取消息携带的第一个embed(单条消息可携带多个embed)
  const targetEmbed = message.embeds[0]
  // 兼容无author/description的场景,避免报错
  const authorName = targetEmbed.author?.name ?? '无作者信息'
  const description = targetEmbed.description ?? '无描述内容'

  console.log('embed作者:', authorName)
  console.log('embed描述:', description)
})

注意:如果embed未设置author属性,直接访问embed.author.name会触发报错,建议使用可选链?.配合空值合并运算符??做兼容处理。

内容的提问来源于stack exchange,提问作者Andres Avendaño Aguilar

相关产品推荐
方舟 Agent Plan

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

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