So I'm new to discord botting and js and I'm playing around with my own bot. I want to make a typing minigame. When you type ?type
in chat, the bot will say something in chat and then edit that message while counting down. When the counting down is finished, it will display the random generated word. The player needs to type the exact random word in the chat and the bot will show the total time taken.
Here is my code:
case "type":
let randomWord = Math.random().toString(36).replace(/[^a-z]+/g, '');
let timer = 3;
message.channel.send("Generating a new word..")
.then((msg)=> {
var interval = setInterval(function () {
msg.edit(`Starting in **${timer--}**..`)
}, 1000)
});
setTimeout(function() {
clearInterval(interval);
message.channel.send(randomWord)
.then(() => {
message.channel.awaitMessages(response => response.content == randomWord, {
max: 1,
time: 10000,
errors: ['time'],
})
.then(() => {
message.channel.send(`Your time was ${(msg.createdTimestamp - message.createdTimestamp) / 1000} seconds.`);
})
.catch(() => {
message.channel.send('There was no collected message that passed the filter within the time limit!');
});
});
}, 5000);
break;
Currently the code stops after it counted to down 0.
I dont understand why message.channel.send(randomWord)
doesn't work. Also I would love it if someone could help me change this code to use asynch and await if that's something viable.