RoleManager
Class · Sharkord\Managers\RoleManager
Class RoleManager
Manages role lifecycle events and exposes actions for creating roles. Delegates all cache storage to a Roles collection instance.
Accessible via $sharkord->roles.
Examples
Section titled “Examples”// Create a new role and then edit it$sharkord->roles->add()->then(function(\Sharkord\Models\Role $role) { return $role->edit( 'Moderators', '#00aaff', \Sharkord\Permission::MANAGE_MESSAGES, \Sharkord\Permission::PIN_MESSAGES, );});
// Set a role as the server default$sharkord->roles->get(3)?->setAsDefault();
// Delete a role$sharkord->roles->get(5)?->delete();
$sharkord->on(\Sharkord\Events::ROLE_CREATE, function(\Sharkord\Models\Role $role): void { echo "New role created: {$role->name}\n";});Methods
Section titled “Methods”__construct()
Section titled “__construct()”RoleManager constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$sharkord | Sharkord | The main bot instance. |
Creates a new role on the server with default settings.
Sends the roles.add mutation, then fetches all roles via roles.getAll to hydrate the new role into the local cache. The returned Role model is ready for use and can be immediately chained with edit() to set the name, colour, and permissions.
Requires the MANAGE_ROLES permission.
Returns \PromiseInterface — Resolves with the new Role model, rejects on failure.
Example
$sharkord->roles->add()->then(function(\Sharkord\Models\Role $role) { return $role->edit( 'Support', '#9b59b6', \Sharkord\Permission::SEND_MESSAGES, \Sharkord\Permission::REACT_TO_MESSAGES, );})->then(function() { echo "Role created and configured.\n";});Retrieves a cached role by ID.
Parameters
| Parameter | Type | Description |
|---|---|---|
$id | `int | string` |
Returns \Role|null — The cached Role model, or null if not found.
Example
$role = $sharkord->roles->get(3);
if ($role) { echo "Role: {$role->name}\n";}fetch()
Section titled “fetch()”Re-fetches all roles from the server and updates the local cache in place.
Existing Role models held by the caller remain valid — each is updated via updateFromArray() rather than replaced. New roles not previously in cache are added automatically.
Returns \PromiseInterface — Resolves with an array<string, Role> keyed by role ID, rejects on failure.
Example
$sharkord->roles->fetch()->then(function(array $roles) { foreach ($roles as $id => $role) { echo "{$id}: {$role->name}\n"; }});collection()
Section titled “collection()”Returns the underlying Roles collection.
Returns \RolesCollection
Example
foreach ($sharkord->roles->collection() as $id => $role) { echo "{$id}: {$role->name}\n";}