为了账号安全,请及时绑定邮箱和手机立即绑定

我怎样才能使它成为异步函数或承诺?

我怎样才能使它成为异步函数或承诺?

杨魅力 2023-07-06 19:55:32
我正在尝试制作一个不和谐机器人。我使用 AkairoClient 作为框架,其中有一个设置前缀的选项。我有以下代码:            // HANDLERS        this.commandHandler = new CommandHandler(this, {            prefix: msg => {                let prefix;                console.log('first')                con.query(`SELECT * FROM info WHERE id = ${msg.guild.id}`, (err, rows) => {                    if (!err) prefix = rows[0].prefix;                    console.log('Second')                                });                console.log('third')                return prefix ?? '!';            },            blockBots: true,/* Rest of code here ...*/        });当我执行此操作时,控制台打印:第一的第三第一的第三第二第二我在理解如何使其正常工作时遇到问题,因为我希望prefix获取行的值,但在本例中prefix返回的是undefined查询完成之前的结果
查看完整描述

1 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

我不完全确定你的模块是如何工作的,但这将是异步等待函数的正确语法:


this.commandHandler = new CommandHandler(this, {

    prefix: async msg => {

        let prefix;

        console.log('first');


        const rows = await con.query(`SELECT * FROM info WHERE id = ${msg.guild.id}`).catch(console.log);


        console.log('Second');


        if (rows) prefix = rows[0].prefix;


        console.log('third');


        return prefix ?? '!';

    },

    blockBots: true,

/* Rest of code here ...*/

});


查看完整回答
反对 回复 2023-07-06
  • 1 回答
  • 0 关注
  • 102 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信