feat: move me to GuildMemberManager manager (#7669)

Co-authored-by: muchnameless <12682826+muchnameless@users.noreply.github.com>
Co-authored-by: Almeida <almeidx@pm.me>
This commit is contained in:
Synbulat Biishev
2022-05-13 01:51:28 +05:00
committed by GitHub
parent e07b910e68
commit aed687b09f
14 changed files with 48 additions and 38 deletions

View File

@@ -153,7 +153,7 @@ class GuildEmojiManager extends BaseGuildEmojiManager {
throw new Error('EMOJI_MANAGED'); throw new Error('EMOJI_MANAGED');
} }
const { me } = this.guild; const { me } = this.guild.members;
if (!me) throw new Error('GUILD_UNCACHED_ME'); if (!me) throw new Error('GUILD_UNCACHED_ME');
if (!me.permissions.has(PermissionFlagsBits.ManageEmojisAndStickers)) { if (!me.permissions.has(PermissionFlagsBits.ManageEmojisAndStickers)) {
throw new Error('MISSING_MANAGE_EMOJIS_AND_STICKERS_PERMISSION', this.guild); throw new Error('MISSING_MANAGE_EMOJIS_AND_STICKERS_PERMISSION', this.guild);

View File

@@ -12,6 +12,7 @@ const BaseGuildVoiceChannel = require('../structures/BaseGuildVoiceChannel');
const { GuildMember } = require('../structures/GuildMember'); const { GuildMember } = require('../structures/GuildMember');
const { Role } = require('../structures/Role'); const { Role } = require('../structures/Role');
const Events = require('../util/Events'); const Events = require('../util/Events');
const Partials = require('../util/Partials');
/** /**
* Manages API methods for GuildMembers and stores their cache. * Manages API methods for GuildMembers and stores their cache.
@@ -120,6 +121,20 @@ class GuildMemberManager extends CachedManager {
return data instanceof Buffer ? (options.fetchWhenExisting === false ? null : this.fetch(userId)) : this._add(data); return data instanceof Buffer ? (options.fetchWhenExisting === false ? null : this.fetch(userId)) : this._add(data);
} }
/**
* The client user as a GuildMember of this guild
* @type {?GuildMember}
* @readonly
*/
get me() {
return (
this.resolve(this.client.user.id) ??
(this.client.options.partials.includes(Partials.GuildMember)
? this._add({ user: { id: this.client.user.id } }, true)
: null)
);
}
/** /**
* Options used to fetch a single member from a guild. * Options used to fetch a single member from a guild.
* @typedef {BaseFetchOptions} FetchMemberOptions * @typedef {BaseFetchOptions} FetchMemberOptions

View File

@@ -37,6 +37,15 @@ class ThreadMemberManager extends CachedManager {
return member; return member;
} }
/**
* The client user as a ThreadMember of this ThreadChannel
* @type {?ThreadMember}
* @readonly
*/
get me() {
return this.resolve(this.client.user.id);
}
/** /**
* Data that resolves to give a ThreadMember object. This can be: * Data that resolves to give a ThreadMember object. This can be:
* * A ThreadMember object * * A ThreadMember object

View File

@@ -75,7 +75,7 @@ class BaseGuildVoiceChannel extends GuildChannel {
if (permissions.has(PermissionFlagsBits.Administrator, false)) return true; if (permissions.has(PermissionFlagsBits.Administrator, false)) return true;
return ( return (
this.guild.me.communicationDisabledUntilTimestamp < Date.now() && this.guild.members.me.communicationDisabledUntilTimestamp < Date.now() &&
permissions.has(PermissionFlagsBits.Connect, false) permissions.has(PermissionFlagsBits.Connect, false)
); );
} }

View File

@@ -25,7 +25,6 @@ const RoleManager = require('../managers/RoleManager');
const StageInstanceManager = require('../managers/StageInstanceManager'); const StageInstanceManager = require('../managers/StageInstanceManager');
const VoiceStateManager = require('../managers/VoiceStateManager'); const VoiceStateManager = require('../managers/VoiceStateManager');
const DataResolver = require('../util/DataResolver'); const DataResolver = require('../util/DataResolver');
const Partials = require('../util/Partials');
const Status = require('../util/Status'); const Status = require('../util/Status');
const SystemChannelFlagsBitField = require('../util/SystemChannelFlagsBitField'); const SystemChannelFlagsBitField = require('../util/SystemChannelFlagsBitField');
const Util = require('../util/Util'); const Util = require('../util/Util');
@@ -505,20 +504,6 @@ class Guild extends AnonymousGuild {
return this.client.channels.resolve(this.publicUpdatesChannelId); return this.client.channels.resolve(this.publicUpdatesChannelId);
} }
/**
* The client user as a GuildMember of this guild
* @type {?GuildMember}
* @readonly
*/
get me() {
return (
this.members.resolve(this.client.user.id) ??
(this.client.options.partials.includes(Partials.GuildMember)
? this.members._add({ user: { id: this.client.user.id } }, true)
: null)
);
}
/** /**
* The maximum bitrate available for this guild * The maximum bitrate available for this guild
* @type {number} * @type {number}

View File

@@ -416,7 +416,7 @@ class GuildChannel extends Channel {
// This flag allows managing even if timed out // This flag allows managing even if timed out
if (permissions.has(PermissionFlagsBits.Administrator, false)) return true; if (permissions.has(PermissionFlagsBits.Administrator, false)) return true;
if (this.guild.me.communicationDisabledUntilTimestamp > Date.now()) return false; if (this.guild.members.me.communicationDisabledUntilTimestamp > Date.now()) return false;
const bitfield = VoiceBasedChannelTypes.includes(this.type) const bitfield = VoiceBasedChannelTypes.includes(this.type)
? PermissionFlagsBits.ManageChannels | PermissionFlagsBits.Connect ? PermissionFlagsBits.ManageChannels | PermissionFlagsBits.Connect

View File

@@ -55,8 +55,8 @@ class GuildEmoji extends BaseGuildEmoji {
* @readonly * @readonly
*/ */
get deletable() { get deletable() {
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME'); if (!this.guild.members.me) throw new Error('GUILD_UNCACHED_ME');
return !this.managed && this.guild.me.permissions.has(PermissionFlagsBits.ManageEmojisAndStickers); return !this.managed && this.guild.members.me.permissions.has(PermissionFlagsBits.ManageEmojisAndStickers);
} }
/** /**

View File

@@ -239,8 +239,8 @@ class GuildMember extends Base {
if (this.user.id === this.guild.ownerId) return false; if (this.user.id === this.guild.ownerId) return false;
if (this.user.id === this.client.user.id) return false; if (this.user.id === this.client.user.id) return false;
if (this.client.user.id === this.guild.ownerId) return true; if (this.client.user.id === this.guild.ownerId) return true;
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME'); if (!this.guild.members.me) throw new Error('GUILD_UNCACHED_ME');
return this.guild.me.roles.highest.comparePositionTo(this.roles.highest) > 0; return this.guild.members.me.roles.highest.comparePositionTo(this.roles.highest) > 0;
} }
/** /**
@@ -249,8 +249,8 @@ class GuildMember extends Base {
* @readonly * @readonly
*/ */
get kickable() { get kickable() {
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME'); if (!this.guild.members.me) throw new Error('GUILD_UNCACHED_ME');
return this.manageable && this.guild.me.permissions.has(PermissionFlagsBits.KickMembers); return this.manageable && this.guild.members.me.permissions.has(PermissionFlagsBits.KickMembers);
} }
/** /**
@@ -259,8 +259,8 @@ class GuildMember extends Base {
* @readonly * @readonly
*/ */
get bannable() { get bannable() {
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME'); if (!this.guild.members.me) throw new Error('GUILD_UNCACHED_ME');
return this.manageable && this.guild.me.permissions.has(PermissionFlagsBits.BanMembers); return this.manageable && this.guild.members.me.permissions.has(PermissionFlagsBits.BanMembers);
} }
/** /**
@@ -272,7 +272,7 @@ class GuildMember extends Base {
return ( return (
!this.permissions.has(PermissionFlagsBits.Administrator) && !this.permissions.has(PermissionFlagsBits.Administrator) &&
this.manageable && this.manageable &&
(this.guild.me?.permissions.has(PermissionFlagsBits.ModerateMembers) ?? false) (this.guild.members.me?.permissions.has(PermissionFlagsBits.ModerateMembers) ?? false)
); );
} }

View File

@@ -233,10 +233,10 @@ class Invite extends Base {
get deletable() { get deletable() {
const guild = this.guild; const guild = this.guild;
if (!guild || !this.client.guilds.cache.has(guild.id)) return false; if (!guild || !this.client.guilds.cache.has(guild.id)) return false;
if (!guild.me) throw new Error('GUILD_UNCACHED_ME'); if (!guild.members.me) throw new Error('GUILD_UNCACHED_ME');
return Boolean( return Boolean(
this.channel?.permissionsFor(this.client.user).has(PermissionFlagsBits.ManageChannels, false) || this.channel?.permissionsFor(this.client.user).has(PermissionFlagsBits.ManageChannels, false) ||
guild.me.permissions.has(PermissionFlagsBits.ManageGuild), guild.members.me.permissions.has(PermissionFlagsBits.ManageGuild),
); );
} }

View File

@@ -583,7 +583,7 @@ class Message extends Base {
return Boolean( return Boolean(
this.author.id === this.client.user.id || this.author.id === this.client.user.id ||
(permissions.has(PermissionFlagsBits.ManageMessages, false) && (permissions.has(PermissionFlagsBits.ManageMessages, false) &&
this.guild.me.communicationDisabledUntilTimestamp < Date.now()), this.guild.members.me.communicationDisabledUntilTimestamp < Date.now()),
); );
} }

View File

@@ -466,7 +466,7 @@ class ThreadChannel extends Channel {
if (permissions.has(PermissionFlagsBits.Administrator, false)) return true; if (permissions.has(PermissionFlagsBits.Administrator, false)) return true;
return ( return (
this.guild.me.communicationDisabledUntilTimestamp < Date.now() && this.guild.members.me.communicationDisabledUntilTimestamp < Date.now() &&
permissions.has(PermissionFlagsBits.ManageThreads, false) permissions.has(PermissionFlagsBits.ManageThreads, false)
); );
} }
@@ -498,7 +498,7 @@ class ThreadChannel extends Channel {
!(this.archived && this.locked && !this.manageable) && !(this.archived && this.locked && !this.manageable) &&
(this.type !== ChannelType.GuildPrivateThread || this.joined || this.manageable) && (this.type !== ChannelType.GuildPrivateThread || this.joined || this.manageable) &&
permissions.has(PermissionFlagsBits.SendMessagesInThreads, false) && permissions.has(PermissionFlagsBits.SendMessagesInThreads, false) &&
this.guild.me.communicationDisabledUntilTimestamp < Date.now() this.guild.members.me.communicationDisabledUntilTimestamp < Date.now()
); );
} }

View File

@@ -71,7 +71,7 @@ class VoiceChannel extends BaseGuildVoiceChannel {
if (permissions.has(PermissionFlagsBits.Administrator, false)) return true; if (permissions.has(PermissionFlagsBits.Administrator, false)) return true;
return ( return (
this.guild.me.communicationDisabledUntilTimestamp < Date.now() && this.guild.members.me.communicationDisabledUntilTimestamp < Date.now() &&
permissions.has(PermissionFlagsBits.Speak, false) permissions.has(PermissionFlagsBits.Speak, false)
); );
} }

View File

@@ -255,10 +255,10 @@ class VoiceState extends Base {
* @param {boolean} [requestToSpeak=true] Whether or not the client is requesting to become a speaker. * @param {boolean} [requestToSpeak=true] Whether or not the client is requesting to become a speaker.
* @example * @example
* // Making the client request to speak in a stage channel (raise its hand) * // Making the client request to speak in a stage channel (raise its hand)
* guild.me.voice.setRequestToSpeak(true); * guild.members.me.voice.setRequestToSpeak(true);
* @example * @example
* // Making the client cancel a request to speak * // Making the client cancel a request to speak
* guild.me.voice.setRequestToSpeak(false); * guild.members.me.voice.setRequestToSpeak(false);
* @returns {Promise<VoiceState>} * @returns {Promise<VoiceState>}
*/ */
setRequestToSpeak(requestToSpeak = true) { setRequestToSpeak(requestToSpeak = true) {
@@ -270,10 +270,10 @@ class VoiceState extends Base {
* @param {boolean} [suppressed=true] Whether or not the user should be suppressed. * @param {boolean} [suppressed=true] Whether or not the user should be suppressed.
* @example * @example
* // Making the client a speaker * // Making the client a speaker
* guild.me.voice.setSuppressed(false); * guild.members.me.voice.setSuppressed(false);
* @example * @example
* // Making the client an audience member * // Making the client an audience member
* guild.me.voice.setSuppressed(true); * guild.members.me.voice.setSuppressed(true);
* @example * @example
* // Inviting another user to speak * // Inviting another user to speak
* voiceState.setSuppressed(false); * voiceState.setSuppressed(false);

View File

@@ -1113,7 +1113,6 @@ export class Guild extends AnonymousGuild {
public large: boolean; public large: boolean;
public maximumMembers: number | null; public maximumMembers: number | null;
public maximumPresences: number | null; public maximumPresences: number | null;
public get me(): GuildMember | null;
public memberCount: number; public memberCount: number;
public members: GuildMemberManager; public members: GuildMemberManager;
public mfaLevel: GuildMFALevel; public mfaLevel: GuildMFALevel;
@@ -3136,6 +3135,7 @@ export class GuildManager extends CachedManager<Snowflake, Guild, GuildResolvabl
export class GuildMemberManager extends CachedManager<Snowflake, GuildMember, GuildMemberResolvable> { export class GuildMemberManager extends CachedManager<Snowflake, GuildMember, GuildMemberResolvable> {
private constructor(guild: Guild, iterable?: Iterable<RawGuildMemberData>); private constructor(guild: Guild, iterable?: Iterable<RawGuildMemberData>);
public guild: Guild; public guild: Guild;
public get me(): GuildMember | null;
public add( public add(
user: UserResolvable, user: UserResolvable,
options: AddGuildMemberOptions & { fetchWhenExisting: false }, options: AddGuildMemberOptions & { fetchWhenExisting: false },
@@ -3333,6 +3333,7 @@ export class ThreadManager<AllowedThreadType> extends CachedManager<Snowflake, T
export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember, ThreadMemberResolvable> { export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember, ThreadMemberResolvable> {
private constructor(thread: ThreadChannel, iterable?: Iterable<RawThreadMemberData>); private constructor(thread: ThreadChannel, iterable?: Iterable<RawThreadMemberData>);
public thread: ThreadChannel; public thread: ThreadChannel;
public get me(): ThreadMember | null;
public add(member: UserResolvable | '@me', reason?: string): Promise<Snowflake>; public add(member: UserResolvable | '@me', reason?: string): Promise<Snowflake>;
public fetch(options?: ThreadMemberFetchOptions): Promise<ThreadMember>; public fetch(options?: ThreadMemberFetchOptions): Promise<ThreadMember>;
public fetch(cache?: boolean): Promise<Collection<Snowflake, ThreadMember>>; public fetch(cache?: boolean): Promise<Collection<Snowflake, ThreadMember>>;