'How can I get the length of an evaluated command in discord.js?

So I wanted my Bot to send a File whenever the output of the executed evaluation exceeds 4096 Characters (since this is the current Limit of Descriptions in Discord), however, everytime I've tried to let the Bot send the File in Question, it always errors with the following:

DiscordAPIError: Invalid Form Body
embeds: Embed size exceeds maximum size of 6000

The Code is the following:

const Discord = require('discord.js');
const { inspect } = require('util');

module.exports = {
    name: 'eval',
    description: 'Evaluates JavaScript Code.',
    usage: 'ds!eval <code>',
    run: async(client, msg, args) => {
        try{
            const command = args.join(' ');
            if (msg.author.id !== '705557092802625576') {
                msg.channel.send('You are not the Bot Owner!');
                return;
            }
            if (args.length < 1) {
                msg.channel.send('You need to provide some code!');
                return;
            }
            if (command.includes('client.token') || command.includes('process.env.TOKEN')) {
                msg.channel.send('You cannot execute commands containing .token!');
                return;
            }
                    const evaled = eval(command);
                    const type = typeof evaled;
                    const typeCapitalized = type.charAt(0).toUpperCase() + type.slice(1);
                    // if the output exceeds 4096 characters, create a hastebin
                    if (eval(command).toLocaleString().length > 4096) {
                        const file = new Discord.MessageAttachment(`${type}_output.txt`, 'eval_output.txt');
                        return msg.reply(`Output is too large to send.`, {files: [file]});
                    }
                    const embed = new Discord.MessageEmbed()
                    .setColor('BLUE')
                    .setThumbnail(client.user.displayAvatarURL({dynamic: true}))
                    .setAuthor({ name: `${msg.author.tag}`, iconURL: msg.author.displayAvatarURL({dynamic: true})})
                    .setTitle('Evaluated Code')
                    .setDescription(`**Output:**\`\`\`js\n${inspect(evaled,{ depth : 2 })}\`\`\`\n**Input:** \`\`\`js\n${command}\n\`\`\``)
                    .addFields(
                        {
                        name: 'Type:',
                        value: `\`\`\`js\n${typeCapitalized}\n\`\`\``,
                        inline: false
                    },
                        {
                        name: "Time:",
                        value: `\`\`\`js\n${Date.now() - msg.createdTimestamp}ms\n\`\`\``,
                        inline: false
                    }
                    )
                    .setTimestamp();
                    return msg.reply({embeds: [embed]});
                } catch(error) {
            console.log(error);
            const embed = new Discord.MessageEmbed()
                    .setAuthor({ name: `${msg.author.tag}`, iconURL: msg.author.displayAvatarURL({dynamic: true})})
                    .setColor('RED')
                    .setTitle('Error ocurred!')
                    .setDescription(`\`\`\`js\n${error.stack}\n\`\`\``)
                    .setTimestamp();
                    return msg.reply({embeds: [embed]});
        }
    }
};


Solution 1:[1]

Your embed which you want to send is just to big as the error says: Embed size exceeds maximum size of 6000 Also keep in mind that the Title, the Author, the Description and all the Fields together can't exceed 6000 characters, because it's a Limit for every Embed not every Field / Description.

Solution 2:[2]

Alright I fixed the Code myself. This is the new one:

const Discord = require('discord.js');
const { inspect } = require('util');
const Buffer = require('buffer').Buffer;

module.exports = {
    name: 'eval',
    description: 'Evaluates JavaScript Code.',
    usage: 'ds!eval <code>',
    run: async (client, msg, args) => {
        try {
            const command = args.join(' ');
            if (msg.author.id !== '705557092802625576') {
                msg.channel.send('You are not the Bot Owner!');
                return;
            }
            if (args.length < 1) {
                msg.channel.send('You need to provide some code!');
                return;
            }
            if (command.includes('.token') || command.includes('.TOKEN')) {
                msg.channel.send('You cannot execute commands containing .token!');
                return;
            }
            const evaled = eval(command);
            const type = typeof evaled;
            const typeCapitalized = type.charAt(0).toUpperCase() + type.slice(1);
            const embed = new Discord.MessageEmbed()
            embed.setDescription(`**Output:**\`\`\`js\n${inspect(evaled, { depth: 2 })}\`\`\`\n**Input:** \`\`\`js\n${command}\n\`\`\``)
            if (embed.description.length > 4096) {
                embed.setColor('BLUE')
                embed.setThumbnail(client.user.displayAvatarURL({ dynamic: true }))
                embed.setAuthor({ name: `${msg.author.tag}`, iconURL: msg.author.displayAvatarURL({ dynamic: true }) })
                embed.setTitle('Evaluated Code')
                embed.setDescription(`**Input:** \`\`\`js\n${command}\n\`\`\``)
                embed.addFields(
                    { name: 'Type:', value: `\`\`\`js\n${typeCapitalized}\n\`\`\``, inline: false }, { name: "Time:", value: `\`\`\`js\n${Date.now() - msg.createdTimestamp}ms\n\`\`\``, inline: false })
                embed.setTimestamp()
                const file = Buffer.from(inspect(evaled, { depth: 2 }));
                return msg.reply({ embeds: [embed], files: [{ attachment: file, name: 'output.js' }] });
            } else {
                const embed2 = new Discord.MessageEmbed()
                    .setColor('BLUE')
                    .setThumbnail(client.user.displayAvatarURL({ dynamic: true }))
                    .setAuthor({ name: `${msg.author.tag}`, iconURL: msg.author.displayAvatarURL({ dynamic: true }) })
                    .setDescription(`**Output:**\`\`\`js\n${inspect(evaled, { depth: 2 })}\`\`\`\n**Input:** \`\`\`js\n${command}\n\`\`\``)
                    .setTitle('Evaluated Code')
                    .addFields(
                        { name: 'Type:', value: `\`\`\`js\n${typeCapitalized}\n\`\`\``, inline: false }, { name: "Time:", value: `\`\`\`js\n${Date.now() - msg.createdTimestamp}ms\n\`\`\``, inline: false })
                    .setTimestamp()
                return msg.reply({ embeds: [embed2] });
            }
        } catch (error) {
            console.log(error);
            const embed = new Discord.MessageEmbed()
                .setAuthor({ name: `${msg.author.tag}`, iconURL: msg.author.displayAvatarURL({ dynamic: true }) })
                .setColor('RED')
                .setTitle('Error ocurred!')
                .setDescription(`\`\`\`js\n${error.stack}\n\`\`\``)
                .setTimestamp();
            return msg.reply({ embeds: [embed] });
        }
    }
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FirephoenixX02
Solution 2 Itz-Toothless