RoleManager
classe · Source
Manages API methods for roles and stores their cache.
Extends: CachedManager
Properties
guild
The guild belonging to this manager Source
cache
The role cache of this manager Source
everyone
The @everyone role of the guild Source
premiumSubscriberRole
The premium subscriber role of the guild, if any Source
highest
The role with the highest position in the cache Source
_cache
The private cache of items for this manager. Source
holds
The data structure belonging to this manager. Source
client
The client that instantiated this Manager Source
Methods
fetch
Obtains a role from Discord, or the role cache if they're already available. Parameters
| Name | Type | Description |
|---|---|---|
id? | Snowflake | The role's id |
options? | BaseFetchOptions | Additional options for this fetch |
Returns: Promise<(?Role\|Collection<Snowflake, Role>)>
// Fetch all roles from the guild
message.guild.roles.fetch()
.then(roles => console.log(`There are ${roles.size} roles.`))
.catch(console.error);// Fetch a single role
message.guild.roles.fetch('222078108977594368')
.then(role => console.log(`The role color is: ${role.colors.primaryColor}`))
.catch(console.error);fetchMemberCounts
Fetches the member count of each role in the guild. This does not include the @everyone role. Returns: Promise<Collection<Snowflake, number>>Source
fetchMemberIds
Fetches the member ids for a role in the guild. This only returns 100 member ids Parameters
| Name | Type | Description |
|---|---|---|
role | RoleResolvable | The role to fetch member ids for |
Returns: Promise<Array<Snowflake>>Source
resolve
Resolves a RoleResolvable to a Role object. Parameters
| Name | Type | Description |
|---|---|---|
role | RoleResolvable | The role resolvable to resolve |
Returns: RoleSource
resolveId
Resolves a RoleResolvable to a Role id. Parameters
| Name | Type | Description |
|---|---|---|
role | RoleResolvable | The role resolvable to resolve |
Returns: SnowflakeSource
create
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
| Name | Type | Description |
|---|---|---|
options? | CreateRoleOptions | Options for creating the new role |
Returns: Promise<Role>
// Create a new role
guild.roles.create()
.then(console.log)
.catch(console.error);// 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);// 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);edit
Edits a role of the guild. Parameters
| Name | Type | Description |
|---|---|---|
role | RoleResolvable | The role to edit |
data | RoleData | The new data for the role |
reason? | string | Reason for editing this role |
Returns: Promise<Role>
// Edit a role
guild.roles.edit('222079219327434752', { name: 'buddies' })
.then(updated => console.log(`Edited role name to ${updated.name}`))
.catch(console.error);delete
Deletes a role. Parameters
| Name | Type | Description |
|---|---|---|
role | RoleResolvable | The role to delete |
reason? | string | Reason for deleting the role |
Returns: Promise<void>
// Delete a role
guild.roles.delete('222079219327434752', 'The role needed to go')
.then(() => console.log('Deleted the role.'))
.catch(console.error);setPosition
Sets the new position of the role. Parameters
| Name | Type | Description |
|---|---|---|
role | RoleResolvable | The role to change the position of |
position | number | The new position for the role |
options? | SetRolePositionOptions | Options for setting the position |
Returns: Promise<Role>
// Set the position of the role
guild.roles.setPosition('222197033908436994', 1)
.then(updated => console.log(`Role position: ${updated.position}`))
.catch(console.error);setPositions
Batch-updates the guild's role positions Parameters
| Name | Type | Description |
|---|---|---|
rolePositions | Array<GuildRolePosition> | Role positions to update |
Returns: Promise<Guild>
guild.roles.setPositions([{ role: roleId, position: updatedRoleIndex }])
.then(guild => console.log(`Role positions updated for ${guild}`))
.catch(console.error);comparePositions
Compares the positions of two roles. Parameters
| Name | Type | Description |
|---|---|---|
role1 | RoleResolvable | First role to compare |
role2 | RoleResolvable | Second 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
Gets the managed role a user created when joining the guild, if any Only ever available for bots Parameters
| Name | Type | Description |
|---|---|---|
user | UserResolvable | The user to access the bot role for |
Returns: RoleSource