Skip to content
SQKo edited this page Dec 9, 2023 · 3 revisions

Roles are part of Guild (Server). See Overwrite for specific channel role permission.

use Discord\Parts\Guild\Role;

A role defines permissions for the guild. Members can be added to the role. The role belongs to a guild.

Requires GUILDS discord intent

GUILD_ROLE_CREATE

GUILD_ROLE_UPDATE

GUILD_ROLE_DELETE


Create a Role in the Guild

Your BOT must have manage_roles permission

You must first get the Guild object to use the code below

Create a role named "New Role" with pink color, hoisted, and mentionable:

$guild->createRole([
    // All options are optional
    'name' => 'New Role',
    'color' => 0xFF00FF,
    'hoist' => true,
    'mentionable' => true,
    // more options in Docs
])->then(function (Role $role) {
    echo 'Created a new role - ID:', $role->id;
})->done();

https://discord.com/developers/docs/resources/guild#create-guild-role

Get a Specific Role

You must first get the Guild object to use the code below. Change 123123123123123123 below with the Role ID you want to retrieve

Cached, synchronous:

$role = $guild->roles->get('id', '123123123123123123');

If the code above returns null, you may need to freshen the RoleRepository first (Promise, asynchronous):

$guild->roles->freshen()->then(function (RoleRepository $roles) {
    $role = $roles->get('id', '123123123123123123');
})->done();

Add a Member to the Role

Your BOT must have manage_roles permission

You must first get the Member object to use the code below

Add the $member to the $role:

$member->addRole($role)->then(function () {
    // ...
})->done();

Or, change 123123123123123123 below with the Role ID:

$member->addRole('123123123123123123')->then(function () {
    // ...
})->done();

https://discord.com/developers/docs/resources/guild#add-guild-member-role

Remove a Member from the Role

Your BOT must have manage_roles permission

You must first get the Member object to use the code below

Remove the $member from the $role:

$member->removeRole($role)->then(function () {
    // ...
})->done();

Or, change 123123123123123123 below with the Role ID:

$member->removeRole('123123123123123123')->then(function () {
    // ...
})->done();

https://discord.com/developers/docs/resources/guild#remove-guild-member-role

Clone this wiki locally