Skip to content

Commit

Permalink
refactor(UserManager): Move methods to the manager (#7087)
Browse files Browse the repository at this point in the history
Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
Co-authored-by: Noel <buechler.noel@outlook.com>
  • Loading branch information
3 people authored Dec 22, 2021
1 parent a0a5b0e commit 2ed02f7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 40 deletions.
92 changes: 79 additions & 13 deletions src/managers/UserManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,49 @@ class UserManager extends CachedManager {
*/

/**
* Resolves a {@link UserResolvable} to a {@link User} object.
* The DM between the client's user and a user
* @param {Snowflake} userId The user id
* @returns {?DMChannel}
* @private
*/
dmChannel(userId) {
return this.client.channels.cache.find(c => c.type === 'DM' && c.recipient.id === userId) ?? null;
}

/**
* Creates a {@link DMChannel} between the client and a user.
* @param {UserResolvable} user The UserResolvable to identify
* @returns {?User}
* @param {BaseFetchOptions} [options] Additional options for this fetch
* @returns {Promise<DMChannel>}
*/
resolve(user) {
if (user instanceof GuildMember || user instanceof ThreadMember) return user.user;
if (user instanceof Message) return user.author;
return super.resolve(user);
async createDM(user, { cache = true, force = false } = {}) {
const id = this.resolveId(user);

if (!force) {
const dmChannel = this.dmChannel(id);
if (dmChannel && !dmChannel.partial) return dmChannel;
}

const data = await this.client.api.users(this.client.user.id).channels.post({
data: {
recipient_id: id,
},
});
return this.client.channels._add(data, null, { cache });
}

/**
* Resolves a {@link UserResolvable} to a {@link User} id.
* Deletes a {@link DMChannel} (if one exists) between the client and a user. Resolves with the channel if successful.
* @param {UserResolvable} user The UserResolvable to identify
* @returns {?Snowflake}
* @returns {Promise<DMChannel>}
*/
resolveId(user) {
if (user instanceof ThreadMember) return user.id;
if (user instanceof GuildMember) return user.user.id;
if (user instanceof Message) return user.author.id;
return super.resolveId(user);
async deleteDM(user) {
const id = this.resolveId(user);
const dmChannel = this.dmChannel(id);
if (!dmChannel) throw new Error('USER_NO_DM_CHANNEL');
await this.client.api.channels(dmChannel.id).delete();
this.client.channels._remove(dmChannel.id);
return dmChannel;
}

/**
Expand All @@ -70,6 +93,49 @@ class UserManager extends CachedManager {
const data = await this.client.api.users(id).get();
return this._add(data, cache);
}

/**
* Fetches a user's flags.
* @param {UserResolvable} user The UserResolvable to identify
* @param {BaseFetchOptions} [options] Additional options for this fetch
* @returns {Promise<UserFlags>}
*/
async fetchFlags(user, options) {
return (await this.fetch(user, options)).flags;
}

/**
* Sends a message to a user.
* @param {UserResolvable} user The UserResolvable to identify
* @param {string|MessagePayload|MessageOptions} options The options to provide
* @returns {Promise<Message>}
*/
async send(user, options) {
return (await this.createDM(user)).send(options);
}

/**
* Resolves a {@link UserResolvable} to a {@link User} object.
* @param {UserResolvable} user The UserResolvable to identify
* @returns {?User}
*/
resolve(user) {
if (user instanceof GuildMember || user instanceof ThreadMember) return user.user;
if (user instanceof Message) return user.author;
return super.resolve(user);
}

/**
* Resolves a {@link UserResolvable} to a {@link User} id.
* @param {UserResolvable} user The UserResolvable to identify
* @returns {?Snowflake}
*/
resolveId(user) {
if (user instanceof ThreadMember) return user.id;
if (user instanceof GuildMember) return user.user.id;
if (user instanceof Message) return user.author.id;
return super.resolveId(user);
}
}

module.exports = UserManager;
5 changes: 3 additions & 2 deletions src/structures/GuildMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,11 @@ class GuildMember extends Base {

/**
* Creates a DM channel between the client and this member.
* @param {boolean} [force=false] Whether to skip the cache check and request the API
* @returns {Promise<DMChannel>}
*/
createDM() {
return this.user.createDM();
createDM(force = false) {
return this.user.createDM(force);
}

/**
Expand Down
31 changes: 7 additions & 24 deletions src/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,38 +207,24 @@ class User extends Base {
* @readonly
*/
get dmChannel() {
return this.client.channels.cache.find(c => c.type === 'DM' && c.recipient.id === this.id) ?? null;
return this.client.users.dmChannel(this.id);
}

/**
* Creates a DM channel between the client and the user.
* @param {boolean} [force=false] Whether to skip the cache check and request the API
* @returns {Promise<DMChannel>}
*/
async createDM(force = false) {
if (!force) {
const { dmChannel } = this;
if (dmChannel && !dmChannel.partial) return dmChannel;
}

const data = await this.client.api.users(this.client.user.id).channels.post({
data: {
recipient_id: this.id,
},
});
return this.client.channels._add(data);
createDM(force = false) {
return this.client.users.createDM(this.id, force);
}

/**
* Deletes a DM channel (if one exists) between the client and the user. Resolves with the channel if successful.
* @returns {Promise<DMChannel>}
*/
async deleteDM() {
const { dmChannel } = this;
if (!dmChannel) throw new Error('USER_NO_DM_CHANNEL');
await this.client.api.channels(dmChannel.id).delete();
this.client.channels._remove(dmChannel.id);
return dmChannel;
deleteDM() {
return this.client.users.deleteDM(this.id);
}

/**
Expand Down Expand Up @@ -285,11 +271,8 @@ class User extends Base {
* @param {boolean} [force=false] Whether to skip the cache check and request the API
* @returns {Promise<UserFlags>}
*/
async fetchFlags(force = false) {
if (this.flags && !force) return this.flags;
const data = await this.client.api.users(this.id).get();
this._patch(data);
return this.flags;
fetchFlags(force = false) {
return this.client.users.fetchFlags(this.id, { force });
}

/**
Expand Down
7 changes: 6 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2331,7 +2331,7 @@ export class User extends PartialTextBasedChannel(Base) {
public username: string;
public avatarURL(options?: ImageURLOptions): string | null;
public bannerURL(options?: ImageURLOptions): string | null;
public createDM(): Promise<DMChannel>;
public createDM(force?: boolean): Promise<DMChannel>;
public deleteDM(): Promise<DMChannel>;
public displayAvatarURL(options?: ImageURLOptions): string;
public equals(user: User): boolean;
Expand Down Expand Up @@ -3164,7 +3164,12 @@ export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember,

export class UserManager extends CachedManager<Snowflake, User, UserResolvable> {
private constructor(client: Client, iterable?: Iterable<RawUserData>);
private dmChannel(userId: Snowflake): DMChannel | null;
public createDM(user: UserResolvable, options?: BaseFetchOptions): Promise<DMChannel>;
public deleteDM(user: UserResolvable): Promise<DMChannel>;
public fetch(user: UserResolvable, options?: BaseFetchOptions): Promise<User>;
public fetchFlags(user: UserResolvable, options?: BaseFetchOptions): Promise<UserFlags>;
public send(user: UserResolvable, options: string | MessagePayload | MessageOptions): Promise<Message>;
}

export class VoiceStateManager extends CachedManager<Snowflake, VoiceState, typeof VoiceState> {
Expand Down

0 comments on commit 2ed02f7

Please sign in to comment.