Roles
Note Make sure the bot has permission to manage your server’s roles.
This tutorial will walk you through some of the more advanced role management functions. In previous tutorials we made use of hasRole
, giveRole
, takeRole
, but this tutorial will focus on creating, editing and deleting roles. It will showcase an example that gives your server admins the ability to allow certain users to create their own role and then freely change its name and color.
Create
In order to create a new role, you have to make use of the RoleInfo
type. This type allows you to specify some information about the new role that you’re creating so that the bot can forward it to Discord.
Here are the available pieces of data that RoleInfo
can accept:
.Name | String | required | The role’s name |
.Color | Integer | optional | The color the role should have (as a decimal, not hex) |
.Separate | Bool | optional | If true, members with this role show up separately on the side bar |
.Permissions | Array[DiscordPermission] | optional | The overall permissions of the role |
.Mentionable | Bool | optional | Whether this role is mentionable |
It’s possible to create a new role by only supplying the name. This will result in a new role with all the settings set to default.
Once you’ve set up your RoleInfo
data, you can pass it along to createRole
. Here is an example:
Here is another example, but this time we specify a color:
With this example it sets a couple of the permissions:
Editing
For any role that the bot can manage, there are a few other functions that can edit these existing roles:
editRoleName roleID newName
This sets the role given by roleID
to have newName
. roleID
is the Discord ID, not the role’s name.
editRoleColor roleID color
This sets the role given by roleID
to have color
which should be a decimal (not hex). roleID
is the Discord ID, not the role’s name.
moveRoleAfter roleID afterID
moveRoleBefore roleID beforeID
These two move functions deal with the Discord role hierarchy. In Discord, roles that come after a certain role are higher up in the hierarchy, while roles that come before a certain role are lower down in the hierarchy. This can have a big impact on what permissions a role has: for example, trying to edit a role higher than your own highest role is not allowed.
The bot is able to shuffle roles around using moveRoleAfter
and moveRoleBefore
. The catch is that the bot is not allowed to move a role to a position higher than its own highest role. You will need to go into your guild’s settings and make sure the bot’s highest role sits somewhere in the role hierarchy such that allows it to do everything you need the bot to do.
Also keep in mind that roleID
, afterID
, and beforeID
all need to reference the role IDs provided by Discord. They are not the role names.
Deleting
You can also delete a role using deleteRole roleID
. For this function, roleID
is still the Discord ID, not the role’s name.
Example: Custom role for a member
This example will create a few functions. The first two are functions that only your guild administrators can use, and they enable them to allow and disallow server members to create and manage their own role.
allow-role
/create-cmd name: allow-role description: Allow a certain user to create and manage their own role
Command initialization code
Command execute code
disallow-role
/create-cmd name: disallow-role description: Disallows a user from having a custom role
Command initialization code
Command execution code
The next functions will be all about creating a custom role, editing its name, and editing its color.
claim-role
/create-cmd name: claim-role description: Claim a custom role in the server
Command initialization code
Command execution code
edit-role
/create-cmd name: edit-role description: Edit your custom role
Command initialization code
Command execution code
Limitations
Currently there is no way to set and manage a role image through the bot. This is a feature that is planned but may take some time to implement.