【问题标题】:How do I make a command cooldown for certain discord roles?如何为某些不和谐角色设置命令冷却时间?
【发布时间】:2021-10-04 15:43:48
【问题描述】:

所以基本上,我需要为我的不和谐命令添加冷却时间,但仅限于某些角色,并且每个角色必须有不同的冷却时间。例如:成员角色:.ping cmd 的 24 小时冷却时间,订阅者角色:.ping cmd 的 12 小时冷却时间。

module.exports = {
    name: 'ping',
    description: 'pings everyone',
    execute(message, args){
        if(!message.member.roles.cache.some(r => ['869682149203271710', '869682285904031764', '869682372231196682'].includes(r.id))) return message.channel.send('You dont have perms to execute this command')
        message.channel.send('@everyone');
            }
        }

基本上我的 cmd 只允许 3 个角色使用 cmd,我需要帮助为每个允许使用 cmd 的角色添加不同的定时冷却时间。每个角色都需要有不同的冷却时间。

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    例如,如果你想冷静下来,你就需要这样做

    module.exports = {
        name: 'beep',
        description: 'Beep!',
        cooldown: 1, 
        execute(message) {
            message.channel.send('Boop.');
        },
    };
    

    【讨论】:

    • 是的,但我需要它以便每个角色都有不同的冷却时间。我不希望所有角色都有相同的冷却时间。例如:成员有 24 小时的冷却时间,而版主有 12 小时的冷却时间。
    【解决方案2】:

    这不是很好的方法,但可以作为如何实现自己的冷却系统的一个很好的例子。

    为什么不那么好?

    取决于使用情况,如果您想要单个命令的冷却时间。那么这可能是最好的方法。但是如果你想冷却多个命令,这就是它失败的地方。因为您必须一次又一次地将冷却代码复制到每个命令中。这真的是糟糕。冷却功能应该在您的命令处理程序内部实现。该命令仅提供冷却时间和角色 ID 等冷却数据。 如果您想限制特定角色使用的命令,也是如此。但正如我所说,这取决于使用情况和您想要做什么。

    const activeCooldowns = new Set();
    
    module.exports = {
        name: "ping",
        description: "Ping everyone.",
        cooldowns: [ // The order is important!
            {
                id: "867559128179671051", // role id
                time: 12 // hours
            },
            {
                id: "862850962758696990", // role id
                time: 24 // hours
            }
        ],
        execute(message, args) {
    
            // Check if the user has any of the roles to access this command
            if (!message.member.roles.cache.some(role => ["867559128179671051", "862850962758696990", "869682372231196682"].includes(role.id))) {
                return message.channel.send("You don't have permissions to execute this command!");
            }
    
            // Check for an active cooldown
            if (activeCooldowns.has(message.author.id)) {
                message.channel.send("You need to wait before using this command again!");
                return;
            }
    
            // Execute command
            message.channel.send("@everyone");
    
            // Get first matching role's cooldown
            const cooldown = this.cooldowns.find((cooldown) => {
                return message.member.roles.cache.has(cooldown.id);
            });
    
            if (cooldown) { // Check if the role has any cooldown at all
                // Command used, set cooldown
                activeCooldowns.add(message.author.id);
                setTimeout(() => { // Schedule cooldown removal
                    activeCooldowns.delete(message.author.id);
                }, cooldown.time * 60 * 60 * 1000); // Convert hours to milliseconds
            }
    
        }
    };
    
    

    为什么冷却数组的顺序很重要?

    由于用户可以拥有多个角色,如果我们有一个用户,他同时拥有这两个角色并有冷却时间,那么哪一个将获胜并决定冷却时间?这就是数组顺序出现的时候。当数组从第一个元素到最后一个元素进行搜索时,我们会选择第一个匹配项。

    您还可以仅根据用户的最高角色来确定用户的冷却时间。还有很多其他方法...

    【讨论】:

    • 好的,这真的很好。我的回答中没有考虑数组。
    猜你喜欢
    • 2021-05-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多