Search code examples
javascriptdiscorddiscord.js

TypeError: MessageEmbed is not a constructor


I have this code it calculates a crate then puts the total value in an embed

Here is the code:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = 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({name: "Normal", value: "normal"})
                .addChoices({name: "Large", value: "large"})
        )
        .addStringOption(option =>
            option.setName('item')
                .setDescription('The item to calculate')
                .setRequired(true)
                .addChoices({name: "Melon", value: "melon"})
                .addChoices({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') {
            if (item === 'melon') {
                totalValue = normalMelonValue * amount;
            } else if (item === 'pineapple') {
                totalValue = normalPineappleValue * amount;
            }
        } else if (crateSize === 'large') {
            if (item === 'melon') {
                totalValue = LargeMelonValue * amount;
            } else if (item === 'pineapple') {
                totalValue = LargePineappleValue * amount;
            }
        }

        const embed = new MessageEmbed()
            .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('calculated successfully')

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

every time I try to fix it when I execute the code I run to this error:

TypeError: MessageEmbed is not a constructor
    at Object.execute (C:\Users\Dell\Desktop\bot\commands\fun\crates.js:52:23)
    at Object.execute (C:\Users\Dell\Desktop\bot\events\interactionCreate.js:16:18)
    at Client.<anonymous> (C:\Users\Dell\Desktop\bot\index.js:35:44)
    at Client.emit (node:events:512:28)
    at InteractionCreateAction.handle (C:\Users\Dell\Desktop\bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (C:\Users\Dell\Desktop\bot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)

i tried fixing it and it wouldn't work i tried what i knew but i always run into this error can you help me? thanks in advance.


Solution

  • in the new discordjs MessageEmbed has been replaced by EmbedBuilder try it.