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

@@ -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