等待仅在异步功能上

时间:2019-09-09 16:05:34

标签: javascript discord discord.js

我的问题:错误:

  

SyntaxError:等待仅在异步功能中有效

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;
    });
})

我有错误:SyntaxError:await仅在异步函数中有效 我该如何解决?

我已经搜索了错误,但没有看到它们!我尝试了一切我想知道是否有人可以帮助我^^不要犹豫帮助我解决我的错误

1 个答案:

答案 0 :(得分:2)

您使用await的函数是从此处开始的then回调:

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

...其中包含:

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

目前尚不清楚为什么要这么做,因为代码随后在任何地方都没有使用cat

可以进行该回调async,从而使您可以在其中使用await

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

...但是,一般而言,我建议不要直接通过.then.catch函数的async和{{1}}回调使用promise混合。相反,我建议您选择一个或另一个并在整个过程中使用它。