简体   繁体   English

我怎样才能给 discord 用户一个特定的角色限制?

[英]How can I give discord users with a specific role limitations?

I want to limit the actions of users on a discord server that are interacting with a discord bot for a specific time.我想在特定时间内限制 discord 服务器上与 discord 机器人交互的用户的操作。 As an example: The user with the role "user" can only press a button 10 times in a day (24h).例如:角色为“user”的用户一天(24 小时)只能按一个按钮 10 次。 A other user with the role "user2" can press the button 20 times in a day.另一个角色为“user2”的用户一天可以按按钮 20 次。 After that the user gets a message that he reached the daily limit.之后,用户会收到一条消息,说明他已达到每日限额。 How can I do that in js?我怎样才能在js中做到这一点?

I couldn't find anything about the topic.我找不到有关该主题的任何信息。

You have to create a function that will check whenever the member is rate limited and another where you add 1 to the limit.您必须创建一个 function 来检查成员何时受到速率限制,以及另一个您将限制加 1 的地方。 I decided to merge those 2 functions so you can call it when someone runs a command and it will add 1 to the limit.我决定合并这两个函数,这样你就可以在有人运行命令时调用它,它会将限制加 1。

I assume that this is for a guild (since its role-based).我假设这是一个公会(因为它是基于角色的)。 So you must give a GuildMember as a variable of the function.所以你必须给一个GuildMember作为function的变量。

let Limits = {};

function isRateLimited(member){
    let Today = new Date();
    Limits[member.id] ??= {Date:Today,Rate:0}; //if Limits[member.id] has no value, then it will set it as an object.
    if(!(Limits[member.id].Date.getDate() == Today.getDate() && Limits[member.id].Date.getMonth() == Today.getMonth() && Limits[member.id].Date.getFullYear() == Today.getFullYear())){//if not today, then reseting the rate limit & setting date as today.
        Limits[member.id].Date = Today;
        Limits[member.id].Rate = 0;
    };
    if(member.roles.cache.find(role => role.name == "ROLE 1") && Limits[member.id].Rate < 20){ //if member has a specific role and his rate limit is lower than authorized, then adding 1 to the rate limit and returning true
        Limits[member.id].Rate++;
        return true;
    }else if(member.roles.cache.find(role => role.name == "ROLE 2") && Limits[member.id].Rate < 10){ //if member has a specific role and his rate limit is lower than authorized, then adding 1 to the rate limit and returning true
        Limits[member.id].Rate++;
        return true;
    }else if(member.roles.cache.find(role => role.name == "ROLE 3") && Limits[member.id].Rate < 5){ //if member has a specific role and his rate limit is lower than authorized, then adding 1 to the rate limit and returning true.
        Limits[member.id].Rate++;
        return true;
    };
    return false; //the member is rate limited (he has reached the limit), it will return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM