简体   繁体   中英

Discord.js 'await is only valid in an async function' even though it is an async function?

I am coding a bot to make a ticketing system, and I am trying to get the bot to react to the message, but it isn't as I am getting the error await is only valid in an async function . I know what that means, and the part where I am confused is that it is an async function: I know this because earlier in the function/event, there is an await statement. Here is the code:

client.on("message", async (message) => {

if (message.author.bot) return;

const filter = (m) => m.author.id === message.author.id;

if (message.content === "-ticket") {

let channel = message.author.dmChannel;
if (!channel) channel = await message.author.createDM();

let embed = new Discord.MessageEmbed();
embed.setTitle('Open a Ticket')
embed.setDescription('Thank you for reaching out to us. If you have a question, please state it below so I can connect you to a member of our support team. If you a reporting a user, please describe your report in detail below.')
embed.setColor('AQUA')

message.author.send(embed);
channel
  .awaitMessages(filter, {max: 1, time: 1000 * 300, errors: ['time'] })
  .then((collected) => {

    const msg = collected.first();
    message.author.send(`
     >>> ✅ Thank you for reaching out to us! I have created a case for 
    your inquiry with out support team. Expect a reply soon!
    ❓ Your question: ${msg}
    `);

    let claimEmbed = new Discord.MessageEmbed();
    claimEmbed.setTitle('New Ticket')
    claimEmbed.setDescription(`       
    New ticket created by ${message.author.tag}: ${msg}

    React with ✅ to claim!
    `)
    claimEmbed.setColor('AQUA')
    claimEmbed.setTimestamp()

    try{

      let claimChannel = client.channels.cache.find(channel => channel.name === 'general');
      claimChannel.send(claimEmbed);

      await claimMessage.react("✅");

    } catch (err) {
      throw (err);
    }

  })
  .catch((err) => console.log(err));
  }
})

When you collect the messages, there is an arrow function that has a missing async keyword:

client.on('message', async (message) => {
  if (message.author.bot) return;

  const filter = (m) => m.author.id === message.author.id;

  if (message.content === '-ticket') {
    let channel = message.author.dmChannel;
    if (!channel) channel = await message.author.createDM();

    let embed = new Discord.MessageEmbed();
    embed.setTitle('Open a Ticket');
    embed.setDescription(
      'Thank you for reaching out to us. If you have a question, please state it below so I can connect you to a member of our support team. If you a reporting a user, please describe your report in detail below.',
    );
    embed.setColor('AQUA');

    message.author.send(embed);
    channel
      .awaitMessages(filter, { max: 1, time: 1000 * 300, errors: ['time'] })
      // it should be async
      .then(async (collected) => {
        const msg = collected.first();
        message.author.send(`
     >>> ✅ Thank you for reaching out to us! I have created a case for
    your inquiry with out support team. Expect a reply soon!
    ❓ Your question: ${msg}
    `);

        let claimEmbed = new Discord.MessageEmbed();
        claimEmbed.setTitle('New Ticket');
        claimEmbed.setDescription(`
    New ticket created by ${message.author.tag}: ${msg}

    React with ✅ to claim!
    `);
        claimEmbed.setColor('AQUA');
        claimEmbed.setTimestamp();

        try {
          let claimChannel = client.channels.cache.find(
            (channel) => channel.name === 'general',
          );
          claimChannel.send(claimEmbed);

          await claimMessage.react('✅');
        } catch (err) {
          throw err;
        }
      })
      .catch((err) => console.log(err));
  }
});

Make sure if this code belongs to a promise callback to ensure the callback function is also asynchronous. If you don't provide your full code we can't provide anymore help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM