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

Time:12-25

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related