aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Matei Pintilie <lucafulger@gmail.com>2021-11-28 15:53:31 +0000
committerLuca Matei Pintilie <lucafulger@gmail.com>2021-11-28 15:53:31 +0000
commit9a185ca463d653dc1cbecf007187ec1e4040bb46 (patch)
tree15c16bec39fed3fa40fa8e4a79cded2b1e8dd0ec
parent05f3b1e6b5b9b0cec08d58fc95cd33f25cf9eec8 (diff)
downloadspeedrunbot-slash-9a185ca463d653dc1cbecf007187ec1e4040bb46.tar
speedrunbot-slash-9a185ca463d653dc1cbecf007187ec1e4040bb46.tar.gz
speedrunbot-slash-9a185ca463d653dc1cbecf007187ec1e4040bb46.tar.bz2
speedrunbot-slash-9a185ca463d653dc1cbecf007187ec1e4040bb46.tar.lz
speedrunbot-slash-9a185ca463d653dc1cbecf007187ec1e4040bb46.tar.xz
speedrunbot-slash-9a185ca463d653dc1cbecf007187ec1e4040bb46.tar.zst
speedrunbot-slash-9a185ca463d653dc1cbecf007187ec1e4040bb46.zip
Try again with a simple handler
-rw-r--r--src/deps_server.ts4
-rw-r--r--src/pages/api/discord/interactions.ts123
2 files changed, 15 insertions, 112 deletions
diff --git a/src/deps_server.ts b/src/deps_server.ts
index 4e824a9..cef3760 100644
--- a/src/deps_server.ts
+++ b/src/deps_server.ts
@@ -1,5 +1,5 @@
-export * from "https://code.harmony.rocks/bc7bebc/mod.ts";
-export * from "https://code.harmony.rocks/bc7bebc/src/utils/encoding.ts";
+export * from "https://code.harmony.rocks/08dc7b99089d94d62d6c87bf26e279746dfe418a/mod.ts";
+export * from "https://code.harmony.rocks/08dc7b99089d94d62d6c87bf26e279746dfe418a/src/utils/encoding.ts";
export { start } from "https://raw.githubusercontent.com/lucacasonato/fresh/d1076b0ad1b420aec689324b3342e543c0d5a591/server.ts";
export type { HandlerContext } from "https://raw.githubusercontent.com/lucacasonato/fresh/d1076b0ad1b420aec689324b3342e543c0d5a591/server.ts";
import "https://deno.land/x/dot_env@0.2.0/load.ts";
diff --git a/src/pages/api/discord/interactions.ts b/src/pages/api/discord/interactions.ts
index 17c0083..58f9d34 100644
--- a/src/pages/api/discord/interactions.ts
+++ b/src/pages/api/discord/interactions.ts
@@ -1,17 +1,4 @@
-// deno-lint-ignore-file no-explicit-any
-import {
- ApplicationCommandInteraction,
- AutocompleteInteraction,
- decodeText,
- HandlerContext,
- Interaction,
- InteractionApplicationCommandResolved,
- InteractionPayload,
- InteractionResponseType,
- InteractionsClient,
- InteractionType,
- User,
-} from "../../../deps_server.ts";
+import { HandlerContext, InteractionsClient } from "../../../deps_server.ts";
import { SpeedrunCom } from "../../../srcom/slash_commands.ts";
export const client = new InteractionsClient({
@@ -21,104 +8,20 @@ export const client = new InteractionsClient({
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> {
+ 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;
- if (rawbody === undefined) return bad;
-
- if (req.method.toLowerCase() !== "post") return bad;
-
- const signature = req.headers.get("x-signature-ed25519");
- const timestamp = req.headers.get("x-signature-timestamp");
- if (signature === null || timestamp === null) return bad;
-
- const verify = await client.verifyKey(rawbody, signature, timestamp);
- if (!verify) return bad;
-
- let res: ApplicationCommandInteraction | Interaction;
- try {
- const payload: InteractionPayload = JSON.parse(decodeText(rawbody));
- 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) {
- console.log(payload);
- 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,
- });
- } else {
- 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),
- {
- status: 200,
- headers: new Headers({
- "content-type": res instanceof FormData
- ? "multipart/form-data"
- : "application/json",
- }),
- },
- );
- } catch (e) {
- console.log(e);
- return bad;
+ // deno-lint-ignore no-async-promise-executor
+ return new Promise(async (res) => {
+ const interaction = await client.verifyFetchEvent({
+ respondWith: res,
+ request: req,
+ });
+ if (interaction === false) {
+ return res(new Response(null, { status: 401 }));
}
- } catch (e) {
- console.log(e);
- await client.emit("interactionError", e);
- return bad;
- }
+ if (interaction.type === 1) return interaction.respond({ type: 1 });
+ await client._process(interaction);
+ });
},
};