Add missing cdn functions, dynamic image format

This commit is contained in:
Helloyunho 2021-03-04 21:13:50 +09:00
parent d26059fdc8
commit 88519d9420
4 changed files with 82 additions and 8 deletions

View File

@ -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}`
}

View File

@ -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<Emoji> {
if (this.id === null) throw new Error('Emoji ID is not valid.')

View File

@ -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
*/

View File

@ -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'