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

Discord.js调用MessageEmbed报is not a constructor错误求助

问题根因与解决方案

你触发这个报错有两个核心原因,按优先级修改即可:

1. 主文件的Discord对象导入错误

你当前主文件中写的const Discord = require('./commands/discord');是错误的,该代码导入的是你自己commands目录下的命令文件,而非官方discord.js SDK,所以你传递给命令的Discord对象根本不存在MessageEmbed属性。
修改方案:
替换主文件中该行导入代码为:

const Discord = require('discord.js');

2. discord.js v14版本API变更

如果你使用的是discord.js v14及以上版本,原来的MessageEmbed已经被重命名为EmbedBuilder,且配套的方法、发送格式都有调整:

命令文件修改点:

  • 替换new Discord.MessageEmbed()为new Discord.EmbedBuilder()
  • setFooter方法传参改为对象格式
  • 发送嵌入时需要放到embeds数组中,不能直接传递实例
  • 本地图片需要先封装为附件才能嵌入
    修改后的命令文件代码示例:
module.exports = {
    name: 'command',
    description: "Embeds!",
    execute(message, args, Discord){
        // 加载本地图片为附件
        const ruleImage = new Discord.AttachmentBuilder('./images/rules.png', { name: 'rules.png' });
        const newEmbed = new Discord.EmbedBuilder()
        .setColor('#FFA62B')
        .setTitle('Rules')
        .setURL('https://discord.gg/fPAsvEey2k')
        .setDescription('**This is an embed for the server rules.**')
        .addFields(
            {name: '1.', value: 'Treat everyone with respect. Absolutely no harassment, witch hunting, sexism, racism or hate speech will be tolerated.'},
            {name: '2.', value: 'No spam or self-promotion (server invites, advertisements, etc.) without permission from a staff member. This includes DMing fellow members.'},
            {name: '3.', value: 'No NSFW or obscene content. This includes text, images or links featuring nudity, sex, hard violence or other graphically disturbing content.'},
            {name: '4.', value: 'if you see something against the rules or something that makes you feel unsafe, let staff know. We want this server to be a welcoming space!'},
            {name: '5.', value: 'Keep public conversations in English.'},
            {name: '6.', value: 'This list is not exhaustive and will be updated as we see fit.'}
        )
        .setImage('attachment://rules.png')
        .setFooter({ text: 'Make sure to follow the rules' });
        // 发送时指定附件和嵌入数组
        message.channel.send({ files: [ruleImage], embeds: [newEmbed] });
    }
}

额外兼容调整

discord.js v14已废弃message事件,如需正常接收消息,将主文件中的client.on('message', message =>{替换为client.on('messageCreate', message =>{即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 18:54:06