A Guide to Discord Bots

Roles & Channels Permissions

A clean server needs a clean permission system.
Thankfully, permissions can be made really easy:
Channel (User) > Channel (Role) > Channel (@ everyone) > Server (@ everyone) > Server (Role)

Note: if a channel and category are:

  • Synced: the channel's permissions are overwritten by the category's permissions
  • Not synced: the channel's permissions stay, and the category's permissions are not used

Channel-wide permissions will always overwrite server-wide permissions.
I use almost exclusively channel-wide role permissions, plus server-wide role permissions for server-wide exclusive permissions: administrator, managing server/channels/roles, reading audit logs...

I won't explain how to use permissions, because there's a good tutorial by Discord themselves.
Instead, let's manage them with our bot!

client.on('message', message => {
    // Getting member permission
    message.member.hasPermission('SEND_MESSAGES');
    message.member.hasPermissions(['SEND_MESSAGES', 'ADD_REACTIONS']);

    // Getting channel-wide user permissions
    message.member.permissionIn('channel_id').hasPermission('SEND_MESSAGES');
    message.member.permissionIn('channel_id').hasPermissions(['SEND_MESSAGES', 'ADD_REACTIONS']);

    // Setting channel-wide user permissions
    message.channel.overwritePermissions('user_id', { SEND_MESSAGES: true, ADD_REACTIONS: false});


    // Getting channel-wide role permissions
    /* Alright, this is a little more complicated because there's no
        built-in method for it.
        You can make one using the following:
    */
        message.channel.permissionOverwrites.get('role_id').allow;
        message.channel.permissionOverwrites.get('role_id').deny;

    // Setting channel-wide role permissions
    message.channel.overwritePermissions('role_id', { SEND_MESSAGES: true, ADD_REACTIONS: false});


    // Getting server-wide role permissions
    message.guild.roles.get('rold_id').hasPermission('SEND_MESSAGES');
    message.guild.roles.get('rold_id').hasPermissions(['SEND_MESSAGES', 'ADD_REACTIONS']);

    // Setting server-wide role permissions
    message.guild.roles.get('role_id').setPermissions([SEND_MESSAGES: true, ADD_REACTIONS: false]);
});

/* List of permissions, directly taken from the Discord.js code
ADMINISTRATOR
CREATE_INSTANT_INVITE
KICK_MEMBERS
BAN_MEMBERS
MANAGE_CHANNELS
MANAGE_GUILD
ADD_REACTIONS
VIEW_AUDIT_LOG
VIEW_CHANNEL
READ_MESSAGES
SEND_MESSAGES
SEND_TTS_MESSAGES
MANAGE_MESSAGES
EMBED_LINKS
ATTACH_FILES
READ_MESSAGE_HISTORY
MENTION_EVERYONE
USE_EXTERNAL_EMOJIS
EXTERNAL_EMOJIS
CONNECT // Voice Channel
SPEAK
MUTE_MEMBERS
DEAFEN_MEMBERS
MOVE_MEMBERS
USE_VAD // Voice Auto Detection
CHANGE_NICKNAME
MANAGE_NICKNAMES
MANAGE_ROLES
MANAGE_ROLES_OR_PERMISSIONS
MANAGE_WEBHOOKS
MANAGE_EMOJIS
*/

You can also check if a user has certain roles:

client.on('message', message => {
    // If member has a role
    message.member.roles.has('role_id');

    // Get all members that have a role
    message.guild.roles.get('role_id').members;

    // Giving roles to a member
    message.member.addRole('role_id');
    message.member.addRoles(['role_id', 'role_id']);

    // Removing roles from a member
    message.member.removeRole('role_id');
    message.member.removeRoles(['role_id', 'role_id']);
});

results matching ""

    No results matching ""