feat(GuildMemberManager): add addRole and removeRole methods

#9108 djs
This commit is contained in:
Elysia 2023-02-22 12:01:29 +07:00
parent d908926ffb
commit e4013237d6
3 changed files with 39 additions and 1 deletions

View File

@ -186,7 +186,7 @@
"newline-per-chained-call": [
"error",
{
"ignoreChainWithDepth": 3
"ignoreChainWithDepth": 4
}
],
"no-array-constructor": "error",

View File

@ -614,6 +614,38 @@ class GuildMemberManager extends CachedManager {
});
}
/**
* Adds a role to a member.
* @param {GuildMemberResolvable} user The user to add the role from
* @param {RoleResolvable} role The role to add
* @param {string} [reason] Reason for adding the role
* @returns {Promise<GuildMember|User|Snowflake>}
*/
async addRole(user, role, reason) {
const userId = this.guild.members.resolveId(user);
const roleId = this.guild.roles.resolveId(role);
await this.client.api.guilds(this.guild.id).members(userId).roles(roleId).put({ reason });
return this.resolve(user) ?? this.client.users.resolve(user) ?? userId;
}
/**
* Removes a role from a member.
* @param {UserResolvable} user The user to remove the role from
* @param {RoleResolvable} role The role to remove
* @param {string} [reason] Reason for removing the role
* @returns {Promise<GuildMember|User|Snowflake>}
*/
async removeRole(user, role, reason) {
const userId = this.guild.members.resolveId(user);
const roleId = this.guild.roles.resolveId(role);
await this.client.api.guilds(this.guild.id).members(userId).roles(roleId).delete({ reason });
return this.resolve(user) ?? this.client.users.resolve(user) ?? userId;
}
_fetchMany({
limit = 0,
withPresences: presences = true,

6
typings/index.d.ts vendored
View File

@ -4022,6 +4022,12 @@ export class GuildMemberManager extends CachedManager<Snowflake, GuildMember, Gu
public prune(options?: GuildPruneMembersOptions): Promise<number>;
public search(options: GuildSearchMembersOptions): Promise<Collection<Snowflake, GuildMember>>;
public unban(user: UserResolvable, reason?: string): Promise<User | null>;
public addRole(user: UserResolvable, role: RoleResolvable, reason?: string): Promise<GuildMember | User | Snowflake>;
public removeRole(
user: UserResolvable,
role: RoleResolvable,
reason?: string,
): Promise<GuildMember | User | Snowflake>;
}
export class GuildBanManager extends CachedManager<Snowflake, GuildBan, GuildBanResolvable> {