-2

My problem : Error :

SyntaxError: await is only valid in async function

let ChooseEmbed = new Discord.RichEmbed()
.setAuthor("Choissisez le type de serveur")
.setDescription("**Normal `` ➞ [`Exemple`](https://imgur.com/upload)\n **Gaming** `` ➞ [`Exemple`](https://imgur.com/upload)\n ")
message.channel.send(ChooseEmbed).then(msg => { 

  msg.react('').then(() => msg.react(''));

  const filter = (reaction, user) => {
    return ['', ''].includes(reaction.emoji.name) && user.id === message.author.id;
  };

  msg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
    .then(collected => {
      const reaction = collected.first();

      if (reaction.emoji.name === '') {
        //Normal
        message.channel.send(VerifiedEmbed).then(msgg => { 

          msgg.react('✅').then(() => msgg.react('❌'));

          const filter = (reaction, user) => {
            return ['✅', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
          };

          msgg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
            .then(collected => {
              const reaction = collected.first();

              if (reaction.emoji.name === '✅') {
                console.log("C'est bon")

              } else {
                message.channel.send(AnnulEmbed)
                msgg.delete()
                return;
              }
            })

            .catch(collected => {
            return;
            });
        })

        msg.delete()
      } else {
        //Gaming
        message.channel.send(VerifiedEmbed).then(msggg => { 
          msggg.react('✅').then(() => msggg.react('❌'));

          const filter = (reaction, user) => {
            return ['✅', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
          };

          msggg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
            .then(collected => {
              const reaction = collected.first();

              if (reaction.emoji.name === '✅') {
                console.log("C'est bon")
                message.guild.channels.deleteAll();
                message.guild.roles.deleteAll();

                message.guild.setName(`Serveur de ${message.author.username}`).then(g => console.log("g")).catch(console.error);
                message.guild.setIcon(message.author.displayAvatarURL).then(g => console.log("g")).catch(console.error);

                let cat = await message.guild.createChannel("IMPORTANT", "category");

                } else {
                msggg.delete()
                message.channel.send(AnnulEmbed)
                return;
              }
            })
            .catch(collected => {
              return;
            });
        msg.delete()
      }
    )}})
    .catch(collected => {
      return;
    });
})

I have the error : SyntaxError: await is only valid in async function how i can fix this ?

I have search the error but i don't see them ! I tried everything I would like to know if someone could help me ^^ Do not hesitate to help me to solve my mistake

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Coco Mol
  • 1
  • 1

1 Answers1

2

The function in which you're using await is the then callback that starts here:

msggg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
  .then(collected => {

...which contains:

let cat = await message.guild.createChannel("IMPORTANT", "category");

It's not clear why you're doing that as the code doesn't use cat anywhere subsequently.

You could make that callback async, which would let you use await there:

msggg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
  .then(async (collected) => {
// -----^^^^^^^---------^

...but in general, I would recommend not mixing using promises directly via .then and .catch callbacks with async functions. Instead, I'd suggest you pick one or the other and use it throughout.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875