From 1dd0e589d0556c4f56fe3da3c7ca5c42f108100b Mon Sep 17 00:00:00 2001 From: xhyrom Date: Tue, 21 Dec 2021 16:10:57 +0100 Subject: [PATCH] support for custom emojis --- src/bot/bot.ts | 14 +++++++++----- src/bot/snowflakeUtils.ts | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 src/bot/snowflakeUtils.ts diff --git a/src/bot/bot.ts b/src/bot/bot.ts index 31d46ff..06f8adb 100644 --- a/src/bot/bot.ts +++ b/src/bot/bot.ts @@ -1,5 +1,6 @@ import { APIApplicationCommandInteraction, APIInteractionResponse, APIMessageComponentInteraction, APIPingInteraction, InteractionResponseType, InteractionType, MessageFlags, RouteBases, Routes } from 'discord-api-types/v9'; import { isJSON } from './isJson'; +import { isSnowflake } from './snowflakeUtils'; import { verify } from './verify'; const respond = (response: APIInteractionResponse) => new Response(JSON.stringify(response), {headers: {'content-type': 'application/json'}}) @@ -49,16 +50,19 @@ export const handleRequest = async(request: Request): Promise => { if (!roles || Object.values(json.roles).filter((role: any) => role.id && role.label).length === 0 || roles.length === 0 || roles.length > 25) return badFormatting(roles.length > 25); roles = roles.map((r: any) => { - return { + let o: any = { type: 2, style: r.style || 2, label: r.label, - emoji: { - id: null, - name: r.emoji - }, custom_id: r.id } + + if (r.emoji) { + if (isSnowflake(r.emoji)) o.emoji = { id: r.emoji, name: null }; + else o.emoji = { id: null, name: r.emoji }; + } + + return o; }) const finalComponents = []; diff --git a/src/bot/snowflakeUtils.ts b/src/bot/snowflakeUtils.ts new file mode 100644 index 0000000..a1e01c5 --- /dev/null +++ b/src/bot/snowflakeUtils.ts @@ -0,0 +1,15 @@ +export const toSnowflake = (snowflake: number, epoch = DISCORD_EPOCH) => { + return new Date(snowflake / 4194304 + epoch) +} + +export const DISCORD_EPOCH = 1420070400000; + +export const isSnowflake = (snowflake: number, epoch?: number) => { + if (!Number.isInteger(+snowflake)) return false; + if (snowflake < 4194304) return false; + + const timestamp = toSnowflake(snowflake, epoch); + if (isNaN(timestamp.getTime())) return false; + + return true; +} \ No newline at end of file