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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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"),
);
// Get the user calling this slash command
const userId = await body.member?.user?.id;
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()}`,
},
});
if (body.data?.name === "balance")
return Response.json({
type: 4,
data: {
content: `You have 0 <:jopacoin:954159801049440297>`,
},
});
if (body.data?.name === "tip") {
// Get the user
// Check if the number is greater than 0
if (body.data.options[1].value <= 0) {
// Tell them they cannot do this
return Response.json({
type: 4,
data: {
content: `You cannot tip less than 0 <:jopacoin:954159801049440297>, <@${userId}>. Nice try though.`,
},
});
}
}
// Parse the first one to see if it is a user on this channel
if (body.data.options[0].type === 6)
return Response.json({
type: 4,
data: {
content: ` has tipped <@${body.data.options[0].value}> 0 <:jopacoin:954159801049440297>`,
},
});
return new Response("Bad request", {
status: 400,
statusText: "Bad request",
});
}
return new Response("Not handled", { status: 422 });
};