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

231 lines
7.3 KiB
JavaScript

"use strict";
const Base = require("./Base");
const Endpoints = require("../rest/Endpoints");
const Permission = require("./Permission");
/**
* Represents a role
* @extends Base
*/
class Role extends Base {
/**
* The ID of the role
* @member {String} Role#id
*/
/**
* Timestamp of the role's creation
* @member {Number} Role#createdAt
*/
constructor(data, guild) {
super(data.id);
/**
* The guild that owns the role
* @type {Guild}
*/
this.guild = guild;
this.update(data);
}
update(data) {
if(data.name !== undefined) {
/**
* The name of the role
* @type {String}
*/
this.name = data.name;
}
if(data.mentionable !== undefined) {
/**
* Whether the role is mentionable or not
* @type {Boolean}
*/
this.mentionable = data.mentionable;
}
if(data.managed !== undefined) {
/**
* Whether a guild integration manages this role or not
* @type {Boolean}
*/
this.managed = data.managed;
}
if(data.hoist !== undefined) {
/**
* Whether users with this role are hoisted in the user list or not
* @type {Boolean}
*/
this.hoist = data.hoist;
}
if(data.color !== undefined) {
/**
* The hex color of the role in base 10
* @type {Number}
*/
this.color = data.color;
}
if(data.position !== undefined) {
/**
* The position of the role
* @type {Number}
*/
this.position = data.position;
}
if(data.permissions !== undefined) {
/**
* The permissions representation of the role
* @type {Permission}
*/
this.permissions = new Permission(data.permissions);
}
if(data.tags !== undefined) {
/**
* The tags of the role
* @type {Object<string, string | boolean>}
* @prop {String?} bot_id The ID of the bot associated with the role
* @prop {String?} integration_id The ID of the integration associated with the role
* @prop {Boolean?} premium_subscriber Whether the role is the guild's premium subscriber role
*/
this.tags = data.tags;
if(this.tags.guild_connections === null) {
this.tags.guild_connections = true;
}
if(this.tags.premium_subscriber === null) {
this.tags.premium_subscriber = true;
}
if(this.tags.available_for_purchase === null) {
this.tags.available_for_purchase = true;
}
}
if(data.icon !== undefined) {
/**
* The hash of the role's icon, or null if no icon
* @type {String?}
*/
this.icon = data.icon;
}
if(data.unicode_emoji !== undefined) {
/**
* Unicode emoji for the role
* @type {String?}
*/
this.unicodeEmoji = data.unicode_emoji;
}
if(data.flags !== undefined) {
/**
* Role flags. See [Discord's documentation](https://discord.com/developers/docs/topics/permissions#role-object-role-flags) for a list of them
* @type {Number}
*/
this.flags = data.flags;
}
if(data.colors !== undefined) {
/**
* The colors of the role
* @type {Role.RoleColors}
*/
this.colors = {
primaryColor: data.colors.primary_color,
secondaryColor: data.colors.secondary_color,
tertiaryColor: data.colors.tertiary_color
};
}
}
/**
* The URL of the role's icon
* @type {String}
*/
get iconURL() {
return this.icon ? this.guild.shard.client._formatImage(Endpoints.ROLE_ICON(this.id, this.icon)) : null;
}
/**
* Generates a JSON representation of the role permissions
* @type {Object<string, boolean>}
*/
get json() {
return this.permissions.json;
}
/**
* A string that mentions the role
* @type {String}
*/
get mention() {
return `<@&${this.id}>`;
}
/**
* Delete the role
* @param {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
delete(reason) {
return this.guild.shard.client.deleteRole.call(this.guild.shard.client, this.guild.id, this.id, reason);
}
/**
* Get the role's icon with the given format and size
* @param {String} [format] The filetype of the icon ("jpg", "jpeg", "png", "gif", or "webp")
* @param {Number} [size] The size of the icon (any power of two between 16 and 4096)
* @param {Boolean} [forceStatic=false] Whether to force the role icon to be static even if it is animated
* @returns {String?}
*/
dynamicIconURL(format, size, forceStatic) {
return this.icon ? this.guild.shard.client._formatImage(Endpoints.ROLE_ICON(this.id, this.icon), format, size, forceStatic) : null;
}
/**
* Edit the guild role
* @param {Object} options The properties to edit
* @param {Number} [options.color] The hex color of the role, in number form (ex: 0x3da5b3 or 4040115)
* @param {Boolean} [options.hoist] Whether to hoist the role in the user list or not
* @param {String} [options.icon] The role icon as a base64 data URI
* @param {Boolean} [options.mentionable] Whether the role is mentionable or not
* @param {String} [options.name] The name of the role
* @param {BigInt | Number} [options.permissions] The role permissions number
* @param {String?} [options.unicodeEmoji] The role's unicode emoji
* @param {String} [reason] The reason to be displayed in audit logs
* @returns {Promise<Role>}
*/
edit(options, reason) {
return this.guild.shard.client.editRole.call(this.guild.shard.client, this.guild.id, this.id, options, reason);
}
/**
* Edit the role's position. Note that role position numbers are highest on top and lowest at the bottom.
* @param {Number} position The new position of the role
* @returns {Promise}
*/
editPosition(position) {
return this.guild.shard.client.editRolePosition.call(this.guild.shard.client, this.guild.id, this.id, position);
}
toJSON(props = []) {
return super.toJSON([
"color",
"colors",
"hoist",
"icon",
"flags",
"managed",
"mentionable",
"name",
"permissions",
"position",
"tags",
"unicodeEmoji",
...props
]);
}
}
module.exports = Role;
/**
* Represents the colors of a role
* @typedef {Object} Role.RoleColors
* @prop {Number} primaryColor The primary color of the role
* @prop {Number?} [secondaryColor] The secondary color of the role (makes the role gradient)
* @prop {Number?} [tertiaryColor] The tertiary color of the role (makes the role holographic)
*/