From 4a3574b946b7c2d1a8a1189e40fb212a09138a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9B=90=EB=8D=94?= Date: Mon, 11 Jan 2021 13:03:00 +0900 Subject: [PATCH] feat: added CDN methods --- utils/Constants.ts | 18 +++++++++++++++++- utils/Tools.ts | 12 +++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/utils/Constants.ts b/utils/Constants.ts index dec3ee4..95ddb96 100644 --- a/utils/Constants.ts +++ b/utils/Constants.ts @@ -1,3 +1,6 @@ +import { ImageOptions } from '../types' +import { makeImageURL } from './Tools' + export const Status = { online: { text: '온라인', @@ -88,4 +91,17 @@ export const reportCats = [ '기타', ] -export const git = { 'github.com': { icon: 'github', text: 'Github' }, 'gitlab.com': { icon: 'gitlab', text: 'Gitlab' }} \ No newline at end of file +export const BASE_URLs = { + cdn: 'https://cdn.discordapp.com' +} +export const DiscordEnpoints = { + CDN: class CDN { + static root = BASE_URLs.cdn + static emoji (id: string, options:ImageOptions={}) { return makeImageURL(`${this.root}/emojis/${id}`, options) } + static guild (id: string, hash: string, options:ImageOptions={}) { return makeImageURL(`${this.root}/icons/${id}/${hash}`, options) } + static default (tag: string, options:ImageOptions={}) { return makeImageURL(`${this.root}/embed/avatars/${isNaN(Number(tag)) ? Number(tag) % 5 : 0}`, options) } + static user (id: string, hash: string, options:ImageOptions={}) { return makeImageURL(`${this.root}/avatars/${id}/${hash}`, options) } + } +} + +export const git = { 'github.com': { icon: 'github', text: 'Github' }, 'gitlab.com': { icon: 'gitlab', text: 'Gitlab' }} diff --git a/utils/Tools.ts b/utils/Tools.ts index 6d89a83..589e843 100644 --- a/utils/Tools.ts +++ b/utils/Tools.ts @@ -1,7 +1,7 @@ -import { UserPemissionFlags } from '../types' -import { perms } from './Constants' +import { ImageFormat, ImageOptions, ImageSize, UserPemissionFlags } from '../types' +import { BASE_URLs, perms } from './Constants' -function formatNumber(value: number):string { +export function formatNumber(value: number):string { const suffixes = ['', '만', '억', '조','해'] const suffixNum = Math.floor((''+value).length/4) let shortValue:string|number = parseFloat((suffixNum != 0 ? (value / Math.pow(10000,suffixNum)) : value).toPrecision(2)) @@ -12,10 +12,12 @@ function formatNumber(value: number):string { return shortValue+suffixes[suffixNum] } -function checkPerm(base: number, required: number | UserPemissionFlags):boolean { +export function checkPerm(base: number, required: number | UserPemissionFlags):boolean { required = typeof required === 'number' ? required : perms[required] if (typeof required !== 'number' && !required) throw new Error('올바르지 않은 권한입니다.') return (base & required) === required } -export { formatNumber, checkPerm } +export function makeImageURL(root:string, { format='png', size=256 }:ImageOptions):string { + return `${root}.${format}?size=${size}` +} \ No newline at end of file