Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
Async in a if condition (JavaScript v13)

I want to use the await keyword in an if statement but it says:

await message.guild.channels.create("tickets", {
^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules
if (message.content.startsWith("n!ticket setup")) {
  let channel = message.mentions.channels.first()
  let kate;
  let modrole = message.mentions.roles;

  message.guild.channels.cache.forEach(chn => {
    if (chn.type == "GUILD_CATEGORY" && !kate && chn.name.toLowerCase() == "tickets ") {
      kate = chn;
    }
  })

  if (!channel) return message.channel.send({
    content: "Du musst einen Kanal angeben wo die Nachricht reingesendet werden soll."
  });

  if (!kate) {
    await message.guild.channels.create("tickets", {
      type: "GUILD_CATEGORY",
      permissionOverwrites: [{
          id: message.guild.id,
          deny: ["VIEW_CHANNEL"]
        },
        {
          id: bot.user.id,
          allow: ["VIEW_CHANNEL"]
        }
      ]
    }).then(l => kate = l);
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Change your code to something like below ,wrap everything in an async function.You have to wrap your if code in some async function to make it work

async function foo(){
  let channels=message.guild.channels.cache
    for(let i=0;i<channels.length;i++){
            if(channels[i].type == "GUILD_CATEGORY" && !kate && channels[i].name.toLowerCase() == "tickets "){
                kate = channels[i];
            }
        })
        
        if(!channel) return message.channel.send({content: "Du musst einen Kanal angeben wo die Nachricht reingesendet werden soll."});
       
        if(!kate){
            await message.guild.channels.create("tickets", {
                type:"GUILD_CATEGORY",
                permissionOverwrites:[
                    {id:message.guild.id, deny:["VIEW_CHANNEL"]},
                    {id:bot.user.id,allow:["VIEW_CHANNEL"]}
                ]
            }).then(l=>kate=l);
        }```


}

Or May be you can create async version of forEach from example See this

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!