mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
feat(RoleManager): added edit method, alias Role#edit (#5983)
Co-authored-by: monbrey <rsm999@uowmail.edu.au>
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const BaseManager = require('./BaseManager');
|
const BaseManager = require('./BaseManager');
|
||||||
|
const { TypeError } = require('../errors');
|
||||||
const Role = require('../structures/Role');
|
const Role = require('../structures/Role');
|
||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
const Permissions = require('../util/Permissions');
|
const Permissions = require('../util/Permissions');
|
||||||
const { resolveColor } = require('../util/Util');
|
const { resolveColor, setPosition } = require('../util/Util');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages API methods for roles and stores their cache.
|
* Manages API methods for roles and stores their cache.
|
||||||
@@ -143,6 +144,53 @@ class RoleManager extends BaseManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edits a role of the guild.
|
||||||
|
* @param {RoleResolvable} role The role to edit
|
||||||
|
* @param {RoleData} data The new data for the role
|
||||||
|
* @param {string} [reason] Reason for editing this role
|
||||||
|
* @returns {Promise<Role>}
|
||||||
|
* @example
|
||||||
|
* // Edit a role
|
||||||
|
* guild.roles.edit('222079219327434752', { name: 'buddies' })
|
||||||
|
* .then(updated => console.log(`Edited role name to ${updated.name}`))
|
||||||
|
* .catch(console.error);
|
||||||
|
*/
|
||||||
|
async edit(role, data, reason) {
|
||||||
|
role = this.resolve(role);
|
||||||
|
if (!role) throw new TypeError('INVALID_TYPE', 'role', 'RoleResolvable');
|
||||||
|
|
||||||
|
if (typeof data.position === 'number') {
|
||||||
|
const updatedRoles = await setPosition(
|
||||||
|
role,
|
||||||
|
data.position,
|
||||||
|
false,
|
||||||
|
this.guild._sortedRoles(),
|
||||||
|
this.client.api.guilds(this.guild.id).roles,
|
||||||
|
reason,
|
||||||
|
);
|
||||||
|
|
||||||
|
this.client.actions.GuildRolesPositionUpdate.handle({
|
||||||
|
guild_id: this.guild.id,
|
||||||
|
roles: updatedRoles,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const _data = {
|
||||||
|
name: data.name,
|
||||||
|
color: typeof data.color === 'undefined' ? undefined : resolveColor(data.color),
|
||||||
|
hoist: data.hoist,
|
||||||
|
permissions: typeof data.permissions === 'undefined' ? undefined : new Permissions(data.permissions),
|
||||||
|
mentionable: data.mentionable,
|
||||||
|
};
|
||||||
|
|
||||||
|
const d = await this.client.api.guilds(this.guild.id).roles(role.id).patch({ data: _data, reason });
|
||||||
|
|
||||||
|
const clone = role._clone();
|
||||||
|
clone._patch(d);
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the managed role a user created when joining the guild, if any
|
* Gets the managed role a user created when joining the guild, if any
|
||||||
* <info>Only ever available for bots</info>
|
* <info>Only ever available for bots</info>
|
||||||
|
|||||||
@@ -196,38 +196,8 @@ class Role extends Base {
|
|||||||
* .then(updated => console.log(`Edited role name to ${updated.name}`))
|
* .then(updated => console.log(`Edited role name to ${updated.name}`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async edit(data, reason) {
|
edit(data, reason) {
|
||||||
if (typeof data.position !== 'undefined') {
|
return this.guild.roles.edit(this, data, reason);
|
||||||
await Util.setPosition(
|
|
||||||
this,
|
|
||||||
data.position,
|
|
||||||
false,
|
|
||||||
this.guild._sortedRoles(),
|
|
||||||
this.client.api.guilds(this.guild.id).roles,
|
|
||||||
reason,
|
|
||||||
).then(updatedRoles => {
|
|
||||||
this.client.actions.GuildRolesPositionUpdate.handle({
|
|
||||||
guild_id: this.guild.id,
|
|
||||||
roles: updatedRoles,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return this.client.api.guilds[this.guild.id].roles[this.id]
|
|
||||||
.patch({
|
|
||||||
data: {
|
|
||||||
name: data.name ?? this.name,
|
|
||||||
color: data.color !== null ? Util.resolveColor(data.color ?? this.color) : null,
|
|
||||||
hoist: data.hoist ?? this.hoist,
|
|
||||||
permissions: typeof data.permissions !== 'undefined' ? new Permissions(data.permissions) : this.permissions,
|
|
||||||
mentionable: data.mentionable ?? this.mentionable,
|
|
||||||
},
|
|
||||||
reason,
|
|
||||||
})
|
|
||||||
.then(role => {
|
|
||||||
const clone = this._clone();
|
|
||||||
clone._patch(role);
|
|
||||||
return clone;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
3
typings/index.d.ts
vendored
3
typings/index.d.ts
vendored
@@ -2633,6 +2633,7 @@ declare module 'discord.js' {
|
|||||||
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<Role | null>;
|
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<Role | null>;
|
||||||
public fetch(id?: Snowflake, options?: BaseFetchOptions): Promise<Collection<Snowflake, Role>>;
|
public fetch(id?: Snowflake, options?: BaseFetchOptions): Promise<Collection<Snowflake, Role>>;
|
||||||
public create(options?: CreateRoleOptions): Promise<Role>;
|
public create(options?: CreateRoleOptions): Promise<Role>;
|
||||||
|
public edit(role: RoleResolvable, options: RoleData, reason?: string): Promise<Role>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class StageInstanceManager extends BaseManager<Snowflake, StageInstance, StageInstanceResolvable> {
|
export class StageInstanceManager extends BaseManager<Snowflake, StageInstance, StageInstanceResolvable> {
|
||||||
@@ -4212,7 +4213,7 @@ declare module 'discord.js' {
|
|||||||
position: number;
|
position: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
type RoleResolvable = Role | string;
|
type RoleResolvable = Role | Snowflake;
|
||||||
|
|
||||||
interface RoleTagData {
|
interface RoleTagData {
|
||||||
botID?: Snowflake;
|
botID?: Snowflake;
|
||||||
|
|||||||
Reference in New Issue
Block a user