Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda