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

Discord自定义embedjson命令报错:Cannot read property 'mentions' of undefined求助

问题排查与解决方案

错误原因

触发TypeError: Cannot read property 'mentions' of undefined的核心原因是代码中的message变量为undefined。这是因为你使用的WOKCommands v2及以上版本默认采用Slash命令(斜杠命令),其run方法的参数对象中不再包含message属性,而是interaction;如果是使用前缀消息命令,可能是初始化时未启用该功能,或参数解构错误。

针对性解决方案

方案一:继续使用前缀消息命令

  1. 确保WOKCommands初始化时开启消息命令支持:
new WOKCommands(client, {
  // 其他配置项
  messageCommand: {
    enabled: true,
    prefix: '!' // 替换为你的命令前缀
  }
})
  1. 修正run函数的参数结构(适配WOKCommands v2+的消息命令参数):
run: async ({ message, args }) => {
  // 获取目标频道
  const targetChannel = message.mentions.channels.first()
  if (!targetChannel) {
    await message.reply('请指定要发送嵌入的频道')
    return
  }

  args.shift()

  try {
    const json = JSON.parse(args.join(' '))
    const { text = '' } = json

    // 适配Discord.js v13+的发送格式
    await targetChannel.send({
      content: text,
      embeds: [json]
    })
  } catch (error) {
    await message.reply(`无效的JSON:${error.message}`)
  }
}

方案二:切换为Slash命令(Discord官方推荐)

Slash命令是Discord当前主推的交互方式,无需依赖前缀,且兼容性更好:

module.exports = {
  name: "embedjson",
  category: "info",
  description: "通过JSON数据发送嵌入消息",
  // 定义Slash命令参数
  options: [
    {
      name: 'channel',
      description: '要发送嵌入消息的频道',
      type: 'CHANNEL',
      required: true
    },
    {
      name: 'json',
      description: '嵌入消息的JSON数据',
      type: 'STRING',
      required: true
    }
  ],
  run: async ({ interaction }) => {
    // 获取目标频道
    const targetChannel = interaction.options.getChannel('channel')
    if (!targetChannel) {
      await interaction.reply('请指定要发送嵌入的频道')
      return
    }

    // 获取JSON数据
    const jsonStr = interaction.options.getString('json')
    try {
      const json = JSON.parse(jsonStr)
      const { text = '' } = json

      // 发送嵌入消息
      await targetChannel.send({
        content: text,
        embeds: [json]
      })
      await interaction.reply('嵌入消息已成功发送')
    } catch (error) {
      await interaction.reply(`无效的JSON:${error.message}`)
    }
  },
}

额外注意事项

  • Discord.js v13及以上版本已废弃embed字段,需改用embeds数组传递嵌入消息对象;
  • 若使用Slash命令,需确保WOKCommands初始化时配置了testServers(测试服务器ID),或已将命令全局同步。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 07:35:25