CEEJAYDocs

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 @everyone role;
  • 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 @everyone role cannot. Where @everyone can, 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:

ValueWhat it means
managedDiscord or another app manages the role
can_moderateThe role can moderate or manage the server
can_mention_everyoneThe role can mention everyone, and your server's @everyone role cannot
dashboard_accessThe role is a bot master role of your community
above_botThe 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.

StatuscodeWhen
429discord_rate_limitedDiscord is limiting the bot. retryAfter is the number of seconds Discord asked you to wait, if it provided one
500discord_not_configuredDiscord is not configured on this instance
503roles_unavailableThe server's roles could not be read from Discord. Retry with a pause between attempts
503dashboard_roles_unavailableCEEJAY 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

ceejay.roles.list
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

StatuscodeWhen
400bad_requestAn ID is not valid
403role_not_allowedThe role is one the API never changes. See Which roles can be changed
403bot_missing_permissionThe bot lacks the permission to manage this role, or its own role sits below it
404role_not_foundThe role is not one of your server's
404member_not_foundDiscord found no such member in your server
429discord_rate_limitedDiscord is limiting the bot. retryAfter is the number of seconds Discord asked you to wait, if it provided one
500discord_not_configuredDiscord is not configured on this instance
503discord_unavailableDiscord 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
503roles_unavailableCEEJAY could not read your server's roles from Discord. Nothing was changed, so you can retry
503dashboard_roles_unavailableCEEJAY 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

ceejay.roles.grant
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

StatuscodeWhen
400bad_requestAn ID is not valid
403role_not_allowedThe role is one the API never changes. See Which roles can be changed
403bot_missing_permissionThe bot lacks the permission to manage this role, or its own role sits below it
404role_not_foundThe role is not one of your server's
404member_not_foundDiscord found no such member in your server
429discord_rate_limitedDiscord is limiting the bot. retryAfter is the number of seconds Discord asked you to wait, if it provided one
500discord_not_configuredDiscord is not configured on this instance
503discord_unavailableDiscord 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
503roles_unavailableCEEJAY could not read your server's roles from Discord. Nothing was changed, so you can retry
503dashboard_roles_unavailableCEEJAY 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

ceejay.roles.revoke
const result = await ceejay.roles.revoke({
  discordId: '409876543210987654',
  roleId: '718230049827100399',
})

console.log(result.action)

On this page

Community StudiosPrivacyTerms