Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RBAC system commands. #9

Open
2 of 4 tasks
victorst79 opened this issue Jul 16, 2020 · 2 comments
Open
2 of 4 tasks

RBAC system commands. #9

victorst79 opened this issue Jul 16, 2020 · 2 comments
Labels
Alpha-v1 APP Related to App (Core) feature New feature or request High priority
Milestone

Comments

@victorst79
Copy link
Member

victorst79 commented Jul 16, 2020

Description

A role system must be implemented for the available commands.
If a user has moderator permissions, he can see and execute the commands with the tag, Moderation Commands and Public Commands for example. Whereas a normal user without moderator roles could only execute and view public commands.

It should be noted that when talking about displaying commands, it is referred to that the normal role user executes the "help" command, only the commands available to his role should be shown.

Progress

  • Commands organized by folders.
  • Commands identified with the type of command they belong to (Moderation, Private ...)
  • Command execution only available for assigned roles.
  • Show available commands based on the roles of the user who runs the "help" command.
@victorst79 victorst79 added feature New feature or request High priority labels Jul 16, 2020
@victorst79 victorst79 added this to the Alpha milestone Jul 16, 2020
@victorst79
Copy link
Member Author

An improvement has been added to the feedback of the! Info command.
Now it shows the roles and permissions that the user has on the server.

@victorst79
Copy link
Member Author

victorst79 commented Dec 10, 2020

Working in the RBAC system for commands according to the role / permission of the user.
Reference to follow for the development of the system.

Permissions

Permission Flags

Some commands should only be used by someone with certain permissions.
There are options to help you do this.
The two options to use are clientPermissions and userPermissions.

const { Command } = require('discord-akairo');

class BanCommand extends Command {
    constructor() {
        super('ban', {
            aliases: ['ban'],
            args: [
                {
                    id: 'member',
                    type: 'member'
                }
            ],
            clientPermissions: ['BAN_MEMBERS'],
            userPermissions: ['BAN_MEMBERS'],
            channel: 'guild'
        });
    }

    async exec(message, args) {
        if (!args.member) {
            return message.reply('No member found with that name.');    
        }

        await args.member.ban();
        return message.reply(`${args.member} was banned!`);
    }
}

module.exports = BanCommand;

This now checks for the required permissions for the client, then the user.
When blocked, it emits missingPermissions on the command handler.
It will pass the message, command, either client or user, then the missing permissions.

Dynamic Permissions

Sometimes, you may want to check for a role instead of permission flags.
This means you can use a function instead of an array!
A function can be used on both clientPermissions and userPermissions.

The return value is the missing parameter that is sent to the missingPermissions event.
If the return value is null, then that means they're not missing anything.

const { Command } = require('discord-akairo');

class BanCommand extends Command {
    constructor() {
        super('ban', {
            aliases: ['ban'],
            args: [
                {
                    id: 'member',
                    type: 'member'
                }
            ],
            clientPermissions: ['BAN_MEMBERS'],
            channel: 'guild'
        });
    }

    userPermissions(message) {
        if (!message.member.roles.cache.some(role => role.name === 'Moderator')) {
            return 'Moderator';
        }

        return null;
    }

    async exec(message, args) {
        if (!args.member) {
            return message.reply('No member found with that name.');    
        }

        await args.member.ban();
        return message.reply(`${args.member} was banned!`);
    }
}

module.exports = BanCommand;

Origin:
https://github.com/discord-akairo/discord-akairo/blob/master/docs/commands/permissions.md

@victorst79 victorst79 added the APP Related to App (Core) label Dec 10, 2020
@victorst79 victorst79 modified the milestones: Alpha, Alpha V1 Jan 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Alpha-v1 APP Related to App (Core) feature New feature or request High priority
Projects
None yet
Development

No branches or pull requests

1 participant