diff options
author | Luca Matei Pintilie <lucafulger@gmail.com> | 2021-11-28 13:05:14 +0000 |
---|---|---|
committer | Luca Matei Pintilie <lucafulger@gmail.com> | 2021-11-28 13:05:14 +0000 |
commit | b4d9b4a1cb44026b67a20653cd92e51001755177 (patch) | |
tree | 25af7d76f0ec5671e120cf1f4b9d94ac7098c369 | |
parent | 6d5b97cfd7a7eed815e69a08847bdd182bd33c07 (diff) | |
download | speedrunbot-slash-b4d9b4a1cb44026b67a20653cd92e51001755177.tar speedrunbot-slash-b4d9b4a1cb44026b67a20653cd92e51001755177.tar.gz speedrunbot-slash-b4d9b4a1cb44026b67a20653cd92e51001755177.tar.bz2 speedrunbot-slash-b4d9b4a1cb44026b67a20653cd92e51001755177.tar.lz speedrunbot-slash-b4d9b4a1cb44026b67a20653cd92e51001755177.tar.xz speedrunbot-slash-b4d9b4a1cb44026b67a20653cd92e51001755177.tar.zst speedrunbot-slash-b4d9b4a1cb44026b67a20653cd92e51001755177.zip |
Reverting to an older version plus autocomplete
-rw-r--r-- | src/pages/api/discord/interactions.ts | 224 |
1 files changed, 65 insertions, 159 deletions
diff --git a/src/pages/api/discord/interactions.ts b/src/pages/api/discord/interactions.ts index d049bc7..4e83872 100644 --- a/src/pages/api/discord/interactions.ts +++ b/src/pages/api/discord/interactions.ts @@ -1,24 +1,15 @@ +// deno-lint-ignore-file no-explicit-any import { ApplicationCommandInteraction, AutocompleteInteraction, - Channel, - Client, decodeText, - Guild, - GuildPayload, HandlerContext, Interaction, - InteractionApplicationCommandData, InteractionApplicationCommandResolved, - InteractionChannel, InteractionPayload, + InteractionResponseType, InteractionsClient, InteractionType, - Member, - Message, - MessageComponentInteraction, - Role, - TextChannel, User, } from "../../../deps_server.ts"; import { SpeedrunCom } from "../../../srcom/slash_commands.ts"; @@ -28,14 +19,19 @@ export const client = new InteractionsClient({ publicKey: Deno.env.get("PUBLIC_KEY"), }); -const bad = new Response(null, { status: 400 }); - client.loadModule(new SpeedrunCom()); +const bad = new Response( + JSON.stringify({ code: 400, message: "Bad request " }), + { + status: 400, + }, +); export const handler = { async POST(ctx: HandlerContext): Promise<Response> { const req = ctx.req; try { + client.verifyServerRequest; if (req.bodyUsed === true) throw new Error("Request Body already used"); if (req.body === null) return bad; const rawbody = (await req.body.getReader().read()).value; @@ -50,155 +46,66 @@ export const handler = { const verify = await client.verifyKey(rawbody, signature, timestamp); if (!verify) return bad; + let res: ApplicationCommandInteraction | Interaction; try { const payload: InteractionPayload = JSON.parse(decodeText(rawbody)); - - // Note: there's a lot of hacks going on here. - - const clientScoped = client as unknown as Client; - - let res; - - const channel = payload.channel_id !== undefined - ? (new Channel(clientScoped, { - id: payload.channel_id!, - type: 0, - }) as unknown as TextChannel) - : undefined; - - const user = new User( - clientScoped, - (payload.member?.user ?? payload.user)!, - ); - - const guild = payload.guild_id !== undefined - ? // eslint-disable-next-line @typescript-eslint/consistent-type-assertions - new Guild(clientScoped, { - id: payload.guild_id!, - unavailable: true, - } as GuildPayload) - : undefined; - - const member = payload.member !== undefined - ? // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - new Member(clientScoped, payload.member, user, guild!) - : undefined; - - if ( - payload.type === InteractionType.APPLICATION_COMMAND || - payload.type === InteractionType.AUTOCOMPLETE - ) { - const resolved: InteractionApplicationCommandResolved = { - users: {}, - members: {}, - roles: {}, - channels: {}, - messages: {}, - }; - - for ( - const [id, data] of Object.entries( - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - (payload.data as InteractionApplicationCommandData).resolved - ?.users ?? - {}, - ) - ) { - resolved.users[id] = new User(clientScoped, data); - } - - for ( - const [id, data] of Object.entries( - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - (payload.data as InteractionApplicationCommandData).resolved - ?.members ?? {}, - ) - ) { - resolved.members[id] = new Member( - clientScoped, - data, - resolved.users[id], - undefined as unknown as Guild, - ); - } - - for ( - const [id, data] of Object.entries( - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - (payload.data as InteractionApplicationCommandData).resolved - ?.roles ?? - {}, - ) - ) { - resolved.roles[id] = new Role( - clientScoped, - data, - undefined as unknown as Guild, - ); - } - - for ( - const [id, data] of Object.entries( - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - (payload.data as InteractionApplicationCommandData).resolved - ?.channels ?? {}, - ) - ) { - resolved.channels[id] = new InteractionChannel(clientScoped, data); - } - - for ( - const [id, data] of Object.entries( - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - (payload.data as InteractionApplicationCommandData).resolved - ?.messages ?? {}, - ) - ) { - resolved.messages[id] = new Message( - clientScoped, - data, - data.channel_id as unknown as TextChannel, - new User(clientScoped, data.author), - ); - } - - res = payload.type === InteractionType.APPLICATION_COMMAND - ? new ApplicationCommandInteraction(clientScoped, payload, { - user, - member, - guild, - channel, - resolved, - }) - : new AutocompleteInteraction(clientScoped, payload, { - user, - member, - guild, - channel, - resolved, - }); - } else if (payload.type === InteractionType.MESSAGE_COMPONENT) { - res = new MessageComponentInteraction(clientScoped, payload, { - channel, - guild, - member, - user, - message: new Message( - clientScoped, - payload.message!, - payload.message!.channel_id as unknown as TextChannel, - new User(clientScoped, payload.message!.author), + if (payload.type === InteractionType.APPLICATION_COMMAND) { + res = new ApplicationCommandInteraction(client as any, payload, { + user: new User( + client as any, + (payload.member?.user ?? payload.user)!, ), + member: payload.member as any, + guild: payload.guild_id as any, + channel: payload.channel_id as any, + resolved: ((payload.data as any) + ?.resolved as unknown as InteractionApplicationCommandResolved) ?? + { + users: {}, + members: {}, + roles: {}, + channels: {}, + }, + }); + } else if (payload.type === InteractionType.AUTOCOMPLETE) { + res = new AutocompleteInteraction(client as any, payload, { + user: new User( + client as any, + (payload.member?.user ?? payload.user)!, + ), + member: payload.member as any, + guild: payload.guild_id as any, + channel: payload.channel_id as any, + resolved: ((payload.data as any) + ?.resolved as unknown as InteractionApplicationCommandResolved) ?? + { + users: {}, + members: {}, + roles: {}, + channels: {}, + }, }); } else { - res = new Interaction(clientScoped, payload, { - user, - member, - guild, - channel, + res = new Interaction(client as any, payload, { + user: new User( + client as any, + (payload.member?.user ?? payload.user)!, + ), + member: payload.member as any, + guild: payload.guild_id as any, + channel: payload.channel_id as any, }); } + if (res.type === InteractionType.PING) { + await res.respond({ type: InteractionResponseType.PONG }); + client.emit("ping"); + return bad; + } + + await client.emit("interaction", res); + await (client as any)._process(res); + return new Response( res instanceof FormData ? res : JSON.stringify(res), { @@ -211,14 +118,13 @@ export const handler = { }, ); } catch (e) { - await client.emit("interactionError", e as Error); + console.log(e); return bad; } } catch (e) { - await client.emit("interactionError", e as Error); - return new Response(e, { - status: 500, - }); + console.log(e); + await client.emit("interactionError", e); + return bad; } }, }; |