Files
TTtie 476fa65b0a chore: default the image format to .webp, add missing dynamic image methods (#257)
- Makes .webp the default image format for both static and animated
images
  - Discord documentation states that assets uploaded as animated
  .webp/.avif aren't converted to other formats. (see ref)
  - Although static .webp is still converted to other formats, using a
  single image format makes matters a bit more consistent
  - This is obviously a breaking change for users that expect to get a
  .jpg/.gif out of the URL getters. Most reasonably modern image
  processing toolkits should be able to take .webp media just fine though.
- .webp links will now contain the `?animated=true` query string
parameter for animated images
  - An additional boolean parameter has been added to `dynamic[...]URL()`
  APIs to exclude the parameter, forcing a static version of the image.
- Added some missing dynamic URL APIs, namely `Role#dynamicIconURL` and
`GuildScheduledEvent#dynamicImageURL`

Ref:
https://github.com/discord/discord-api-docs/commit/ab3ea3c8d7a7c5ddc6b991f409355b4fa86bc9b3
2026-07-29 23:34:49 +00:00

279 lines
8.5 KiB
JavaScript

"use strict";
const Base = require("./Base");
const Endpoints = require("../rest/Endpoints");
/**
* Represents a user
* @extends Base
*/
class User extends Base {
/**
* The ID of the user
* @member {String} User#id
*/
/**
* Timestamp of the user's creation
* @member {Number} User#createdAt
*/
#client;
#missingClientError;
constructor(data, client) {
super(data.id);
if(!client) {
this.#missingClientError = new Error("Missing client in constructor"); // Preserve constructor callstack
}
this.#client = client;
/**
* Whether the user is an OAuth bot or not
* @type {Boolean}
*/
this.bot = !!data.bot;
/**
* Whether the user is an official Discord system user (e.g. urgent messages)
* @type {Boolean}
*/
this.system = !!data.system;
this.update(data);
}
update(data) {
if(data.avatar !== undefined) {
/**
* The hash of the user's avatar, or null if no avatar
* @type {String?}
*/
this.avatar = data.avatar;
}
if(data.avatar_decoration_data !== undefined) {
/**
* The data of the user's avatar decoration, or null if no avatar decoration
* @type {Object?}
*/
this.avatarDecorationData = data.avatar_decoration_data;
}
if(data.username !== undefined) {
/**
* The username of the user
* @type {String}
*/
this.username = data.username;
}
if(data.discriminator !== undefined) {
/**
* The discriminator of the user - if a single zero digit ("0"), the user is using the unique username system
* @type {String}
*/
this.discriminator = data.discriminator;
}
if(data.public_flags !== undefined) {
/**
* Publicly visible flags for this user
* @type {Number?}
*/
this.publicFlags = data.public_flags;
}
if(data.banner !== undefined) {
/**
* The hash of the user's banner, or null if no banner (REST only)
* @type {String?}
*/
this.banner = data.banner;
}
if(data.accent_color !== undefined) {
/**
* The user's banner color, or null if no banner color (REST only)
* @type {Number?}
*/
this.accentColor = data.accent_color;
}
if(data.global_name !== undefined) {
/**
* The globally visible display name of the user
* @type {String?}
*/
this.globalName = data.global_name;
}
if(data.collectibles !== undefined) {
/**
* The user's collectibles, or null if the user hasn't got any
* @type {User.Collectibles?}
*/
this.collectibles = data.collectibles;
}
if(data.primary_guild !== undefined) {
/**
* The primary guild of the user
* @type {User.PrimaryGuild?}
*/
this.primaryGuild = data.primary_guild != null
? {
badge: data.primary_guild.badge,
identityEnabled: data.primary_guild.identity_enabled,
identityGuildID: data.primary_guild.identity_guild_id,
tag: data.primary_guild.tag
}
: null;
}
}
/**
* The URL of the user's avatar either as a static image in the preferred format (or a PNG for default avatars), or an animated WEBP image
* @type {String}
*/
get avatarURL() {
if(this.#missingClientError) {
throw this.#missingClientError;
}
return this.avatar ? this.#client._formatImage(Endpoints.USER_AVATAR(this.id, this.avatar)) : this.defaultAvatarURL;
}
/**
* The URL of the user's avatar decoration, which is usually an animated PNG
* @type {String?}
*/
get avatarDecorationURL() {
if(!this.avatarDecorationData) {
return null;
}
const url = Endpoints.USER_AVATAR_DECORATION_PRESET(this.avatarDecorationData.asset);
return `${Endpoints.CDN_URL}${url}.png`;
}
/**
* The URL of the user's banner
* @type {String?}
*/
get bannerURL() {
if(!this.banner) {
return null;
}
if(this.#missingClientError) {
throw this.#missingClientError;
}
return this.#client._formatImage(Endpoints.BANNER(this.id, this.banner));
}
/**
* The hash for the default avatar of a user if there is no avatar set
* @type {String}
*/
get defaultAvatar() {
if(this.discriminator === "0") {
return Base.getDiscordEpoch(this.id) % 6;
}
return this.discriminator % 5;
}
/**
* The URL of the user's default avatar
* @type {String}
*/
get defaultAvatarURL() {
return `${Endpoints.CDN_URL}${Endpoints.DEFAULT_USER_AVATAR(this.defaultAvatar)}.png`;
}
/**
* A string that mentions the user
* @type {String}
*/
get mention() {
return `<@${this.id}>`;
}
/**
* The URL of the user's avatar (always a static WEBP, or a PNG for default avatars)
* @type {String}
*/
get staticAvatarURL() {
if(this.#missingClientError) {
throw this.#missingClientError;
}
return this.avatar ? this.#client._formatImage(Endpoints.USER_AVATAR(this.id, this.avatar), "webp", null, true) : this.defaultAvatarURL;
}
/**
* Get the user's avatar with the given format and size
* @param {String} [format] The filetype of the avatar ("jpg", "jpeg", "png", "gif", or "webp")
* @param {Number} [size] The size of the avatar (any power of two between 16 and 4096)
* @param {Boolean} [forceStatic=false] Whether to force the avatar to be static even if it is animated
* @returns {String}
*/
dynamicAvatarURL(format, size, forceStatic) {
if(!this.avatar) {
return this.defaultAvatarURL;
}
if(this.#missingClientError) {
throw this.#missingClientError;
}
return this.#client._formatImage(Endpoints.USER_AVATAR(this.id, this.avatar), format, size, forceStatic);
}
/**
* Get the user's banner with the given format and size
* @param {String} [format] The filetype of the banner ("jpg", "jpeg", "png", "gif", or "webp")
* @param {Number} [size] The size of the banner (any power of two between 16 and 4096)
* @param {Boolean} [forceStatic=false] Whether to force the banner to be static even if it is animated
* @returns {String?}
*/
dynamicBannerURL(format, size, forceStatic) {
if(!this.banner) {
return null;
}
if(this.#missingClientError) {
throw this.#missingClientError;
}
return this.#client._formatImage(Endpoints.BANNER(this.id, this.banner), format, size, forceStatic);
}
/**
* Get a DM channel with the user, or create one if it does not exist
* @returns {Promise<PrivateChannel>}
*/
getDMChannel() {
return this.#client.getDMChannel.call(this.#client, this.id);
}
toJSON(props = []) {
return super.toJSON([
"accentColor",
"avatar",
"banner",
"bot",
"collectibles",
"discriminator",
"globalName",
"primaryGuild",
"publicFlags",
"system",
"username",
...props
]);
}
}
module.exports = User;
/**
* The collectibles that the user has
* @typedef User.Collectibles
* @prop {User.Nameplate} [nameplate] The nameplate the user has
*/
/**
* The nameplate data
* @typedef User.Nameplate
* @prop {String} sku_id The ID of the nameplate SKU
* @prop {String} asset Path to the nameplate asset
* @prop {String} label The label of the nameplate
* @prop {String} palette The background color of the nameplate
*/
/**
* User's primary guild data
* @typedef User.PrimaryGuild
* @prop {String?} badge The hash of the guild badge
* @prop {Boolean?} identityEnabled Whether the primary guild's server tag is displayed or not
* @prop {String?} identityGuildID The ID of the primary guild
* @prop {String?} tag The guild tag
*/