【问题标题】:Using async() functions in eval() - discord.js在 eval() 中使用 async() 函数 - discord.js
【发布时间】:2022-01-23 11:34:06
【问题描述】:
  • 最近,我在我的机器人中使用 'await' 尝试了 eval 命令,但由于 await 在异步函数中有效,我创建了以下 aeval 命令。
  • 这里的问题是,它只为我评估的所有内容返回 undefined
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\`\`\``);
        }
    },
});

【问题讨论】:

  • 脚本的例子是什么?
  • await message.member.fetch()

标签: javascript discord.js


【解决方案1】:

你可以这样试试:

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

这会将函数移到 eval 之外,以便您可以在异步上下文中成功调用它。

请注意,使用eval 是个坏主意,它允许对您的服务器进行无限控制,包括擦除数据库或删除机器人本身。请非常小心,并考虑使用其他选项来执行您需要的服务器管理。

【讨论】:

  • 好吧我试试
  • 是的,我会记住的!
猜你喜欢
  • 2019-10-04
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
相关资源
最近更新 更多