resolveEmoji utils

This commit is contained in:
xhyrom 2022-04-10 21:09:37 +02:00
parent da0e805c05
commit 7b8256f86d
6 changed files with 23 additions and 23 deletions

View file

@ -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<Response> => {
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;
});

View file

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

View file

@ -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(/<?(?:(a):)?(\w{2,32}):(\d{17,19})?>?/);
return match && { animated: Boolean(match[1]), name: match[2], id: match[3] ?? null };
}

View file

@ -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;
});