Skip to content

RoleManager

classe · Source

Manages API methods for roles and stores their cache.

Extends: CachedManager

Properties

guild

guild: Guild

The guild belonging to this manager Source

cache

cache: Collection<Snowflake, Role>

The role cache of this manager Source

everyone

readonly

everyone: Role

The @everyone role of the guild Source

premiumSubscriberRole

readonly · nullable

premiumSubscriberRole: Role

The premium subscriber role of the guild, if any Source

highest

readonly

highest: Role

The role with the highest position in the cache Source

_cache

readonly · private

_cache: Collection

The private cache of items for this manager. Source

holds

readonly · private

holds: function

The data structure belonging to this manager. Source

client

readonly

client: Client

The client that instantiated this Manager Source

Methods

fetch

async

async fetch(id?: Snowflake, options?: BaseFetchOptions): Promise<(?Role|Collection<Snowflake, Role>)>

Obtains a role from Discord, or the role cache if they're already available. Parameters

NameTypeDescription
id?SnowflakeThe role's id
options?BaseFetchOptionsAdditional options for this fetch

Returns: Promise<(?Role\|Collection<Snowflake, Role>)>

js
// Fetch all roles from the guild
message.guild.roles.fetch()
  .then(roles => console.log(`There are ${roles.size} roles.`))
  .catch(console.error);
js
// Fetch a single role
message.guild.roles.fetch('222078108977594368')
  .then(role => console.log(`The role color is: ${role.colors.primaryColor}`))
  .catch(console.error);

Source

fetchMemberCounts

async

async fetchMemberCounts(): Promise<Collection<Snowflake, number>>

Fetches the member count of each role in the guild. This does not include the @everyone role. Returns: Promise<Collection<Snowflake, number>>Source

fetchMemberIds

async

async fetchMemberIds(role: RoleResolvable): Promise<Array<Snowflake>>

Fetches the member ids for a role in the guild. This only returns 100 member ids Parameters

NameTypeDescription
roleRoleResolvableThe role to fetch member ids for

Returns: Promise<Array<Snowflake>>Source

resolve

resolve(role: RoleResolvable): Role

Resolves a RoleResolvable to a Role object. Parameters

NameTypeDescription
roleRoleResolvableThe role resolvable to resolve

Returns: RoleSource

resolveId

resolveId(role: RoleResolvable): Snowflake

Resolves a RoleResolvable to a Role id. Parameters

NameTypeDescription
roleRoleResolvableThe role resolvable to resolve

Returns: SnowflakeSource

create

async

async create(options?: CreateRoleOptions): Promise<Role>

Creates a new role in the guild with given information. The position will silently reset to 1 if an invalid one is provided, or none. Parameters

NameTypeDescription
options?CreateRoleOptionsOptions for creating the new role

Returns: Promise<Role>

js
// Create a new role
guild.roles.create()
  .then(console.log)
  .catch(console.error);
js
// Create a new role with data and a reason
guild.roles.create({
  name: 'Super Cool Blue People',
  colors: {
    primaryColor: 'BLUE',
  },
  reason: 'we needed a role for Super Cool People',
})
  .then(console.log)
  .catch(console.error);
js
// Create a role with holographic colors
guild.roles.create({
  name: 'Holographic Role',
  reason: 'Creating a role with holographic effect',
  colors: {
    primaryColor: Constants.HolographicStyles.PRIMARY,
    secondaryColor: Constants.HolographicStyles.SECONDARY,
    tertiaryColor: Constants.HolographicStyles.TERTIARY,
  },
})
  .then(console.log)
  .catch(console.error);

Source

edit

async

async edit(role: RoleResolvable, data: RoleData, reason?: string): Promise<Role>

Edits a role of the guild. Parameters

NameTypeDescription
roleRoleResolvableThe role to edit
dataRoleDataThe new data for the role
reason?stringReason for editing this role

Returns: Promise<Role>

js
// Edit a role
guild.roles.edit('222079219327434752', { name: 'buddies' })
  .then(updated => console.log(`Edited role name to ${updated.name}`))
  .catch(console.error);

Source

delete

async

async delete(role: RoleResolvable, reason?: string): Promise<void>

Deletes a role. Parameters

NameTypeDescription
roleRoleResolvableThe role to delete
reason?stringReason for deleting the role

Returns: Promise<void>

js
// Delete a role
guild.roles.delete('222079219327434752', 'The role needed to go')
  .then(() => console.log('Deleted the role.'))
  .catch(console.error);

Source

setPosition

async

async setPosition(role: RoleResolvable, position: number, options?: SetRolePositionOptions): Promise<Role>

Sets the new position of the role. Parameters

NameTypeDescription
roleRoleResolvableThe role to change the position of
positionnumberThe new position for the role
options?SetRolePositionOptionsOptions for setting the position

Returns: Promise<Role>

js
// Set the position of the role
guild.roles.setPosition('222197033908436994', 1)
  .then(updated => console.log(`Role position: ${updated.position}`))
  .catch(console.error);

Source

setPositions

async

async setPositions(rolePositions: Array<GuildRolePosition>): Promise<Guild>

Batch-updates the guild's role positions Parameters

NameTypeDescription
rolePositionsArray<GuildRolePosition>Role positions to update

Returns: Promise<Guild>

js
guild.roles.setPositions([{ role: roleId, position: updatedRoleIndex }])
 .then(guild => console.log(`Role positions updated for ${guild}`))
 .catch(console.error);

Source

comparePositions

comparePositions(role1: RoleResolvable, role2: RoleResolvable): number

Compares the positions of two roles. Parameters

NameTypeDescription
role1RoleResolvableFirst role to compare
role2RoleResolvableSecond role to compare

Returns: number — Negative number if the first role's position is lower (second role's is higher), positive number if the first's is higher (second's is lower), 0 if equal Source

botRoleFor

botRoleFor(user: UserResolvable): Role

Gets the managed role a user created when joining the guild, if any Only ever available for bots Parameters

NameTypeDescription
userUserResolvableThe user to access the bot role for

Returns: RoleSource

Unofficial software. Not affiliated with or supported by Discord.