0

I'm trying to check if a player is already in my database and if not send him a custom discord message.

I have an async function that awaits the response of my database (I am using supabase). If the user does not exist it returns false, else it returns true.

async function doesUserExist(user_id:string){
    let id = await database.GetUserInfo(user_id)
    if (id == null){
        return false;
    }
    return true;
}

This info is checked when a message is send to the bot. In the code snippet below I use the response of the async function to determine if I should send a discord message. However the code snippet never runs even when the output of the function should be true.

client.on("messageCreate", (message) => {
    if(!doesUserExist(message.author.id)) {
        // do something
    }
}

I presume this has something to do with async functions returning promise and therefore the if statement just never fires. I am however fairly new to typescript (and javascript).

the packages I am using are

If you have any further questions regarding my issue feel free to ask them.

3
  • 2
    Yep, it should be async (message) => { and you should resolve the promise first, i.e if(! await doesUserExist(message.author.id)) { Commented Nov 20, 2022 at 0:55
  • Thank you very much @Zslot I did not know u could pass an async when creating a function that way. Many thanks for the quick respons. Commented Nov 20, 2022 at 1:06
  • The second parameter for client.on is a function. It doesn't care if it's a synchronous function (the default) or an asynchronous function (specified by the async keyword). Commented Nov 20, 2022 at 1:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.