node.js - 命令冷却 10 秒不工作不和谐机器人
问题描述
我想添加冷却时间并编写此代码,但不起作用,您能告诉我错误在哪里吗?很感谢。
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)
}}}}});
解决方案
你可以试试这个!
// 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);
}
推荐阅读
- java - 不使用 excel 为任何长度的字符串格式化图表的最佳方法
- javascript - 处理 Typeahead 和外部 JSON 输入不起作用
- python - 在条件下将整个网络从 float32 切换到 float64
- angular - Angular - 从 API 获取数据
- python - 使用 matplotlib.pyplot 绘制饼图时,绘制的线会被压缩
- linux - 如何列出某些提交之间的提交消息?
- docker - 谷歌云上的 Jupyter Docker
- algorithm - 谷歌面试问题:在 2D 阵列上优化分配人和汽车
- h2o - 如何确定 H2O 二元分类器中哪个标签被认为是“正”类?
- android - 后台服务因 CPU 使用过多而终止