BGamerManu
BGamerManu

Reputation: 11

Discord.js: How can i make my bot typing for some seconds?

I wanted to put a small "cooldown" inside my Discord bot before the bot responds to a command, do you know what I have to add to make that happen?

Upvotes: 1

Views: 1631

Answers (2)

Chiqui
Chiqui

Reputation: 39

Something as simply as this should work

client.on("message", (message) => {
    if(message.content == "!ping"){ 
        message.channel.startTyping();
       
        sleep(2000) // Wait two seconds.
        message.channel.send("pong!"); 
        message.channel.stopTyping()
    }
});


Upvotes: -2

Kelvin Schoofs
Kelvin Schoofs

Reputation: 8718

You can simply mark the bot as typing, then use setTimeout(() => { ... }, 2e3) to e.g. wait 2 seconds before executing the code in the arrow function.

Normally this is used for if a command can take a long time to execute, e.g. if it's querying/fetching a database, doing heavy computations, ... and usually not manually. After all, most users want bots to respond quickly instead of wasting a few seconds for a typing indicator.

Upvotes: 4

Related Questions