简体   繁体   中英

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

我想在我的 Discord 机器人响应命令之前在我的 Discord 机器人中放置一个小的“冷却时间”,你知道我必须添加什么才能实现吗?

You can simply mark the bot as typing, then use setTimeout(() => { ... }, 2e3) to eg 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, eg 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.

sleep(second * 1000)效率更高

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


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM