問題描述
我正在開發(fā)一個 discord.js 機(jī)器人,我想為命令設(shè)置冷卻時間.
I am developing a discord.js bot and I want to make a cooldown for a command.
我在 Google 上看到了很多關(guān)于如何執(zhí)行此操作的教程,但所有這些教程都針對所有命令執(zhí)行此操作(因此,當(dāng)用戶輸入 !mycmd 時,所有用戶都必須等待 X 分鐘/秒才能輸入再次).
I saw a lot of tutorials on how to do it on Google, but all those tutorials do it for all the commands (so when a user type !mycmd all the users have to wait X minutes/seconds until it can be typed again).
但我想為每個用戶執(zhí)行此操作(當(dāng)用戶輸入 !mycmd 時,只有該用戶必須等待 X 分鐘/秒,直到用戶可以再次輸入).
But I want to do it for each user (when a user type !mycmd , ONLY this user have to wait X minutes/seconds until THE USER can type it again).
有可能嗎?
謝謝!
推薦答案
是的,這很容易而且可能.
Yes it is easy and possible.
在你的 JS 文件的頂部添加這個:
Add this at the top of your JS file:
// First, this must be at the top level of your code, **NOT** in any event!
const talkedRecently = new Set();
現(xiàn)在在命令事件中添加:
Now in the command event add this:
if (talkedRecently.has(msg.author.id)) {
msg.channel.send("Wait 1 minute before getting typing this again. - " + msg.author);
} else {
// the user can type the command ... your command code goes here :)
// Adds the user to the set so that they can't talk for a minute
talkedRecently.add(msg.author.id);
setTimeout(() => {
// Removes the user from the set after a minute
talkedRecently.delete(msg.author.id);
}, 60000);
}
這篇關(guān)于Discord.js - 每個用戶的命令冷卻時間,而不是所有用戶的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!