Discord.js "SyntaxError: await is only valid in async function"

Hey! So I’m trying to code a minigame for a Discord.js economy system. It’s a guessing game, where the bot generates 3 random words from a set and the user has to reply with them. Here’s my code:

const profileModel = require("../models/profileSchema");
module.exports = {
    name: "work",
    description: "work for coins",
    async execute(client, message, args, Discord, profileData) {
      
      let firstWords =[
     "ugly",
     "clean",
     "shiny",
     "chubby",
     "scary",
     "tiny",
      "huge"
    ]
    let firstWord = firstWords[Math.floor(Math.random() * firstWords.length)];
    let secondWords =[
        "blue",
        "red",
        "purple",
        "green",
        "pink",
        "orange",
        "turqoise"
    ]
    let secondWord = secondWords[Math.floor(Math.random() * secondWords.length)];
    let thirdWords =[
      "cat",
      "bird",
      "mouse",
      "ferret",
      "fish",
      "horse",
      "snake",
      "dog"
    ]
    let thirdWord = thirdWords[Math.floor(Math.random() * thirdWords.length)];
          
            let embed =  new Discord.MessageEmbed()
            .setColor('#ec5252')
            .setTitle('Work Guessing Game')
            .setDescription('Memorize the following words and then guess them. Answer like so: "?guesswords word1, word2, word3"')
            .addField( 'Word 1:', firstWord)
            .addField('Word 2:', secondWord)
            .addField('Word 3:', thirdWord)
    
            message.channel.send(embed)
            .then(msg => {
              msg.delete({ timeout: 10000 })
            })
            .catch(console.error);
            
            message.channel.awaitMessages(m => m.author.id == message.author.id,
              {max: 1, time: 30000}).then(collected => {
                
                   
                      if (collected.first().content.toLowerCase() == `${firstWord}, ${secondWord}, ${thirdWord}`) {
                        await profileModel.findOneAndUpdate(
                          {
                              userID: message.author.id
                          },
                          {
                              $inc: {
                                  coins: 100,
                              },
                          }
                      );
                              message.reply('Correct! You earned 100 coins.');
                              
                      }

                      else
                              message.reply(`Sorry, incorrect. The correct answers were ${firstWord}, ${secondWord}, and ${thirdWord}. You didnt get paid.`);      
              }).catch(() => {
                      message.reply('Out of time!');
              });
  
    
  }
}

When I try to run my code in the the terminal, I get this error:
SyntaxError: await is only valid in async function
How do I fix this? If you see any additional errors in my code, please let me know. Thank you so much!

Is this regular Discord.js, or discord.js-commando? I believe if you have something that looks similar to this:

client.on("message", message => {
// your commands would be here
});

just add async before the message part, so:

client.on("message", async message => {
// your commands would be here
});

You can use await inside of it now, without needing other async functions I believe.

Hope this helps. :relaxed:

Also,

In your message embed, put a semicolon after the last .addField
so it’s like this :slight_smile:

    let embed =  new Discord.MessageEmbed()
            .setColor('#ec5252')
            .setTitle('Work Guessing Game')
            .setDescription('Memorize the following words and then guess them. Answer like so: "?guesswords word1, word2, word3"')
            .addField( 'Word 1:', firstWord)
            .addField('Word 2:', secondWord)
            .addField('Word 3:', thirdWord);

Sorry for the third reply, I believe you also should make sure execute is a function so

  async function execute(client, message, args, Discord, profileData) {

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.