diff --git a/packages/bot/bot.ts b/packages/bot/bot.ts index b6433f3..fd681dd 100644 --- a/packages/bot/bot.ts +++ b/packages/bot/bot.ts @@ -1,7 +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'; +import { isJSON } from './utils/isJson'; +import { verify } from './utils/verify'; const respond = (response: APIInteractionResponse) => new Response(JSON.stringify(response), {headers: {'content-type': 'application/json'}}); @@ -58,10 +57,7 @@ export const handleRequest = async(request: Request): Promise => { 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 }; - } + if (r.emoji) o.emoji = resolvePartialEmoji(r.emoji); return o; }); diff --git a/packages/bot/snowflakeUtils.ts b/packages/bot/snowflakeUtils.ts deleted file mode 100644 index 9280866..0000000 --- a/packages/bot/snowflakeUtils.ts +++ /dev/null @@ -1,15 +0,0 @@ -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 diff --git a/packages/bot/isJson.ts b/packages/bot/utils/isJson.ts similarity index 100% rename from packages/bot/isJson.ts rename to packages/bot/utils/isJson.ts diff --git a/packages/bot/utils/resolveEmoji.ts b/packages/bot/utils/resolveEmoji.ts new file mode 100644 index 0000000..0eac725 --- /dev/null +++ b/packages/bot/utils/resolveEmoji.ts @@ -0,0 +1,19 @@ +/** + * https://github.com/discordjs/discord.js/blob/3c0bbac82fa9988af4a62ff00c66d149fbe6b921/packages/discord.js/src/util/Util.js#L292-L311 + */ + +const resolvePartialEmoji = (emoji: string) => { + if (!emoji) return null; + if (typeof emoji === 'string') return /^\d{17,19}$/.test(emoji) ? { id: emoji } : parseEmoji(emoji); + const { id, name, animated } = emoji; + if (!id && !name) return null; + return { id, name, animated: Boolean(animated) }; +} + +const parseEmoji = (text: string) => { + if (text.includes('%')) text = decodeURIComponent(text); + if (!text.includes(':')) return { animated: false, name: text, id: null }; + + const match = text.match(/?/); + return match && { animated: Boolean(match[1]), name: match[2], id: match[3] ?? null }; +} \ No newline at end of file diff --git a/packages/bot/verify.ts b/packages/bot/utils/verify.ts similarity index 100% rename from packages/bot/verify.ts rename to packages/bot/utils/verify.ts diff --git a/packages/website/public/script.js b/packages/website/public/script.js index 118e9c3..27974d6 100644 --- a/packages/website/public/script.js +++ b/packages/website/public/script.js @@ -3,7 +3,7 @@ const json = { }; $('input').change((e) => { - json[e.currentTarget.id] = e.currentTarget.value; + json[e.currentTarget.id] = e.currentTarget.value.replaceAll('\\n', '\n'); document.getElementById('json').innerHTML = hljs.highlight(JSON.stringify(json), { language: 'json' }).value; });