1

i'm trying to make a discord bot. I'm using AkairoClient as a framework, within it there's an option to set a prefix. what I have is the following code:

            // 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 ...*/
        });

When i execute this, The console prints:

  • First
  • Third
  • First
  • Third
  • Second
  • Second

I'm having issues understanding on how should I make this work properly as I want the prefix to get the value of rows, but in this instance prefix is returning as undefined before the query finishes

Jayy
  • 73
  • 1
  • 8
  • What is `con.query`? Doesn't the library you use support promises instead of callbacks? – Bergi Nov 04 '20 at 15:39
  • i'm using MySQL node module. i don't think it supports it, i'm looking at MySQL2 which might. `con` is a constant define in a line i didnt show, it's a connection to the MySQL database. – Jayy Nov 04 '20 at 15:41

1 Answers1

0

I'm not entirely sure how your modules work but this would be the correct syntax for an async await function:

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 ...*/
});
Ahsoka
  • 149
  • 7