Discord.js 机器人速率限制

编程入门 行业动态 更新时间:2024-10-10 19:18:34
本文介绍了Discord.js 机器人速率限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我想在线上传我的不和谐机器人,但我想防止用户向它的命令发送垃圾邮件.所以让延迟为 5 秒,如果用户运行 !help 机器人会回答,但如果用户在延迟到期之前运行 !help 机器人会说:wait some再次使用此命令之前的时间.此外,我希望延迟仅适用于消息作者,而不影响其他用户.我正在使用命令处理程序,所以可以制作类似 module.exports.delay 的东西吗?

So i want to upload my discord bot online but i want to prevent users from spamming it's commands. So let the delay be 5 seconds, if user runs !help the bot answers, but if user run !help before the delay is expired bot says: wait some time before using this command again. Also i want the delay to work only for message author and not affect other users. I'm using command handler so is it possible to make something like module.exports.delay?

推荐答案

为此,您可以使用以下时间戳:

to do that you can use timestamps like:

let msgTimestamp = []; if(message.content === "?" + "help") { if(typeof msgTimestamp[0] !== "undefined") { if(msgTimestamp[0] + 5000 < Date.now()) { message.channel.send("Here embed of command help."); msgTimestamp = []; } else { message.channel.send("wait some time before using this command again."); } } else { msgTimestamp.push(Date.now()); message.channel.send("Here embed of command help."); } }

在此代码中,您使用时间戳,检查消息的内容是否与您要使用的命令一致,如果用户已经发送消息,则包含消息时间戳的变量已满,然后检查如果变量的 typeof 不是未定义的,那么你检查 id 用户发送消息时的时间戳低于实际时间戳 + 你想要多少延迟!

In this coode you use timestamps, you check if the content of the message is eualt to the command you want to use, if the user had already sent a message, the variable which containing the message timestamp is full, you check then if the the typeof of the variable is not undefined and then you check id the timestamp of when the user sent the message is lower than the actual timestamp + how many delay you want!

更多推荐

Discord.js 机器人速率限制

本文发布于:2023-11-23 13:10:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:速率   机器人   Discord   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!