Discord.js 机器人速率限制 [英] Discord.js Bot rate limit

查看:27
本文介绍了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 机器人速率限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆