AlexzanderFlores / WOKCommands

132 stars 61 forks source link

Requiredrole dose not work with multiple roles #197

Open GamerBossHarmon opened 2 years ago

GamerBossHarmon commented 2 years ago

let's say I have 3 roles Admin, Mod, Temp-mod and I want all of them to use /kick I would use /requiredrole kick ID for all 3 of them but if I was a Mod and use /kick I would get this message "You do not have the required roles to use this command! You need one of the following: Admin, Temp-mod" if I gave myself Admin and still have Mod I would get this message "You do not have the required roles to use this command! You need one of the following: Temp-mod" the command will only work if I have all 3 roles

benzon commented 2 years ago

Simple solution to get around this issue prob not perfect in any way, is to modify has-roles.js

What i did is very simple, just added a part to the for loop that uses break if a role matches, and clears the table saving missing roles.

           if (realRole !== null && member.roles.cache.has(role)) {
                missingRoles.pop();
                missingRolesNames.pop();
                break;
            }
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
const CommandErrors_1 = __importDefault(require("../enums/CommandErrors"));
module.exports = async (guild, command, instance, member, user, reply) => {
    if (!guild || !member) {
        return true;
    }
    const { error } = command;
    const roles = command.getRequiredRoles(guild.id);
    if (roles && roles.length) {
        const missingRoles = [];
        const missingRolesNames = [];
        for (const role of roles) {
            const realRole = await guild.roles.fetch(role);
            if (realRole !== null && member.roles.cache.has(role)) {
                missingRoles.pop();
                missingRolesNames.pop();
                break;
            }
            if (realRole !== null && !member.roles.cache.has(role)) {
                missingRoles.push(role);
                missingRolesNames.push(realRole.name);
            }
        }
        if (missingRoles.length) {
            if (error) {
                error({
                    error: CommandErrors_1.default.MISSING_ROLES,
                    command,
                    message: null,
                    info: {
                        missingRoles,
                    },
                });
            }
            else {
                reply(instance.messageHandler.get(guild, 'MISSING_ROLES', {
                    ROLES: missingRolesNames.join(', '),
                })).then((message) => {
                    if (!message) {
                        return;
                    }
                    if (instance.delErrMsgCooldown === -1 || !message.deletable) {
                        return;
                    }
                    setTimeout(() => {
                        message.delete();
                    }, 1000 * instance.delErrMsgCooldown);
                });
            }
            return false;
        }
    }
    else if (command.doesRequireRoles) {
        reply(instance.messageHandler.get(guild, 'REQUIRE_ROLES', {
            PREFIX: instance.getPrefix(guild),
            COMMAND: command.names[0],
        })).then((message) => {
            if (!message) {
                return;
            }
            if (instance.delErrMsgCooldown === -1 || !message.deletable) {
                return;
            }
            setTimeout(() => {
                message.delete();
            }, 1000 * instance.delErrMsgCooldown);
        });
        return false;
    }
    return true;
};