Skip to content

Commit cae2a68

Browse files
committed
refactor: move to user primary guild to its own class
1 parent 12ff289 commit cae2a68

File tree

3 files changed

+78
-35
lines changed

3 files changed

+78
-35
lines changed

packages/discord.js/src/structures/User.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { calculateUserDefaultAvatarIndex } = require('@discordjs/rest');
55
const { DiscordSnowflake } = require('@sapphire/snowflake');
66
const { UserFlagsBitField } = require('../util/UserFlagsBitField.js');
77
const { Base } = require('./Base.js');
8+
const { UserPrimaryGuild } = require('./UserPrimaryGuild.js');
89

910
/**
1011
* Represents a user on Discord.
@@ -152,26 +153,13 @@ class User extends Base {
152153
this.avatarDecorationData = null;
153154
}
154155

155-
/**
156-
* @typedef {Object} UserPrimaryGuild
157-
* @property {?Snowflake} identityGuildId - The id of the user's primary guild
158-
* @property {?boolean} identityEnabled - Whether the user is displaying the primary guild's tag
159-
* @property {?string} tag - The user's guild tag. Limited to 4 characters
160-
* @property {?string} badge - The guild tag badge hash
161-
*/
162-
163156
if (data.primary_guild) {
164157
/**
165158
* The primary guild of the user
166159
*
167160
* @type {?UserPrimaryGuild}
168161
*/
169-
this.primaryGuild = {
170-
identityGuildId: data.primary_guild.identity_guild_id,
171-
identityEnabled: data.primary_guild.identity_enabled,
172-
tag: data.primary_guild.tag,
173-
badge: data.primary_guild.badge,
174-
};
162+
this.primaryGuild = new UserPrimaryGuild(data.primary_guild, this.client);
175163
} else {
176164
this.primaryGuild = null;
177165
}
@@ -270,18 +258,6 @@ class User extends Base {
270258
return this.banner && this.client.rest.cdn.banner(this.id, this.banner, options);
271259
}
272260

273-
/**
274-
* A link to the user's guild tag badge.
275-
*
276-
* @param {ImageURLOptions} [options={}] Options for the image URL
277-
* @returns {?string}
278-
*/
279-
guildTagBadgeURL(options = {}) {
280-
return this.primaryGuild?.badge
281-
? this.client.rest.cdn.guildTagBadge(this.primaryGuild.identityGuildId, this.primaryGuild.badge, options)
282-
: null;
283-
}
284-
285261
/**
286262
* The tag of this user
287263
* <info>This user's username, or their legacy tag (e.g. `hydrabolt#0001`)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
/**
4+
* Represents the primary guild of a User.
5+
*/
6+
class UserPrimaryGuild {
7+
constructor(data, client) {
8+
/**
9+
* The client that instantiated this
10+
*
11+
* @name UserPrimaryGuild#client
12+
* @type {Client}
13+
* @readonly
14+
*/
15+
Object.defineProperty(this, 'client', { value: client });
16+
17+
/**
18+
* The id of the user's primary guild
19+
*
20+
* @type {?Snowflake}
21+
*/
22+
this.identityGuildId = data.identity_guild_id;
23+
24+
/**
25+
* Whether the user is displaying the primary guild's tag
26+
*
27+
* @type {?boolean}
28+
*/
29+
this.identityEnabled = data.identity_enabled;
30+
31+
/**
32+
* The user's guild tag. Limited to 4 characters
33+
*
34+
* @type {?string}
35+
*/
36+
this.tag = data.tag;
37+
38+
/**
39+
* The guild tag badge hash
40+
*
41+
* @type {?string}
42+
*/
43+
this.badge = data.badge;
44+
}
45+
46+
/**
47+
* The user's primary guild
48+
*
49+
* @type {?Guild}
50+
* @readonly
51+
*/
52+
get identityGuild() {
53+
return this.client.guilds.cache.get(this.identityGuildId) ?? null;
54+
}
55+
56+
/**
57+
* A link to the user's guild tag badge.
58+
*
59+
* @param {ImageURLOptions} [options={}] Options for the image URL
60+
* @returns {?string}
61+
*/
62+
guildTagBadgeURL(options = {}) {
63+
return this.badge ? this.client.rest.cdn.guildTagBadge(this.identityGuildId, this.badge, options) : null;
64+
}
65+
}
66+
67+
exports.UserPrimaryGuild = UserPrimaryGuild;

packages/discord.js/typings/index.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ import {
3131
APIComponentInModalActionRow,
3232
APIContainerComponent,
3333
APIEmbed,
34-
APIEmbedAuthor,
3534
APIEmbedField,
36-
APIEmbedFooter,
37-
APIEmbedImage,
3835
APIEmbedProvider,
3936
APIEmoji,
4037
APIEntitlement,
@@ -3497,11 +3494,15 @@ export interface AvatarDecorationData {
34973494
skuId: Snowflake;
34983495
}
34993496

3500-
export interface UserPrimaryGuild {
3501-
badge: string | null;
3502-
identityEnabled: boolean | null;
3503-
identityGuildId: Snowflake | null;
3504-
tag: string | null;
3497+
export class UserPrimaryGuild {
3498+
// TODO: Change to APIUserPrimaryGuild
3499+
private constructor(data: unknown, client: Client<true>);
3500+
public identityGuildId: Snowflake | null;
3501+
public identityEnabled: boolean | null;
3502+
public tag: string | null;
3503+
public badge: string | null;
3504+
public get identityGuild(): Guild | null;
3505+
public guildTagBadgeURL(options?: ImageURLOptions): string | null;
35053506
}
35063507

35073508
export interface UnfurledMediaItemData {
@@ -3542,7 +3543,6 @@ export class User extends Base {
35423543
public avatarURL(options?: ImageURLOptions): string | null;
35433544
public avatarDecorationURL(): string | null;
35443545
public bannerURL(options?: ImageURLOptions): string | null | undefined;
3545-
public guildTagBadgeURL(options?: ImageURLOptions): string | null;
35463546
public createDM(force?: boolean): Promise<DMChannel>;
35473547
public deleteDM(): Promise<DMChannel>;
35483548
public displayAvatarURL(options?: ImageURLOptions): string;

0 commit comments

Comments
 (0)