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

Discord.js中TypeError: MessageEmbed is not a constructor问题求助

解决Discord.js中MessageEmbed is not a constructor错误

错误原因

你当前使用的是Discord.js v14及以上版本,该版本中**MessageEmbed已被重命名为EmbedBuilder**,且导出路径调整,继续使用旧的MessageEmbed会导致找不到构造函数的错误。

修复步骤

  1. 修正导入语句:将MessageEmbed替换为EmbedBuilder,从@discordjs/builders或discord.js的对应模块引入
  2. 替换构造函数调用:把代码里所有new MessageEmbed()改为new EmbedBuilder()
  3. 适配v14 API变更:setFooter方法需传入包含text属性的对象,不再支持直接传字符串

修正后的完整代码

const { SlashCommandBuilder, EmbedBuilder } = require('@discordjs/builders');
// 也可使用:const { EmbedBuilder } = require('discord.js');

const normalMelonValue = 9600;
const normalPineappleValue = 11520;
const LargeMelonValue = 27000;
const LargePineappleValue = 31050;

module.exports = {
    data: new SlashCommandBuilder()
        .setName('crate')
        .setDescription('Crate Calculator')
        .addStringOption(option =>
            option.setName('type')
                .setDescription('The Type of the crate')
                .setRequired(true)
                // 优化:一次性传入多个选项,无需多次调用addChoices
                .addChoices(
                    {name: "Normal", value: "normal"},
                    {name: "Large", value: "large"}
                )
        )
        .addStringOption(option =>
            option.setName('item')
                .setDescription('The item to calculate')
                .setRequired(true)
                .addChoices(
                    {name: "Melon", value: "melon"},
                    {name: "Pineapple", value: "pineapple"}
                )
        )
        .addIntegerOption(option =>
            option.setName('amount')
                .setDescription('The amount of the item to calculate')
                .setRequired(true),
        ),
    async execute(interaction) {
        const crateSize = interaction.options.getString('type');
        const item = interaction.options.getString('item');
        const amount = interaction.options.getInteger('amount');

        let totalValue = 0;
        if (crateSize === 'normal') {
            totalValue = item === 'melon' ? normalMelonValue * amount : normalPineappleValue * amount;
        } else if (crateSize === 'large') {
            totalValue = item === 'melon' ? LargeMelonValue * amount : LargePineappleValue * amount;
        }

        const embed = new EmbedBuilder()
            .setColor('#0099ff')
            .setTitle(`Crate Calculated - ${crateSize} ${item} crate`)
            .addFields(
                { name: 'Amount', value: `${amount} ${item} crates`, inline: true},
                { name: 'Total Value', value: `$${totalValue}`, inline: true},
            )
            .setTimestamp()
            .setFooter({ text: 'calculated successfully' })

        await interaction.reply({ embeds: [embed]})
    },
};

额外优化说明

  • 使用三元表达式简化了价值计算的嵌套if判断
  • 合并了addChoices的多次调用,代码更简洁

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 00:32:29