Skip to content

Commit

Permalink
refactor: use client.rest.cdn over raw URLs (skyra-project#2550)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet authored Jan 13, 2024
1 parent 0d2d01f commit 89422d1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 35 deletions.
5 changes: 5 additions & 0 deletions src/lib/util/functions/emojis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { formatEmoji } from '@discordjs/builders';
import { FormattedCustomEmojiWithGroups, TwemojiRegex } from '@sapphire/discord-utilities';
import { container } from '@sapphire/framework';
import { isNullish } from '@sapphire/utilities';

// Based on the identifiers at https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/72x72/
Expand Down Expand Up @@ -31,6 +32,10 @@ export function getTwemojiUrl<E extends EncodedTwemoji>(emoji: E) {
return `https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/72x72/${emoji}.png` as const;
}

export function getCustomEmojiUrl(id: string, animated: boolean) {
return container.client.rest.cdn.emoji(id, { extension: animated ? 'gif' : 'png', size: 64 });
}

interface EmojiObjectPartial {
name: string | null;
id: string | null;
Expand Down
35 changes: 6 additions & 29 deletions src/lib/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LanguageKeys } from '#lib/i18n/languageKeys';
import type { GuildMessage } from '#lib/types';
import { BrandingColors, ZeroWidthSpace } from '#lib/util/constants';
import { EmbedBuilder } from '@discordjs/builders';
import { container } from '@sapphire/framework';
import { send } from '@sapphire/plugin-editable-commands';
import type { TFunction } from '@sapphire/plugin-i18next';
import { isNullishOrEmpty, tryParseURL, type Nullish } from '@sapphire/utilities';
Expand All @@ -11,8 +12,7 @@ import {
type EmbedAuthorData,
type Guild,
type GuildChannel,
type ImageExtension,
type ImageSize,
type ImageURLOptions,
type Message,
type MessageMentionTypes,
type ThreadChannel,
Expand Down Expand Up @@ -143,36 +143,13 @@ export function usesPomelo(user: User | APIUser) {
return isNullishOrEmpty(user.discriminator) || user.discriminator === '0';
}

/**
* The options used for image URLs, ported from {@link https://github.com/discordjs/discord.js/blob/main/packages/rest/src/lib/CDN.ts}.
*/
export interface AvatarOptions {
/**
* The extension to use for the image URL
*
* @defaultValue `'webp'`
*/
extension?: ImageExtension;
/**
* The size specified in the image URL
*/
size?: ImageSize;
/**
* Whether or not to prefer the static version of an image asset.
*/
forceStatic?: boolean;
}

const ROOT = 'https://cdn.discordapp.com';
export function getDisplayAvatar(user: User | APIUser, options: AvatarOptions = {}) {
export function getDisplayAvatar(user: User | APIUser, options?: Readonly<ImageURLOptions>) {
if (user.avatar === null) {
const id = (usesPomelo(user) ? (BigInt(user.id) >> 22n) % 6n : Number(user.discriminator) % 5).toString();
return `${ROOT}/embed/avatars/${id}.png`;
const id = usesPomelo(user) ? Number(BigInt(user.id) >> 22n) % 6 : Number(user.discriminator) % 5;
return container.client.rest.cdn.defaultAvatar(id);
}

const extension = !options.forceStatic && user.avatar.startsWith('a_') ? 'gif' : options.extension ?? 'webp';
const size = typeof options.size === 'undefined' ? '' : `?size=${options.size}`;
return `${ROOT}/avatars/${user.id}/${user.avatar}.${extension}${size}`;
return container.client.rest.cdn.avatar(user.id, user.avatar, options);
}

export function getTag(user: User | APIUser) {
Expand Down
6 changes: 3 additions & 3 deletions src/listeners/reactions/rawReactionAddBlockList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Events } from '#lib/types';
import type { LLRCData } from '#utils/LongLivingReactionCollector';
import { floatPromise, seconds } from '#utils/common';
import { Colors } from '#utils/constants';
import { deleteMessage, getEmojiReactionFormat, getEncodedTwemoji, getTwemojiUrl, type SerializedEmoji } from '#utils/functions';
import { deleteMessage, getCustomEmojiUrl, getEmojiReactionFormat, getEncodedTwemoji, getTwemojiUrl, type SerializedEmoji } from '#utils/functions';
import { getFullEmbedAuthor } from '#utils/util';
import { EmbedBuilder } from '@discordjs/builders';
import { ApplyOptions } from '@sapphire/decorators';
Expand Down Expand Up @@ -84,9 +84,9 @@ export class UserModerationEvent extends ModerationListener<ArgumentType, unknow
.setColor(Colors.Red)
.setAuthor(getFullEmbedAuthor(user))
.setThumbnail(
data.emoji.id === null
data.emoji.id === null //
? getTwemojiUrl(getEncodedTwemoji(data.emoji.name!))
: `https://cdn.discordapp.com/emojis/${data.emoji.id}.${data.emoji.animated ? 'gif' : 'png'}?size=64`
: getCustomEmojiUrl(data.emoji.id, data.emoji.animated)
)
.setDescription(`[${t(LanguageKeys.Misc.JumpTo)}](https://discord.com/channels/${data.guild.id}/${data.channel.id}/${data.messageId})`)
.setFooter({ text: `${data.channel.name} | ${t(LanguageKeys.Events.Reactions.FilterFooter)}` })
Expand Down
6 changes: 3 additions & 3 deletions src/listeners/reactions/rawReactionAddNotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LanguageKeys } from '#lib/i18n/languageKeys';
import { Events } from '#lib/types';
import type { LLRCData } from '#utils/LongLivingReactionCollector';
import { Colors } from '#utils/constants';
import { getEmojiId, getEmojiReactionFormat, getEncodedTwemoji, getTwemojiUrl, type SerializedEmoji } from '#utils/functions';
import { getCustomEmojiUrl, getEmojiId, getEmojiReactionFormat, getEncodedTwemoji, getTwemojiUrl, type SerializedEmoji } from '#utils/functions';
import { getFullEmbedAuthor } from '#utils/util';
import { EmbedBuilder } from '@discordjs/builders';
import { ApplyOptions } from '@sapphire/decorators';
Expand Down Expand Up @@ -57,9 +57,9 @@ export class UserListener extends Listener {
.setColor(Colors.Green)
.setAuthor(getFullEmbedAuthor(user))
.setThumbnail(
data.emoji.id === null
data.emoji.id === null //
? getTwemojiUrl(getEncodedTwemoji(data.emoji.name!))
: `https://cdn.discordapp.com/emojis/${data.emoji.id}.${data.emoji.animated ? 'gif' : 'png'}?size=64`
: getCustomEmojiUrl(data.emoji.id, data.emoji.animated)
)
.setDescription(
[
Expand Down

0 comments on commit 89422d1

Please sign in to comment.