Public
HTTP
xi8s-handlediscordinteraction.web.val.run
June 12, 2024
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { verify_discord_signature } from "https://esm.town/v/mattx/verify_discord_signature?v=8";
export const handleDiscordInteraction = async (req: Request) => {
if (req.method === "GET") return new Response("GET Method Not Allowed", { status: 405 });
const body = await req.json();
const verified = await verify_discord_signature(
Deno.env.get("discordPublicKey"),
JSON.stringify(body),
req.headers.get("X-Signature-Ed25519"),
req.headers.get("X-Signature-Timestamp"),
);
if (!verified)
return new Response("signature invalid", {
status: 401,
statusText: "signature invalid",
});
// PING
if (body.type === 1)
return Response.json({ type: 1 }); // PONG
// APPLICATION_COMMAND interactions
if (body.type === 2) {
if (body.data?.name === "ping")
return Response.json({
type: 4,
data: {
content: `Pong! It is ${new Date()}`,
},
});
return new Response("Bad request", {
status: 400,
statusText: "Bad request",
});
}
return new Response("Not handled", { status: 422 });
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 12, 2024