mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 17:43:30 +01:00
refactor: move member adding to manager (#6231)
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com> Co-authored-by: Antonio Román <kyradiscord@gmail.com> Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
This commit is contained in:
@@ -66,6 +66,56 @@ class GuildMemberManager extends CachedManager {
|
||||
return this.cache.has(userResolvable) ? userResolvable : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options used to add a user to a guild using OAuth2.
|
||||
* @typedef {Object} AddGuildMemberOptions
|
||||
* @property {string} accessToken An OAuth2 access token for the user with the `guilds.join` scope granted to the
|
||||
* bot's application
|
||||
* @property {string} [nick] The nickname to give to the member (requires `MANAGE_NICKNAMES`)
|
||||
* @property {Collection<Snowflake, Role>|RoleResolvable[]} [roles] The roles to add to the member
|
||||
* (requires `MANAGE_ROLES`)
|
||||
* @property {boolean} [mute] Whether the member should be muted (requires `MUTE_MEMBERS`)
|
||||
* @property {boolean} [deaf] Whether the member should be deafened (requires `DEAFEN_MEMBERS`)
|
||||
* @property {boolean} [force] Whehter to skip the cache check and call the API directly
|
||||
* @property {boolean} [fetchWhenExisting=true] Whether to fetch the user if not cached and already a member
|
||||
*/
|
||||
|
||||
/**
|
||||
* Adds a user to the guild using OAuth2. Requires the `CREATE_INSTANT_INVITE` permission.
|
||||
* @param {UserResolvable} user The user to add to the guild
|
||||
* @param {AddGuildMemberOptions} options Options for adding the user to the guild
|
||||
* @returns {Promise<GuildMember|null>}
|
||||
*/
|
||||
async add(user, options) {
|
||||
const userId = this.client.users.resolveId(user);
|
||||
if (!userId) throw new TypeError('INVALID_TYPE', 'user', 'UserResolvable');
|
||||
if (!options.force) {
|
||||
const cachedUser = this.cache.get(userId);
|
||||
if (cachedUser) return cachedUser;
|
||||
}
|
||||
const resolvedOptions = {
|
||||
access_token: options.accessToken,
|
||||
nick: options.nick,
|
||||
mute: options.mute,
|
||||
deaf: options.deaf,
|
||||
};
|
||||
if (options.roles) {
|
||||
if (!Array.isArray(options.roles) && !(options.roles instanceof Collection)) {
|
||||
throw new TypeError('INVALID_TYPE', 'options.roles', 'Array or Collection of Roles or Snowflakes', true);
|
||||
}
|
||||
const resolvedRoles = [];
|
||||
for (const role of options.roles.values()) {
|
||||
const resolvedRole = this.guild.roles.resolveId(role);
|
||||
if (!resolvedRole) throw new TypeError('INVALID_ELEMENT', 'Array or Collection', 'options.roles', role);
|
||||
resolvedRoles.push(resolvedRole);
|
||||
}
|
||||
resolvedOptions.roles = resolvedRoles;
|
||||
}
|
||||
const data = await this.client.api.guilds(this.guild.id).members(userId).put({ data: resolvedOptions });
|
||||
// Data is an empty buffer if the member is already part of the guild.
|
||||
return data instanceof Buffer ? (options.fetchWhenExisting === false ? null : this.fetch(userId)) : this._add(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Options used to fetch a single member from a guild.
|
||||
* @typedef {BaseFetchOptions} FetchMemberOptions
|
||||
|
||||
@@ -8,7 +8,7 @@ const GuildTemplate = require('./GuildTemplate');
|
||||
const Integration = require('./Integration');
|
||||
const Webhook = require('./Webhook');
|
||||
const WelcomeScreen = require('./WelcomeScreen');
|
||||
const { Error, TypeError } = require('../errors');
|
||||
const { Error } = require('../errors');
|
||||
const GuildApplicationCommandManager = require('../managers/GuildApplicationCommandManager');
|
||||
const GuildBanManager = require('../managers/GuildBanManager');
|
||||
const GuildChannelManager = require('../managers/GuildChannelManager');
|
||||
@@ -730,46 +730,6 @@ class Guild extends AnonymousGuild {
|
||||
.then(data => GuildAuditLogs.build(this, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Options used to add a user to a guild using OAuth2.
|
||||
* @typedef {Object} AddGuildMemberOptions
|
||||
* @property {string} accessToken An OAuth2 access token for the user with the `guilds.join` scope granted to the
|
||||
* bot's application
|
||||
* @property {string} [nick] The nickname to give to the member (requires `MANAGE_NICKNAMES`)
|
||||
* @property {Collection<Snowflake, Role>|RoleResolvable[]} [roles] The roles to add to the member
|
||||
* (requires `MANAGE_ROLES`)
|
||||
* @property {boolean} [mute] Whether the member should be muted (requires `MUTE_MEMBERS`)
|
||||
* @property {boolean} [deaf] Whether the member should be deafened (requires `DEAFEN_MEMBERS`)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Adds a user to the guild using OAuth2. Requires the `CREATE_INSTANT_INVITE` permission.
|
||||
* @param {UserResolvable} user The user to add to the guild
|
||||
* @param {AddGuildMemberOptions} options Options for adding the user to the guild
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
async addMember(user, options) {
|
||||
user = this.client.users.resolveId(user);
|
||||
if (!user) throw new TypeError('INVALID_TYPE', 'user', 'UserResolvable');
|
||||
if (this.members.cache.has(user)) return this.members.cache.get(user);
|
||||
options.access_token = options.accessToken;
|
||||
if (options.roles) {
|
||||
if (!Array.isArray(options.roles) && !(options.roles instanceof Collection)) {
|
||||
throw new TypeError('INVALID_TYPE', 'options.roles', 'Array or Collection of Roles or Snowflakes', true);
|
||||
}
|
||||
const resolvedRoles = [];
|
||||
for (const role of options.roles.values()) {
|
||||
const resolvedRole = this.roles.resolveId(role);
|
||||
if (!role) throw new TypeError('INVALID_ELEMENT', 'Array or Collection', 'options.roles', role);
|
||||
resolvedRoles.push(resolvedRole);
|
||||
}
|
||||
options.roles = resolvedRoles;
|
||||
}
|
||||
const data = await this.client.api.guilds(this.id).members(user).put({ data: options });
|
||||
// Data is an empty buffer if the member is already part of the guild.
|
||||
return data instanceof Buffer ? this.members.fetch(user) : this.members._add(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* The data for editing a guild.
|
||||
* @typedef {Object} GuildEditData
|
||||
|
||||
Reference in New Issue
Block a user