Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

443
Views
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\`\`\`js\n${inspect(
                    evaled
                )}\n\`\`\``
            );
        } catch (error) {
            console.log(error);
            message.reply(`Error Occurred:\n\`\`\`js\n${error}\n\`\`\``);
        }
    },
});
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can try it like this:

evaled = await eval('async () => code')()

This moves the function outside of the eval so you can successfully call it in a async context.

Note that using eval is a bad idea and allows unlimited control over your server, including wiping the database or deleting the bot itself. Be very very careful, and consider using other options to do the server administration you require.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!