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

320
Visualizações
getting SyntaxError: await is only valid in async functions and the top level bodies of modules while working through the discord.js guide

I'm working through the guide for discord.js but when I try and set permissions for the slash commands I keep getting the SyntaxError: await is only valid in async functions and the top level bodies of modules. not sure if I missed something or if it's something simple since I'm new to this but I couldn't find a solution for it.

here's the code from the guide that I'm trying to use

if (!client.application?.owner) await client.application?.fetch();

const command = await client.guilds.cache.get('123456789012345678')?.commands.fetch('876543210987654321');

const permissions = [
    {
        id: '224617799434108928',
        type: 'USER',
        permission: false,
    },
];

await command.permissions.add({ permissions });
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I realized I didn't put it in a function, I don't know how I missed that but I guess I really needed a break since I got it right after I took one.

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to wrap await in an async-marked function, if you haven't already.

Try the code below!

// Change the name of the function to whatever name you want!
const discordFetchData = async () => {
  if (!client.application?.owner) await client.application?.fetch();

  const command = await client.guilds.cache.get('123456789012345678')?.commands.fetch('876543210987654321');

  const permissions = [
    {
      id: '224617799434108928',
      type: 'USER',
      permission: false,
    },
  ];

  await command.permissions.add({ permissions });
};

discordFetchData();

The code above will give you a basic understanding of what to do to your code (wrap it in an async function).

You can also make the function a self-calling, anonymous, asynchronous function, like below.

(async () => {
  if (!client.application?.owner) await client.application?.fetch();

  const command = await client.guilds.cache.get('123456789012345678')?.commands.fetch('876543210987654321');

  const permissions = [
    {
      id: '224617799434108928',
      type: 'USER',
      permission: false,
    },
  ];

  await command.permissions.add({ permissions });
})();

In the end, both of these solutions will work!


For more information, visit async function - JavaScript | MDN.

about 4 years ago · Juan Pablo Isaza Relatório

0

You could try this also:

(async function(){
   if (!client.application?.owner) await client.application?.fetch();
   //And more await calls...
})();
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