Skip to content

Commit 12874cc

Browse files
committed
feat: support user guilds
1 parent 42fbca8 commit 12874cc

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,30 @@ class User extends Base {
151151
} else {
152152
this.avatarDecorationData = null;
153153
}
154+
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 server tag
159+
* @property {?string} tag - The user's server tag. Limited to 4 characters
160+
* @property {?string} badge - The server tag badge hash
161+
*/
162+
163+
if (data.primary_guild) {
164+
/**
165+
* The primary guild of the user
166+
*
167+
* @type {?UserPrimaryGuild}
168+
*/
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+
};
175+
} else {
176+
this.primaryGuild = null;
177+
}
154178
}
155179

156180
/**
@@ -246,6 +270,19 @@ class User extends Base {
246270
return this.banner && this.client.rest.cdn.banner(this.id, this.banner, options);
247271
}
248272

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 (
281+
this.primaryGuild?.badge &&
282+
this.client.rest.cdn.guildTagBadge(this.primaryGuild.identityGuildId, this.primaryGuild.badge, options)
283+
);
284+
}
285+
249286
/**
250287
* The tag of this user
251288
* <info>This user's username, or their legacy tag (e.g. `hydrabolt#0001`)
@@ -338,7 +375,11 @@ class User extends Base {
338375
this.banner === user.banner &&
339376
this.accentColor === user.accentColor &&
340377
this.avatarDecorationData?.asset === user.avatarDecorationData?.asset &&
341-
this.avatarDecorationData?.skuId === user.avatarDecorationData?.skuId
378+
this.avatarDecorationData?.skuId === user.avatarDecorationData?.skuId &&
379+
this.primaryGuild?.identityGuildId === user.primaryGuild?.identityGuildId &&
380+
this.primaryGuild?.identityEnabled === user.primaryGuild?.identityEnabled &&
381+
this.primaryGuild?.tag === user.primaryGuild?.tag &&
382+
this.primaryGuild?.badge === user.primaryGuild?.badge
342383
);
343384
}
344385

@@ -363,6 +404,12 @@ class User extends Base {
363404
('avatar_decoration_data' in user
364405
? this.avatarDecorationData?.asset === user.avatar_decoration_data?.asset &&
365406
this.avatarDecorationData?.skuId === user.avatar_decoration_data?.sku_id
407+
: true) &&
408+
('primary_guild' in user
409+
? this.primaryGuild?.identityGuildId === user.primary_guild?.identity_guild_id &&
410+
this.primaryGuild?.identityEnabled === user.primary_guild?.identity_enabled &&
411+
this.primaryGuild?.tag === user.primary_guild?.tag &&
412+
this.primaryGuild?.badge === user.primary_guild?.badge
366413
: true)
367414
);
368415
}
@@ -402,6 +449,7 @@ class User extends Base {
402449
json.avatarURL = this.avatarURL();
403450
json.displayAvatarURL = this.displayAvatarURL();
404451
json.bannerURL = this.banner ? this.bannerURL() : this.banner;
452+
json.guildTagBadgeURL = this.guildTagBadgeURL();
405453
return json;
406454
}
407455
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,6 +3497,13 @@ export interface AvatarDecorationData {
34973497
skuId: Snowflake;
34983498
}
34993499

3500+
export interface UserPrimaryGuild {
3501+
badge: string | null;
3502+
identityEnabled: boolean | null;
3503+
identityGuildId: Snowflake | null;
3504+
tag: string | null;
3505+
}
3506+
35003507
export interface UnfurledMediaItemData {
35013508
url: string;
35023509
}
@@ -3528,12 +3535,14 @@ export class User extends Base {
35283535
public get hexAccentColor(): HexColorString | null | undefined;
35293536
public id: Snowflake;
35303537
public get partial(): false;
3538+
public primaryGuild: UserPrimaryGuild;
35313539
public system: boolean;
35323540
public get tag(): string;
35333541
public username: string;
35343542
public avatarURL(options?: ImageURLOptions): string | null;
35353543
public avatarDecorationURL(): string | null;
35363544
public bannerURL(options?: ImageURLOptions): string | null | undefined;
3545+
public guildTagBadgeURL(options?: ImageURLOptions): string | null;
35373546
public createDM(force?: boolean): Promise<DMChannel>;
35383547
public deleteDM(): Promise<DMChannel>;
35393548
public displayAvatarURL(options?: ImageURLOptions): string;

packages/rest/src/lib/CDN.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ export class CDN {
318318
return `${this.cdn}${CDNRoutes.soundboardSound(soundId)}`;
319319
}
320320

321+
/**
322+
* Generates a URL for a guild tag badge.
323+
*
324+
* @param guildId - The guild id
325+
* @param badgeHash - The hash provided by Discord for this badge
326+
* @param options - Optional options for the badge
327+
*/
328+
public guildTagBadge(guildId: string, badgeHash: string, options?: Readonly<BaseImageURLOptions>): string {
329+
return this.makeURL(`/guild-tag-badges/${guildId}/${badgeHash}`, options);
330+
}
331+
321332
/**
322333
* Constructs the URL for the resource, checking whether or not `hash` starts with `a_` if `dynamic` is set to `true`.
323334
*

0 commit comments

Comments
 (0)