support for custom emojis

This commit is contained in:
xhyrom 2021-12-21 16:10:57 +01:00
parent 0a2480a5ad
commit 1dd0e589d0
2 changed files with 24 additions and 5 deletions

View file

@ -1,5 +1,6 @@
import { APIApplicationCommandInteraction, APIInteractionResponse, APIMessageComponentInteraction, APIPingInteraction, InteractionResponseType, InteractionType, MessageFlags, RouteBases, Routes } from 'discord-api-types/v9'; import { APIApplicationCommandInteraction, APIInteractionResponse, APIMessageComponentInteraction, APIPingInteraction, InteractionResponseType, InteractionType, MessageFlags, RouteBases, Routes } from 'discord-api-types/v9';
import { isJSON } from './isJson'; import { isJSON } from './isJson';
import { isSnowflake } from './snowflakeUtils';
import { verify } from './verify'; import { verify } from './verify';
const respond = (response: APIInteractionResponse) => new Response(JSON.stringify(response), {headers: {'content-type': 'application/json'}}) 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<Response> => {
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); 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) => { roles = roles.map((r: any) => {
return { let o: any = {
type: 2, type: 2,
style: r.style || 2, style: r.style || 2,
label: r.label, label: r.label,
emoji: {
id: null,
name: r.emoji
},
custom_id: r.id 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 = []; const finalComponents = [];

15
src/bot/snowflakeUtils.ts Normal file
View file

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