【发布时间】:2021-12-04 23:35:03
【问题描述】:
我想知道如何为按钮设置冷却时间?就像如果有人按下一个按钮并试图向它发送垃圾邮件,机器人会受到速率限制,discord 也有按钮垃圾邮件检测功能,但到那时我的机器人会受到速率限制,这非常慢,有人可以帮忙吗?
【问题讨论】:
标签: discord.js
我想知道如何为按钮设置冷却时间?就像如果有人按下一个按钮并试图向它发送垃圾邮件,机器人会受到速率限制,discord 也有按钮垃圾邮件检测功能,但到那时我的机器人会受到速率限制,这非常慢,有人可以帮忙吗?
【问题讨论】:
标签: discord.js
这是您可以添加到interactionCreate 事件的解决方案
// At the top of your file
const buttonCooldown = new Set()
client.on("interactionCreate", (ctx) => {
if(!ctx.isButton()) return;
if(buttonCooldown.has(ctx.user.id)) {
// user is on cooldown, do whatever you want
}
buttonCooldown.add(ctx.user.id)
setTimeout(() => buttonCooldown.delete(ctx.user.id), 10_000) // replace 10_000 with cooldown time
// other logic here
})
【讨论】: