mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 02:53:31 +01:00
refactor(UserManager): Move methods to the manager (#7087)
Co-authored-by: SpaceEEC <spaceeec@yahoo.com> Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
@@ -31,6 +31,89 @@ class UserManager extends CachedManager {
|
|||||||
* @typedef {User|Snowflake|Message|GuildMember|ThreadMember} UserResolvable
|
* @typedef {User|Snowflake|Message|GuildMember|ThreadMember} UserResolvable
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
||||||
|
* @returns {Promise<DMChannel>}
|
||||||
|
*/
|
||||||
|
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 });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {Promise<DMChannel>}
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains a user from Discord, or the user cache if it's already available.
|
||||||
|
* @param {UserResolvable} user The user to fetch
|
||||||
|
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
||||||
|
* @returns {Promise<User>}
|
||||||
|
*/
|
||||||
|
async fetch(user, { cache = true, force = false } = {}) {
|
||||||
|
const id = this.resolveId(user);
|
||||||
|
if (!force) {
|
||||||
|
const existing = this.cache.get(id);
|
||||||
|
if (existing && !existing.partial) return existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
* Resolves a {@link UserResolvable} to a {@link User} object.
|
||||||
* @param {UserResolvable} user The UserResolvable to identify
|
* @param {UserResolvable} user The UserResolvable to identify
|
||||||
@@ -53,23 +136,6 @@ class UserManager extends CachedManager {
|
|||||||
if (user instanceof Message) return user.author.id;
|
if (user instanceof Message) return user.author.id;
|
||||||
return super.resolveId(user);
|
return super.resolveId(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Obtains a user from Discord, or the user cache if it's already available.
|
|
||||||
* @param {UserResolvable} user The user to fetch
|
|
||||||
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
|
||||||
* @returns {Promise<User>}
|
|
||||||
*/
|
|
||||||
async fetch(user, { cache = true, force = false } = {}) {
|
|
||||||
const id = this.resolveId(user);
|
|
||||||
if (!force) {
|
|
||||||
const existing = this.cache.get(id);
|
|
||||||
if (existing && !existing.partial) return existing;
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await this.client.api.users(id).get();
|
|
||||||
return this._add(data, cache);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = UserManager;
|
module.exports = UserManager;
|
||||||
|
|||||||
@@ -318,10 +318,11 @@ class GuildMember extends Base {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a DM channel between the client and this member.
|
* 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>}
|
* @returns {Promise<DMChannel>}
|
||||||
*/
|
*/
|
||||||
createDM() {
|
createDM(force = false) {
|
||||||
return this.user.createDM();
|
return this.user.createDM(force);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -207,7 +207,7 @@ class User extends Base {
|
|||||||
* @readonly
|
* @readonly
|
||||||
*/
|
*/
|
||||||
get dmChannel() {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -215,30 +215,16 @@ class User extends Base {
|
|||||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||||
* @returns {Promise<DMChannel>}
|
* @returns {Promise<DMChannel>}
|
||||||
*/
|
*/
|
||||||
async createDM(force = false) {
|
createDM(force = false) {
|
||||||
if (!force) {
|
return this.client.users.createDM(this.id, 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a DM channel (if one exists) between the client and the user. Resolves with the channel if successful.
|
* Deletes a DM channel (if one exists) between the client and the user. Resolves with the channel if successful.
|
||||||
* @returns {Promise<DMChannel>}
|
* @returns {Promise<DMChannel>}
|
||||||
*/
|
*/
|
||||||
async deleteDM() {
|
deleteDM() {
|
||||||
const { dmChannel } = this;
|
return this.client.users.deleteDM(this.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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -285,11 +271,8 @@ class User extends Base {
|
|||||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||||
* @returns {Promise<UserFlags>}
|
* @returns {Promise<UserFlags>}
|
||||||
*/
|
*/
|
||||||
async fetchFlags(force = false) {
|
fetchFlags(force = false) {
|
||||||
if (this.flags && !force) return this.flags;
|
return this.client.users.fetchFlags(this.id, { force });
|
||||||
const data = await this.client.api.users(this.id).get();
|
|
||||||
this._patch(data);
|
|
||||||
return this.flags;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
7
typings/index.d.ts
vendored
7
typings/index.d.ts
vendored
@@ -2331,7 +2331,7 @@ export class User extends PartialTextBasedChannel(Base) {
|
|||||||
public username: string;
|
public username: string;
|
||||||
public avatarURL(options?: ImageURLOptions): string | null;
|
public avatarURL(options?: ImageURLOptions): string | null;
|
||||||
public bannerURL(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 deleteDM(): Promise<DMChannel>;
|
||||||
public displayAvatarURL(options?: ImageURLOptions): string;
|
public displayAvatarURL(options?: ImageURLOptions): string;
|
||||||
public equals(user: User): boolean;
|
public equals(user: User): boolean;
|
||||||
@@ -3164,7 +3164,12 @@ export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember,
|
|||||||
|
|
||||||
export class UserManager extends CachedManager<Snowflake, User, UserResolvable> {
|
export class UserManager extends CachedManager<Snowflake, User, UserResolvable> {
|
||||||
private constructor(client: Client, iterable?: Iterable<RawUserData>);
|
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 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> {
|
export class VoiceStateManager extends CachedManager<Snowflake, VoiceState, typeof VoiceState> {
|
||||||
|
|||||||
Reference in New Issue
Block a user