refactor: Move me and add fetchMe()

#9029 djs
This commit is contained in:
March 7th 2023-01-10 18:08:05 +07:00
parent 579edfa8fe
commit f53d329462
15 changed files with 77 additions and 29 deletions

View File

@ -443,10 +443,10 @@ class Player extends EventEmitter {
10_000,
);
connection.subscribe(this._player);
await this.channel.guild.me.voice
await this.channel.guild.members.me.voice
.setSuppressed(false)
.catch(async () => {
return await this.channel.guild.me.voice
return await this.channel.guild.members.me.voice
.setRequestToSpeak(true);
});
} else {

View File

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

View File

@ -9,6 +9,7 @@ const BaseGuildVoiceChannel = require('../structures/BaseGuildVoiceChannel');
const { GuildMember } = require('../structures/GuildMember');
const { Role } = require('../structures/Role');
const { Events, Opcodes } = require('../util/Constants');
const { PartialTypes } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const SnowflakeUtil = require('../util/SnowflakeUtil');
@ -119,6 +120,20 @@ class GuildMemberManager extends CachedManager {
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(PartialTypes.GUILD_MEMBER)
? this._add({ user: { id: this.client.user.id } }, true)
: null)
);
}
/**
* Options used to fetch a single member from a guild.
* @typedef {BaseFetchOptions} FetchMemberOptions
@ -179,9 +194,9 @@ class GuildMemberManager extends CachedManager {
if (!options || (!options?.query && !options?.user)) {
// Check Permissions
if (
this.guild.me.permissions.has('KICK_MEMBERS') ||
this.guild.me.permissions.has('BAN_MEMBERS') ||
this.guild.me.permissions.has('MANAGE_ROLES')
this.guild.members.me.permissions.has('KICK_MEMBERS') ||
this.guild.members.me.permissions.has('BAN_MEMBERS') ||
this.guild.members.me.permissions.has('MANAGE_ROLES')
) {
return this._fetchMany();
} else {
@ -205,6 +220,15 @@ class GuildMemberManager extends CachedManager {
return this._fetchMany(options);
}
/**
* Fetches the client user as a GuildMember of the guild.
* @param {BaseFetchOptions} [options] The options for fetching the member
* @returns {Promise<GuildMember>}
*/
fetchMe(options) {
return this.fetch({ ...options, user: this.client.user.id });
}
/**
* Options used for searching guild members.
* @typedef {Object} GuildSearchMembersOptions

View File

@ -36,6 +36,24 @@ class ThreadMemberManager extends CachedManager {
return member;
}
/**
* Fetches the client user as a ThreadMember of the thread.
* @param {BaseFetchOptions} [options] The options for fetching the member
* @returns {Promise<ThreadMember>}
*/
fetchMe(options) {
return this.fetch(this.client.user.id, options);
}
/**
* 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:
* * A ThreadMember object

View File

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

View File

@ -26,7 +26,6 @@ const VoiceStateManager = require('../managers/VoiceStateManager');
const {
ChannelTypes,
DefaultMessageNotificationLevels,
PartialTypes,
VerificationLevels,
ExplicitContentFilterLevels,
Status,
@ -41,6 +40,7 @@ const Util = require('../util/Util');
let deprecationEmittedForSetChannelPositions = false;
let deprecationEmittedForSetRolePositions = false;
let deprecationEmittedForDeleted = false;
let deprecationEmittedForMe = false;
/**
* @type {WeakSet<Guild>}
@ -608,15 +608,16 @@ class Guild extends AnonymousGuild {
/**
* The client user as a GuildMember of this guild.
* @type {?GuildMember}
* @deprecated Use {@link GuildMemberManager#me} instead.
* @readonly
*/
get me() {
return (
this.members.resolve(this.client.user.id) ??
(this.client.options.partials.includes(PartialTypes.GUILD_MEMBER)
? this.members._add({ user: { id: this.client.user.id } }, true)
: null)
);
if (!deprecationEmittedForMe) {
process.emitWarning('Guild#me is deprecated. Use Guild#members#me instead.', 'DeprecationWarning');
deprecationEmittedForMe = true;
}
return this.members.me;
}
/**

View File

@ -415,7 +415,7 @@ class GuildChannel extends Channel {
// This flag allows managing even if timed out
if (permissions.has(Permissions.FLAGS.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)
? Permissions.FLAGS.MANAGE_CHANNELS | Permissions.FLAGS.CONNECT

View File

@ -56,7 +56,7 @@ class GuildEmoji extends BaseGuildEmoji {
*/
get deletable() {
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME');
return !this.managed && this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS_AND_STICKERS);
return !this.managed && this.guild.members.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS_AND_STICKERS);
}
/**

View File

@ -334,7 +334,7 @@ class GuildMember extends Base {
if (this.user.id === this.client.user.id) return false;
if (this.client.user.id === this.guild.ownerId) return true;
if (!this.guild.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;
}
/**
@ -343,7 +343,7 @@ class GuildMember extends Base {
* @readonly
*/
get kickable() {
return this.manageable && this.guild.me.permissions.has(Permissions.FLAGS.KICK_MEMBERS);
return this.manageable && this.guild.members.me.permissions.has(Permissions.FLAGS.KICK_MEMBERS);
}
/**
@ -352,7 +352,7 @@ class GuildMember extends Base {
* @readonly
*/
get bannable() {
return this.manageable && this.guild.me.permissions.has(Permissions.FLAGS.BAN_MEMBERS);
return this.manageable && this.guild.members.me.permissions.has(Permissions.FLAGS.BAN_MEMBERS);
}
/**

View File

@ -245,7 +245,7 @@ class Invite extends Base {
if (!guild.me) throw new Error('GUILD_UNCACHED_ME');
return (
this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) ||
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)
guild.members.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)
);
}

View File

@ -638,7 +638,7 @@ class Message extends Base {
return Boolean(
this.author.id === this.client.user.id ||
(permissions.has(Permissions.FLAGS.MANAGE_MESSAGES, false) &&
this.guild.me.communicationDisabledUntilTimestamp < Date.now()),
this.guild.members.me.communicationDisabledUntilTimestamp < Date.now()),
);
}
@ -1040,7 +1040,7 @@ class Message extends Base {
this.mentions.everyone ||
this.mentions.repliedUser?.id === this.client.user.id ||
this.mentions.users.has(this.client.user.id) ||
(this.guildId && this.mentions.roles.some(r => this.guild.me._roles?.includes(r.id)))
(this.guildId && this.mentions.roles.some(r => this.guild.members.me._roles?.includes(r.id)))
? 1
: 0,
},

View File

@ -529,7 +529,7 @@ class ThreadChannel extends Channel {
if (permissions.has(Permissions.FLAGS.ADMINISTRATOR, false)) return true;
return (
this.guild.me.communicationDisabledUntilTimestamp < Date.now() &&
this.guild.members.me.communicationDisabledUntilTimestamp < Date.now() &&
permissions.has(Permissions.FLAGS.MANAGE_THREADS, false)
);
}
@ -561,7 +561,7 @@ class ThreadChannel extends Channel {
!(this.archived && this.locked && !this.manageable) &&
(this.type !== 'GUILD_PRIVATE_THREAD' || this.joined || this.manageable) &&
permissions.has(Permissions.FLAGS.SEND_MESSAGES_IN_THREADS, false) &&
this.guild.me.communicationDisabledUntilTimestamp < Date.now()
this.guild.members.me.communicationDisabledUntilTimestamp < Date.now()
);
}

View File

@ -120,7 +120,7 @@ class VoiceChannel extends BaseGuildVoiceChannel {
if (permissions.has(Permissions.FLAGS.ADMINISTRATOR, false)) return true;
return (
this.guild.me.communicationDisabledUntilTimestamp < Date.now() && permissions.has(Permissions.FLAGS.SPEAK, false)
this.guild.members.me.communicationDisabledUntilTimestamp < Date.now() && permissions.has(Permissions.FLAGS.SPEAK, false)
);
}

View File

@ -220,10 +220,10 @@ class VoiceState extends Base {
* @param {boolean} [request=true] Whether or not the client is requesting to become a speaker.
* @example
* // 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
* // Making the client cancel a request to speak
* guild.me.voice.setRequestToSpeak(false);
* guild.members.me.voice.setRequestToSpeak(false);
* @returns {Promise<void>}
*/
async setRequestToSpeak(request = true) {
@ -246,10 +246,10 @@ class VoiceState extends Base {
* @param {boolean} [suppressed=true] Whether or not the user should be suppressed.
* @example
* // Making the client a speaker
* guild.me.voice.setSuppressed(false);
* guild.members.me.voice.setSuppressed(false);
* @example
* // Making the client an audience member
* guild.me.voice.setSuppressed(true);
* guild.members.me.voice.setSuppressed(true);
* @example
* // Inviting another user to speak
* voiceState.setSuppressed(false);

5
typings/index.d.ts vendored
View File

@ -1322,6 +1322,7 @@ export class Guild extends AnonymousGuild {
public maximumPresences: number | null;
public maxStageVideoChannelUsers: number | null;
public maxVideoChannelUsers: number | null;
/** @deprecated Use {@link GuildMemberManager.me} instead. */
public readonly me: GuildMember | null;
public memberCount: number;
public members: GuildMemberManager;
@ -3935,6 +3936,7 @@ export interface BruteforceOptions {
export class GuildMemberManager extends CachedManager<Snowflake, GuildMember, GuildMemberResolvable> {
private constructor(guild: Guild, iterable?: Iterable<RawGuildMemberData>);
public guild: Guild;
public readonly me: GuildMember | null;
public add(
user: UserResolvable,
options: AddGuildMemberOptions & { fetchWhenExisting: false },
@ -3954,6 +3956,7 @@ export class GuildMemberManager extends CachedManager<Snowflake, GuildMember, Gu
time?: number,
): Promise<Collection<Snowflake, GuildMember>>;
public fetchBruteforce(options?: BruteforceOptions): Promise<Collection<Snowflake, GuildMember>>;
public fetchMe(options?: BaseFetchOptions): Promise<GuildMember>;
public kick(user: UserResolvable, reason?: string): Promise<GuildMember | User | Snowflake>;
public list(options?: GuildListMembersOptions): Promise<Collection<Snowflake, GuildMember>>;
public prune(options: GuildPruneMembersOptions & { dry?: false; count: false }): Promise<null>;
@ -4182,10 +4185,12 @@ export class ThreadManager extends CachedManager<Snowflake, ThreadChannel, Threa
export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember, ThreadMemberResolvable> {
private constructor(thread: ThreadChannel, iterable?: Iterable<RawThreadMemberData>);
public thread: ThreadChannel;
public readonly me: ThreadMember | null;
public add(member: UserResolvable | '@me', reason?: string): Promise<Snowflake>;
public fetch(member?: UserResolvable, options?: BaseFetchOptions): Promise<ThreadMember>;
/** @deprecated Use `fetch(member, options)` instead. */
public fetch(cache?: boolean): Promise<Collection<Snowflake, ThreadMember>>;
public fetchMe(options?: BaseFetchOptions): Promise<ThreadMember>;
public remove(id: Snowflake | '@me', reason?: string): Promise<Snowflake>;
}