From 88519d942049dd021a3e6e48a89202a48b64bbcf Mon Sep 17 00:00:00 2001 From: Helloyunho Date: Thu, 4 Mar 2021 21:13:50 +0900 Subject: [PATCH] Add missing cdn functions, dynamic image format --- src/structures/cdn.ts | 12 +++++---- src/structures/emoji.ts | 16 ++++++++++- src/structures/guild.ts | 60 ++++++++++++++++++++++++++++++++++++++++- src/types/cdn.ts | 2 +- 4 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/structures/cdn.ts b/src/structures/cdn.ts index 83ff27c..78c8495 100644 --- a/src/structures/cdn.ts +++ b/src/structures/cdn.ts @@ -3,11 +3,13 @@ import { ImageFormats, ImageSize } from '../types/cdn.ts' /** Function to get Image URL from a resource on Discord CDN */ export const ImageURL = ( url: string, - format: ImageFormats | undefined = 'png', - size: ImageSize | undefined = 128 + format: ImageFormats = 'png', + size: ImageSize = 128 ): string => { - size = size === undefined ? 128 : size if (url.includes('a_')) { - return `${url}.${format === undefined ? 'gif' : format}?size=${size}` - } else return `${url}.${format === 'gif' ? 'png' : format}?size=${size}` + return `${url}.${format === 'dynamic' ? 'gif' : format}?size=${size}` + } else + return `${url}.${ + format === 'gif' || format === 'dynamic' ? 'png' : format + }?size=${size}` } diff --git a/src/structures/emoji.ts b/src/structures/emoji.ts index fe71889..19cd8e1 100644 --- a/src/structures/emoji.ts +++ b/src/structures/emoji.ts @@ -1,8 +1,10 @@ import { Client } from '../models/client.ts' +import { ImageSize } from '../types/cdn.ts' import { EmojiPayload } from '../types/emoji.ts' -import { EMOJI } from '../types/endpoint.ts' +import { CUSTOM_EMOJI, EMOJI } from '../types/endpoint.ts' import { Snowflake } from '../utils/snowflake.ts' import { Base } from './base.ts' +import { ImageURL } from './cdn.ts' import { Guild } from './guild.ts' import { Role } from './role.ts' import { User } from './user.ts' @@ -54,6 +56,18 @@ export class Emoji extends Base { this.available = data.available } + /** + * Gets emoji image URL + */ + emojiImageURL( + format: 'png' | 'gif' | 'dynamic' = 'png', + size: ImageSize = 512 + ): string | undefined { + return this.id != null + ? `${ImageURL(CUSTOM_EMOJI(this.id), format, size)}` + : undefined + } + /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. Returns the updated emoji object on success. Fires a Guild Emojis Update Gateway event. */ async edit(data: ModifyGuildEmojiParams): Promise { if (this.id === null) throw new Error('Emoji ID is not valid.') diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 11a9d93..81bbca7 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -32,9 +32,13 @@ import { User } from './user.ts' import { Application } from './application.ts' import { GUILD_BAN, + GUILD_BANNER, GUILD_BANS, + GUILD_DISCOVERY_SPLASH, + GUILD_ICON, GUILD_INTEGRATIONS, - GUILD_PRUNE + GUILD_PRUNE, + GUILD_SPLASH } from '../types/endpoint.ts' import { GuildVoiceStatesManager } from '../managers/guildVoiceStates.ts' import { RequestMembersOptions } from '../gateway/index.ts' @@ -42,6 +46,8 @@ import { GuildPresencesManager } from '../managers/presences.ts' import { TemplatePayload } from '../types/template.ts' import { Template } from './template.ts' import { DiscordAPIError } from '../models/rest.ts' +import { ImageFormats, ImageSize } from '../types/cdn.ts' +import { ImageURL } from './cdn.ts' export class GuildBan extends Base { guild: Guild @@ -258,6 +264,58 @@ export class Guild extends SnowflakeBase { } } + /** + * Gets guild icon URL + */ + iconURL( + format: ImageFormats = 'png', + size: ImageSize = 512 + ): string | undefined { + return this.icon != null + ? `${ImageURL(GUILD_ICON(this.id, this.icon), format, size)}` + : undefined + } + + /** + * Gets guild splash URL + */ + splashURL( + format: ImageFormats = 'png', + size: ImageSize = 512 + ): string | undefined { + return this.splash != null + ? `${ImageURL(GUILD_SPLASH(this.id, this.splash), format, size)}` + : undefined + } + + /** + * Gets guild discover splash URL + */ + discoverSplashURL( + format: ImageFormats = 'png', + size: ImageSize = 512 + ): string | undefined { + return this.discoverySplash != null + ? `${ImageURL( + GUILD_DISCOVERY_SPLASH(this.id, this.discoverySplash), + format, + size + )}` + : undefined + } + + /** + * Gets guild banner URL + */ + bannerURL( + format: ImageFormats = 'png', + size: ImageSize = 512 + ): string | undefined { + return this.banner != null + ? `${ImageURL(GUILD_BANNER(this.id, this.banner), format, size)}` + : undefined + } + /** * Gets Everyone role of the Guild */ diff --git a/src/types/cdn.ts b/src/types/cdn.ts index 4fbfc13..b78bd48 100644 --- a/src/types/cdn.ts +++ b/src/types/cdn.ts @@ -1,2 +1,2 @@ export type ImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 -export type ImageFormats = 'jpg' | 'jpeg' | 'png' | 'webp' | 'gif' +export type ImageFormats = 'jpg' | 'jpeg' | 'png' | 'webp' | 'gif' | 'dynamic'