0

I am new to js and have been developing a discord bot for a few days with api calls etc.

But here I am facing a problem that I do not understand at all. I have 2 functions, the 1st calls the second on a condition, the 2nd makes an API call, this allows me to check that the user input is available so depending on the result I return true / false.

The problem is that in my case, the 2nd function returns false, but the 1st function still passes the condition.

I imagine that the problem is a misunderstanding of async functions and awaits. But now I'm drying up a bit, I need help ..

static createTeam (args, message) {
    if (args.length == 3 && args[0].length > 0 && args[1].length > 0 && args[2].length > 0 ) {
         
        var clanTag = args[1]

        if (this.checkIfClanExist(clanTag)) { // Pass in this condition even if the return = false
            message.reply(clanTag)
        }
        else {
            message.reply('There is a probleme with the clan')
        }
    }
    else {
        message.reply('Mhhhhh i think something is missing in your request !')
    }
}
static async checkIfClanExist(tag) {
   var clan = await CocApi.getClanByTag(tag)
 
   if (clan == null) {
       return false // Go through this return but the createTeam funtcion goes through the condition anyway

   }
   return true
}

Thank you in advance for your help !

(and sorry for my english lol)

Crayzze
  • 3
  • 3
  • Have you tried making the call to the 2nd function async too? I mean, make the createTeam an async function, then call the checkIfClanExist with a await too. – m4el Mar 19 '21 at 16:10

1 Answers1

0

You need to await the call on async method calls. Something like below

let res = await this.checkIfClanExist(clanTag); 
if (res) { // Pass in this condition even if the return = false
    message.reply(clanTag)
}
kvp2982
  • 151
  • 5