mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: support user guilds (#10962)
* feat: support user guilds * chore: add cdn test * chore: suggested changes * refactor: move to user primary guild to its own class * docs: update description of `badgeHash` parameter * chore: export UserPrimaryGuild * refactor: revert to no `UserPrimaryGuild` * fix: use key in data * fix: both `in` and is truthy check * docs: remove `-` Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
@@ -139,6 +139,7 @@ class User extends Base {
|
|||||||
* @property {Snowflake} skuId The id of the avatar decoration's SKU
|
* @property {Snowflake} skuId The id of the avatar decoration's SKU
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
if ('avatar_decoration_data' in data) {
|
||||||
if (data.avatar_decoration_data) {
|
if (data.avatar_decoration_data) {
|
||||||
/**
|
/**
|
||||||
* The user avatar decoration's data
|
* The user avatar decoration's data
|
||||||
@@ -152,6 +153,9 @@ class User extends Base {
|
|||||||
} else {
|
} else {
|
||||||
this.avatarDecorationData = null;
|
this.avatarDecorationData = null;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this.avatarDecorationData ??= null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} NameplateData
|
* @typedef {Object} NameplateData
|
||||||
@@ -176,6 +180,34 @@ class User extends Base {
|
|||||||
} else {
|
} else {
|
||||||
this.collectibles = null;
|
this.collectibles = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} UserPrimaryGuild
|
||||||
|
* @property {?Snowflake} identityGuildId The id of the user's primary guild
|
||||||
|
* @property {?boolean} identityEnabled Whether the user is displaying the primary guild's tag
|
||||||
|
* @property {?string} tag The user's guild tag. Limited to 4 characters
|
||||||
|
* @property {?string} badge The guild tag badge hash
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ('primary_guild' in data) {
|
||||||
|
if (data.primary_guild) {
|
||||||
|
/**
|
||||||
|
* The primary guild of the user
|
||||||
|
*
|
||||||
|
* @type {?UserPrimaryGuild}
|
||||||
|
*/
|
||||||
|
this.primaryGuild = {
|
||||||
|
identityGuildId: data.primary_guild.identity_guild_id,
|
||||||
|
identityEnabled: data.primary_guild.identity_enabled,
|
||||||
|
tag: data.primary_guild.tag,
|
||||||
|
badge: data.primary_guild.badge,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
this.primaryGuild = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.primaryGuild ??= null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -271,6 +303,18 @@ class User extends Base {
|
|||||||
return this.banner && this.client.rest.cdn.banner(this.id, this.banner, options);
|
return this.banner && this.client.rest.cdn.banner(this.id, this.banner, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A link to the user's guild tag badge.
|
||||||
|
*
|
||||||
|
* @param {ImageURLOptions} [options={}] Options for the image URL
|
||||||
|
* @returns {?string}
|
||||||
|
*/
|
||||||
|
guildTagBadgeURL(options = {}) {
|
||||||
|
return this.primaryGuild?.badge
|
||||||
|
? this.client.rest.cdn.guildTagBadge(this.primaryGuild.identityGuildId, this.primaryGuild.badge, options)
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The tag of this user
|
* The tag of this user
|
||||||
* <info>This user's username, or their legacy tag (e.g. `hydrabolt#0001`)
|
* <info>This user's username, or their legacy tag (e.g. `hydrabolt#0001`)
|
||||||
@@ -367,7 +411,11 @@ class User extends Base {
|
|||||||
this.collectibles?.nameplate?.skuId === user.collectibles?.nameplate?.skuId &&
|
this.collectibles?.nameplate?.skuId === user.collectibles?.nameplate?.skuId &&
|
||||||
this.collectibles?.nameplate?.asset === user.collectibles?.nameplate?.asset &&
|
this.collectibles?.nameplate?.asset === user.collectibles?.nameplate?.asset &&
|
||||||
this.collectibles?.nameplate?.label === user.collectibles?.nameplate?.label &&
|
this.collectibles?.nameplate?.label === user.collectibles?.nameplate?.label &&
|
||||||
this.collectibles?.nameplate?.palette === user.collectibles?.nameplate?.palette
|
this.collectibles?.nameplate?.palette === user.collectibles?.nameplate?.palette &&
|
||||||
|
this.primaryGuild?.identityGuildId === user.primaryGuild?.identityGuildId &&
|
||||||
|
this.primaryGuild?.identityEnabled === user.primaryGuild?.identityEnabled &&
|
||||||
|
this.primaryGuild?.tag === user.primaryGuild?.tag &&
|
||||||
|
this.primaryGuild?.badge === user.primaryGuild?.badge
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,6 +446,12 @@ class User extends Base {
|
|||||||
this.collectibles?.nameplate?.asset === user.collectibles?.nameplate?.asset &&
|
this.collectibles?.nameplate?.asset === user.collectibles?.nameplate?.asset &&
|
||||||
this.collectibles?.nameplate?.label === user.collectibles?.nameplate?.label &&
|
this.collectibles?.nameplate?.label === user.collectibles?.nameplate?.label &&
|
||||||
this.collectibles?.nameplate?.palette === user.collectibles?.nameplate?.palette
|
this.collectibles?.nameplate?.palette === user.collectibles?.nameplate?.palette
|
||||||
|
: true) &&
|
||||||
|
('primary_guild' in user
|
||||||
|
? this.primaryGuild?.identityGuildId === user.primary_guild?.identity_guild_id &&
|
||||||
|
this.primaryGuild?.identityEnabled === user.primary_guild?.identity_enabled &&
|
||||||
|
this.primaryGuild?.tag === user.primary_guild?.tag &&
|
||||||
|
this.primaryGuild?.badge === user.primary_guild?.badge
|
||||||
: true)
|
: true)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -437,6 +491,7 @@ class User extends Base {
|
|||||||
json.avatarURL = this.avatarURL();
|
json.avatarURL = this.avatarURL();
|
||||||
json.displayAvatarURL = this.displayAvatarURL();
|
json.displayAvatarURL = this.displayAvatarURL();
|
||||||
json.bannerURL = this.banner ? this.bannerURL() : this.banner;
|
json.bannerURL = this.banner ? this.bannerURL() : this.banner;
|
||||||
|
json.guildTagBadgeURL = this.guildTagBadgeURL();
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
packages/discord.js/typings/index.d.ts
vendored
20
packages/discord.js/typings/index.d.ts
vendored
@@ -31,10 +31,7 @@ import {
|
|||||||
APIComponentInModalActionRow,
|
APIComponentInModalActionRow,
|
||||||
APIContainerComponent,
|
APIContainerComponent,
|
||||||
APIEmbed,
|
APIEmbed,
|
||||||
APIEmbedAuthor,
|
|
||||||
APIEmbedField,
|
APIEmbedField,
|
||||||
APIEmbedFooter,
|
|
||||||
APIEmbedImage,
|
|
||||||
APIEmbedProvider,
|
APIEmbedProvider,
|
||||||
APIEmoji,
|
APIEmoji,
|
||||||
APIEntitlement,
|
APIEntitlement,
|
||||||
@@ -3510,6 +3507,17 @@ export interface AvatarDecorationData {
|
|||||||
skuId: Snowflake;
|
skuId: Snowflake;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Collectibles {
|
||||||
|
nameplate: NameplateData | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserPrimaryGuild {
|
||||||
|
badge: string | null;
|
||||||
|
identityEnabled: boolean | null;
|
||||||
|
identityGuildId: Snowflake | null;
|
||||||
|
tag: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
export interface NameplateData {
|
export interface NameplateData {
|
||||||
asset: string;
|
asset: string;
|
||||||
label: string;
|
label: string;
|
||||||
@@ -3517,10 +3525,6 @@ export interface NameplateData {
|
|||||||
skuId: Snowflake;
|
skuId: Snowflake;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Collectibles {
|
|
||||||
nameplate: NameplateData | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UnfurledMediaItemData {
|
export interface UnfurledMediaItemData {
|
||||||
url: string;
|
url: string;
|
||||||
}
|
}
|
||||||
@@ -3553,12 +3557,14 @@ export class User extends Base {
|
|||||||
public get hexAccentColor(): HexColorString | null | undefined;
|
public get hexAccentColor(): HexColorString | null | undefined;
|
||||||
public id: Snowflake;
|
public id: Snowflake;
|
||||||
public get partial(): false;
|
public get partial(): false;
|
||||||
|
public primaryGuild: UserPrimaryGuild | null;
|
||||||
public system: boolean;
|
public system: boolean;
|
||||||
public get tag(): string;
|
public get tag(): string;
|
||||||
public username: string;
|
public username: string;
|
||||||
public avatarURL(options?: ImageURLOptions): string | null;
|
public avatarURL(options?: ImageURLOptions): string | null;
|
||||||
public avatarDecorationURL(): string | null;
|
public avatarDecorationURL(): string | null;
|
||||||
public bannerURL(options?: ImageURLOptions): string | null | undefined;
|
public bannerURL(options?: ImageURLOptions): string | null | undefined;
|
||||||
|
public guildTagBadgeURL(options?: ImageURLOptions): string | null;
|
||||||
public createDM(force?: boolean): 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;
|
||||||
|
|||||||
@@ -134,8 +134,11 @@ test('soundboardSound', () => {
|
|||||||
expect(cdn.soundboardSound(id)).toEqual(`${baseCDN}/soundboard-sounds/${id}`);
|
expect(cdn.soundboardSound(id)).toEqual(`${baseCDN}/soundboard-sounds/${id}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('guildTagBadge', () => {
|
||||||
|
expect(cdn.guildTagBadge(id, hash)).toEqual(`${baseCDN}/guild-tag-badges/${id}/${hash}.webp`);
|
||||||
|
});
|
||||||
|
|
||||||
test('makeURL throws on invalid size', () => {
|
test('makeURL throws on invalid size', () => {
|
||||||
// @ts-expect-error: Invalid size
|
|
||||||
expect(() => cdn.avatar(id, animatedHash, { size: 5 })).toThrow(RangeError);
|
expect(() => cdn.avatar(id, animatedHash, { size: 5 })).toThrow(RangeError);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -340,6 +340,17 @@ export class CDN {
|
|||||||
return `${this.cdn}${CDNRoutes.soundboardSound(soundId)}`;
|
return `${this.cdn}${CDNRoutes.soundboardSound(soundId)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a URL for a guild tag badge.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id
|
||||||
|
* @param badgeHash - The hash of the badge
|
||||||
|
* @param options - Optional options for the badge
|
||||||
|
*/
|
||||||
|
public guildTagBadge(guildId: string, badgeHash: string, options?: Readonly<BaseImageURLOptions>): string {
|
||||||
|
return this.makeURL(`/guild-tag-badges/${guildId}/${badgeHash}`, options);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs the URL for the resource, checking whether or not `hash` starts with `a_` if `dynamic` is set to `true`.
|
* Constructs the URL for the resource, checking whether or not `hash` starts with `a_` if `dynamic` is set to `true`.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user