- Recently, I tried the
eval
command in my bot using 'await' but since await is valid in async functions, I made the followingaeval
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\`\`\``);
}
},
});
CodePudding user response:
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.