Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add role icon properties to ResolvedRole #147

Merged
merged 3 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,13 @@ export interface ResolvedRole {
color: number;
hoist: boolean;
id: string;
icon?: string;
managed: boolean;
mentionable: boolean;
name: string;
permissions: string;
position: number;
unicode_emoji?: string;
}

/** @private */
Expand Down Expand Up @@ -702,7 +704,8 @@ export const Endpoints = {

// CDN
DEFAULT_USER_AVATAR: (userDiscriminator: string | number) => `/embed/avatars/${userDiscriminator}`,
USER_AVATAR: (userID: string, userAvatar: string) => `/avatars/${userID}/${userAvatar}`
USER_AVATAR: (userID: string, userAvatar: string) => `/avatars/${userID}/${userAvatar}`,
ROLE_ICON: (roleID: string, roleIcon: string) => `/role-icons/${roleID}/${roleIcon}`
};

// SlashCreator events for documentation.
Expand Down
2 changes: 1 addition & 1 deletion src/structures/interfaces/commandContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class CommandContext extends MessageInteractionContext {
);
if (data.data.resolved.roles)
Object.keys(data.data.resolved.roles).forEach((id) =>
this.roles.set(id, new Role(data.data.resolved!.roles![id]))
this.roles.set(id, new Role(data.data.resolved!.roles![id], this.creator))
);
if (data.data.resolved.channels)
Object.keys(data.data.resolved.channels).forEach((id) =>
Expand Down
39 changes: 37 additions & 2 deletions src/structures/role.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global BigInt */
import { ResolvedRole } from '../constants';
import { CDN_URL, Endpoints, ImageFormat, ImageFormats, ImageSizeBoundaries, ResolvedRole } from '../constants';
import { SlashCreator } from '../creator';
import { Permissions } from './permissions';

/** Represents a resolved role object. */
Expand All @@ -14,28 +15,62 @@ export class Role {
readonly color: number;
/** Whether the role is being hoisted */
readonly hoist: boolean;
/** The role icon hash */
readonly icon?: string;
/** Whether the role is being managed by an application */
readonly managed: boolean;
/** Whether the role is mentionable by everyone */
readonly mentionable: boolean;
/** The role unicode emoji */
readonly unicodeEmoji?: string;

/** The creator of the role class. */
private _creator: SlashCreator;

private _permissionsBitfield?: Permissions;
private _permissions: string;

/**
* @param data The data for the member
*/
constructor(data: ResolvedRole) {
constructor(data: ResolvedRole, creator: SlashCreator) {
this._creator = creator;

this.id = data.id;
this.name = data.name;
this.position = data.position;
this.color = data.color;
this.hoist = data.hoist;
if (data.icon) this.icon = data.icon;
this.managed = data.managed;
this.mentionable = data.mentionable;
if (data.unicode_emoji) this.unicodeEmoji = data.unicode_emoji;
this._permissions = data.permissions;
}

/** The URL of the role icon. */
get iconURL() {
return this.dynamicIconURL();
}

/**
* Get the role's icon with the given format and size.
* @param format The format of the icon
* @param size The size of the icon
*/
dynamicIconURL(format?: ImageFormat, size?: number) {
if (!this.icon) return null;
if (!format || !ImageFormats.includes(format.toLowerCase())) {
format = this._creator.options.defaultImageFormat;
}

if (!size || size < ImageSizeBoundaries.MINIMUM || size > ImageSizeBoundaries.MAXIMUM) {
size = this._creator.options.defaultImageSize;
}

return `${CDN_URL}${Endpoints.ROLE_ICON(this.id, this.icon)}.${format}?size=${size}`;
}

/** The string that mentions this role. */
get mention() {
return `<@&${this.id}>`;
Expand Down