ceejay.roles
List the roles of your Discord server, assign a role to a member, or remove one.
Every function takes the same last argument, options: signal, timeoutMs and
maxRetries, for that one call. A write also takes idempotencyKey there. Every function
can also raise the errors any request can raise, such as a bad key or the rate limit. See
Errors and retries.
Which roles can be changed
Only an ordinary role of your own server. CEEJAY refuses, with the code
role_not_allowed:
- the
@everyonerole; - a role that Discord or another app manages, such as a bot's own role;
- a role that can moderate or manage the server;
- a role that can mention everyone, in a server whose
@everyonerole cannot. Where@everyonecan, every member already has that permission, the role adds nothing, and it is not refused; - a bot master role: a role your community added under Settings → General → Team Management, whose members have full access to the CEEJAY dashboard, whatever its Discord permissions are.
ceejay.roles.list shows for every role whether it can be changed, and the reason if it cannot.
The CEEJAY bot needs Manage Roles, and its own role must sit above the role you change in Server Settings → Roles.
All three requests go through Discord, which has its own rate limit. Do not retry them in a tight loop.
ceejay.roles.list
Lists your server's roles, highest first, without @everyone, and shows for each one
whether ceejay.roles.grant and ceejay.roles.revoke accept it.
GET /v1/roles · requires roles:manage · API reference: List roles
Retried by the SDK: Yes. It is a read
Parameters: options only.
Returns the full response, typed as Schemas['ListRolesResponse']. The roles are in
data, and the pagination details are in page.
Prop
Type
Each role, typed as Schemas['Role']:
Prop
Type
notGrantableReason is one of five values:
| Value | What it means |
|---|---|
managed | Discord or another app manages the role |
can_moderate | The role can moderate or manage the server |
can_mention_everyone | The role can mention everyone, and your server's @everyone role cannot |
dashboard_access | The role is a bot master role of your community |
above_bot | The role sits at or above the bot's own highest role, so Discord would refuse the change |
above_bot is reported when CEEJAY could read the bot's own role. When it could not, the
role reads as grantable and Discord may still refuse it. A role that moderates only
through the settings of single channels cannot be seen from the list either.
CEEJAY reads the list from Discord and caches it for up to a minute. The response never includes a role's permissions or which members have it.
Errors
A list that could not be read is an error, never an empty list.
| Status | code | When |
|---|---|---|
429 | discord_rate_limited | Discord is limiting the bot. retryAfter is the number of seconds Discord asked you to wait, if it provided one |
500 | discord_not_configured | Discord is not configured on this instance |
503 | roles_unavailable | The server's roles could not be read from Discord. Retry with a pause between attempts |
503 | dashboard_roles_unavailable | CEEJAY could not read which roles are your community's bot master roles, so no role is listed: one of them would otherwise read as grantable |
Example
const roles = await ceejay.roles.list()
for (const role of roles.data) {
console.log(role.roleId, role.name, role.grantable ? 'can be given' : `not given: ${role.notGrantableReason}`)
}ceejay.roles.grant
Assigns a Discord role to a member. A member who already has it is not an error.
POST /v1/roles/grant · requires roles:manage · API reference: Grant a role
Retried by the SDK: Only with idempotencyKey
Parameters: one object, then options.
Prop
Type
Options
Prop
Type
Returns Schemas['GrantRoleResult'].
Prop
Type
Errors
| Status | code | When |
|---|---|---|
400 | bad_request | An ID is not valid |
403 | role_not_allowed | The role is one the API never changes. See Which roles can be changed |
403 | bot_missing_permission | The bot lacks the permission to manage this role, or its own role sits below it |
404 | role_not_found | The role is not one of your server's |
404 | member_not_found | Discord found no such member in your server |
429 | discord_rate_limited | Discord is limiting the bot. retryAfter is the number of seconds Discord asked you to wait, if it provided one |
500 | discord_not_configured | Discord is not configured on this instance |
503 | discord_unavailable | Discord did not respond to the change within 10 seconds. The change may or may not have been made. Retry the request with the same idempotency key to find out |
503 | roles_unavailable | CEEJAY could not read your server's roles from Discord. Nothing was changed, so you can retry |
503 | dashboard_roles_unavailable | CEEJAY could not read which roles are your community's bot master roles, so it cannot tell whether this one may be assigned. Nothing was changed, so you can retry |
Any other status that Discord returns is passed on with the code discord_error. The
message is then Discord API error: followed by that status, and error.body includes
Discord's own response in details. Idempotency errors are covered in
Errors and retries.
Example
import { CeejayError } from '@communitystudios/ceejay'
try {
const result = await ceejay.roles.grant({
discordId: '409876543210987654',
roleId: '718230049827100399',
})
console.log(result.action)
} catch (error) {
if (error instanceof CeejayError && error.code === 'role_not_allowed') {
console.log('The API never gives this role. Choose another one.')
} else {
throw error
}
}ceejay.roles.revoke
Revokes a Discord role from a member. A member who does not have it is not an error.
POST /v1/roles/revoke · requires roles:manage · API reference: Revoke a role
Retried by the SDK: Only with idempotencyKey
Parameters: one object, then options.
Prop
Type
Options
Prop
Type
Returns Schemas['RevokeRoleResult'].
Prop
Type
Errors
| Status | code | When |
|---|---|---|
400 | bad_request | An ID is not valid |
403 | role_not_allowed | The role is one the API never changes. See Which roles can be changed |
403 | bot_missing_permission | The bot lacks the permission to manage this role, or its own role sits below it |
404 | role_not_found | The role is not one of your server's |
404 | member_not_found | Discord found no such member in your server |
429 | discord_rate_limited | Discord is limiting the bot. retryAfter is the number of seconds Discord asked you to wait, if it provided one |
500 | discord_not_configured | Discord is not configured on this instance |
503 | discord_unavailable | Discord did not respond to the change within 10 seconds. The change may or may not have been made. Retry the request with the same idempotency key to find out |
503 | roles_unavailable | CEEJAY could not read your server's roles from Discord. Nothing was changed, so you can retry |
503 | dashboard_roles_unavailable | CEEJAY could not read which roles are your community's bot master roles, so it cannot tell whether this one may be revoked. Nothing was changed, so you can retry |
Any other status that Discord returns is passed on with the code discord_error, as for
ceejay.roles.grant.
Example
const result = await ceejay.roles.revoke({
discordId: '409876543210987654',
roleId: '718230049827100399',
})
console.log(result.action)
