Cofi-Dev / Cofi-Bot

🤖 Cofi-Bot is a multifunction chat bot for Discord.
MIT License
6 stars 1 forks source link

RBAC system commands. #9

Open victorst79 opened 4 years ago

victorst79 commented 4 years ago

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

victorst79 commented 3 years ago

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 commented 3 years ago

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