-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
78 lines (66 loc) · 1.66 KB
/
index.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
json,
serve,
validateRequest,
} from "https://deno.land/x/sift@0.4.0/mod.ts";
import {
APIApplicationCommandInteraction,
APIPingInteraction,
InteractionResponseType,
InteractionType,
MessageFlags,
} from "https://raw.githubusercontent.com/discordjs/discord-api-types/main/deno/v9.ts";
import { getRedis } from "./redis.ts";
import { verifySignature } from "./verify.ts";
serve({
"/": home,
});
async function home(request: Request) {
const { error } = await validateRequest(request, {
POST: {
headers: ["X-Signature-Ed25519", "X-Signature-Timestamp"],
},
});
if (error) {
return json({ error: error.message }, { status: error.status });
}
const { valid, body } = await verifySignature(request);
if (!valid) {
return json(
{ error: "Invalid request" },
{
status: 401,
}
);
}
const interaction = (await JSON.parse(body)) as
| APIPingInteraction
| APIApplicationCommandInteraction;
if (interaction.type === InteractionType.Ping) {
return json({
type: InteractionResponseType.Pong,
});
}
switch (interaction.data.name) {
case "command here!": {
// Do something!
const data = await getRedis("example_key");
return json({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
flags: MessageFlags.Ephemeral,
content: data,
},
});
}
default: {
return json({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
flags: MessageFlags.Ephemeral,
content: "Hey! I don't have a command for that yet :(",
},
});
}
}
}