mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
feat(GuildMemberManager): add addRole and removeRole methods (#9108)
* feat: addrole and removerole * fix: apply suggestions * Apply suggestions from code review Co-authored-by: space <spaceeec@yahoo.com> * fix: missdeleted ban :( --------- Co-authored-by: space <spaceeec@yahoo.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable newline-per-chained-call */
|
||||
'use strict';
|
||||
|
||||
const { Buffer } = require('node:buffer');
|
||||
@@ -436,6 +437,38 @@ class GuildMemberManager extends CachedManager {
|
||||
return this._add(data, cache);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = false,
|
||||
|
||||
Reference in New Issue
Block a user