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!