Using async() functions in eval() – discord.js

  • Recently, I tried the eval command in my bot using ‘await’ but since await is valid in async functions, I made the following aeval command.
  • The problem here is, it only returns undefined for everything I eval.
const { Base } = require("../../../lib/base");
const { inspect } = require("util");

module.exports = new Base({
    name: "aeval",
    description: "Owner only command to evaluate an asynchronous javascript code",
    async execute({ client, message, args }) {
        let script = args.join(" ");
        let evaled;
        let hrDiff;
        try {
            const hrTime = process.hrtime();
            evaled = await eval(`(async() => {${script}})()`);
            hrDiff = process.hrtime(hrTime);
            evaled = inspect(evaled);
            await message.reply(
                `Executed in `${hrDiff[1] / 1000000}` ms.n```jsn${inspect(
                    evaled
                )}n````
            );
        } catch (error) {
            console.log(error);
            message.reply(`Error Occurred:n```jsn${error}n````);
        }
    },
});