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

如何在discord.js中发送存放在其他文件内的embed消息?

解决方案

报错原因

  • 你原有代码中存在非法JS语法:new MessageEmbed()后直接写的my embed不属于合法的方法调用,会导致embed实例构造失败
  • 时间戳方法拼写错误:正确写法为setTimestamp()(首字母T大写),你的小写写法属于无效调用
  • embed实例完全没有配置title/description/fields等任何内容,为空embed
  • 若你使用discord.js v13及以上版本,直接传递embed实例给send()方法的写法已经废弃,会被识别为空消息

调整步骤

1. 编写help.js文件,导出你需要的embed实例

// help.js 内容,可根据需求自定义embed的各项配置
const { MessageEmbed } = require("discord.js");

const customEmbed = new MessageEmbed()
  .setTitle("自定义标题")
  .setDescription("这里是embed的描述内容,至少需要配置一项内容才不会为空")
  .setColor("#2ecc71")
  .addField("命令使用说明", "输入!say即可触发本条消息")
  .setTimestamp();

module.exports = customEmbed;

2. 修改你的say命令文件代码

const { Client, Message } = require("discord.js");
// 路径根据你的实际文件结构调整,若help.js与当前文件同目录则为./help.js
const customEmbed = require("./help.js");

module.exports = {
  name: "say",
  execute: async (client, message, args) => {
    // discord.js v13及以上版本写法
    await message.channel.send({
      embeds: [customEmbed]
    });
    // 若使用v12及更早版本,替换为下面的写法即可
    // await message.channel.send(customEmbed);
  }
};

注意事项

  • 确保require引入help.js的路径和你实际的文件存储位置一致
  • embed实例至少要配置title、description、fields中的任意一项内容,完全空白的embed无法发送
  • 调用异步方法send()时建议加await,同时可以添加try/catch捕获异常方便后续排查问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 05:54:01