Search code examples
javascriptdiscorddiscord.jseval

Eval Slash Command


I'm trying to create an eval command using slash command but this command keep showing the error. I believe there's a v12 code that I use in it.

sorry if my code are mess up. Still new in discord.js.

Here's my code



const Discord = module.require("discord.js");

module.exports = {
  name: "eval",
  description: "Execute a JavaScript Code (owmer only)",
  options: [
      {
          name: "code",
          description: "The question you want to ask the magic 8ball",
          type: 'STRING',
      }
  ],
  run: async (client, interaction, args) => {
            function clean(text) {
            if (typeof text === "string")
                return text
                    .replace(/`/g, "`" + String.fromCharCode(8203))
                    .replace(/@/g, "@" + String.fromCharCode(8203));
            else return text;
        }
        let owner = '833843608905842700'

        if (!owner.includes(interaction.author.id)) return;

        try {
            const code = args.join(" ");
            let evaled = eval(code);

            if (typeof evaled !== "string") evaled = require("util").inspect(evaled);

            message.react("✅");
            var emb = new MessageEmbed()
                .setTitle('Result')
                .setDescription(`\`\`\`js` + '\n' + clean(evaled) + `\n` + `\`\`\``)
                .setFooter(client.user.username, client.user.displayAvatarURL({ dynamic: true }))
                .setColor(0xd26a0e)
            message.channel.send(emb);
        } catch (err) {
            message.react("⚠");
            var emb = new MessageEmbed()
                .setTitle('Result')
                .setDescription(`\`\`\`js` + '\n' + clean(err) + `\n` + `\`\`\``)
                .setFooter(client.user.username, client.user.displayAvatarURL({ dynamic: true }))
                .setColor(0xd26a0e)
            await interaction.reply({ embeds: [emb] })

        }
  }};
  

Solution

  • Hi here's your eval command code for discord.js v13 slash command.

        const {
        MessageEmbed
        } = require("discord.js");
        const {
        inspect
        } = require("util");
    
        module.exports = {
            name: "eval",
            description: "owner's only",
            options: [{
                name: "code",
                description: "type a code to execute",
                type: 3,
                required: true
        }],
        run: async (client, interaction, args) => {
            const nembed = new MessageEmbed()
                .setTitle("EVAL").setColor("RANDOM")
                .setDescription("❌ You dont have perms to use this command. Only Owner's can use this command")
                .setThumbnail(interaction.member.user.displayAvatarURL())
                .setFooter(interaction.member.user.tag)
    
            if (!client.config.owner.includes(interaction.member.user.id)) return interaction.followUp({
                embeds: [nembed]
            });
    
            let toEval = interaction.options.getString("code")
    
    
            try {
                const embed = new MessageEmbed()
                    .setTitle("EVAL").setColor("RANDOM")
                    .setDescription("❌ Error: `Cannot evaluate nothing`")
                    .setThumbnail(interaction.member.user.displayAvatarURL())
                    .setFooter(interaction.member.user.tag)
                let evaluated = inspect(eval(toEval, {
                    depth: 0
                }))
                if (!toEval) return interaction.followUp({
                    embeds: [embed]
                });
                const embed1 = new MessageEmbed()
                    .setTitle("EVAL").setColor("RANDOM")
                    .setDescription("❌ Error: `Request is too long.`")
                    .setThumbnail(interaction.member.user.displayAvatarURL())
                    .setFooter(interaction.member.user.tag)
    
                if (evaluated.length > 1950) return interaction.followUp({
                    embeds: [embed1]
                });
                let hrDiff = process.hrtime(process.hrtime());
                const embed2 = new MessageEmbed()
                    .setTitle("EVAL").setColor("RANDOM")
                    .setDescription(`Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s` : ''}${hrDiff[1] / 1000000}ms.*\`\`\`javascript\n${evaluated}\n\`\`\``)
                    .setThumbnail(interaction.member.user.displayAvatarURL())
                    .setFooter(interaction.member.user.tag)
                interaction.followUp({
                    embeds: [embed2]
                })
            } catch (e) {
                interaction.followUp({
                    content: `An error occurred : \`${e.message}\``
                });
            }
    
        }
    }