我想添加冷却时间并编写此代码,但不起作用,您能告诉我错误在哪里吗?非常感谢。
client.on('message', message => {
const helloSet = new Set();
if (helloSet.has(message.author.id)) {
const expirationTime = helloSet.get(message.author.id) + 10000; // 10000 is cooldow
let user1 = message.author;
let user = message.author;
if (message.mentions.users.first()) {
user = message.mentions.members.first().user
}
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`Ожидайте ${timeLeft.toFixed(1)} перед выдачей печеньки`);
} else {
helloSet.add(message.author.id)
if (user !== user1) {
if (message.content.includes(`?`)) {
db.add(`${user.id}_cockie`, 1);
setTimeout(() => {
helloSet.delete(message.author.id)
}, 10000)
}}}}});
答案 0 :(得分:0)
你可以试试这个!
// First, this must be at the top level of your code, **NOT** in any event!
const talkedRecently = new Set();
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);
}