-
Notifications
You must be signed in to change notification settings - Fork 10
/
mod.ts
116 lines (104 loc) · 3.21 KB
/
mod.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
json,
serve,
validateRequest
} from "sift/mod.ts";
import {
Interaction,
InteractionResponseTypes,
InteractionTypes,
verifySignature
} from "discordeno/mod.ts";
import { camelize } from 'https://deno.land/x/[email protected]/mod.ts';
import { commands } from "template/commands/mod.ts";
import { translate } from "template/languages/mod.ts";
import {
isInteractionResponse,
logWebhook,
hasPermissionLevel,
redeploy
} from "template/utils/mod.ts";
serve({
"/": main,
"/redeploy": redeploy,
});
async function main(request: Request) {
// Validate the incmoing request; whether or not, it includes
// the specified headers that are sent by Discord.
const { error } = await validateRequest(request, {
POST: {
headers: ["X-Signature-Ed25519", "X-Signature-Timestamp"],
},
});
if (error) {
return json({ error: error.message }, { status: error.status });
}
const publicKey = Deno.env.get("DISCORD_PUBLIC_KEY");
if (!publicKey) {
return json({
error: "Missing Discord public key from environment variables.",
});
}
const signature = request.headers.get("X-Signature-Ed25519")!;
const timestamp = request.headers.get("X-Signature-Timestamp")!;
const { body, isValid } = verifySignature({
publicKey,
signature,
timestamp,
body: await request.text(),
});
if (!isValid) {
return json({ error: "Invalid request; could not verify the request" }, {
status: 401,
});
}
const payload = camelize<Interaction>(JSON.parse(body)) as Interaction;
if (payload.type === InteractionTypes.Ping) {
return json({
type: InteractionResponseTypes.Pong,
});
} else if (payload.type === InteractionTypes.ApplicationCommand) {
if (!payload.data?.name) {
return json({
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
content:
"Something went wrong. I was not able to find the command name in the payload sent by Discord.",
},
});
}
const command = commands[payload.data.name];
if (!command) {
return json({
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
content: "Something went wrong. I was not able to find this command.",
},
});
}
// Make sure the user has the permission to run this command.
if (!(await hasPermissionLevel(command, payload))) {
return json({
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
content: translate(
// discordeno marks guildId as bigint, so need to convert it to string, else translate function throws error
payload.guildId! as unknown as string,
"MISSING_PERM_LEVEL",
),
},
});
}
const result = await command.execute(payload);
if (!isInteractionResponse(result)) {
await logWebhook(payload).catch(console.error);
return json({
data: result,
type: InteractionResponseTypes.ChannelMessageWithSource,
});
}
// DENO/TS BUG DOESNT LET US SEND A OBJECT WITHOUT THIS OVERRIDE
return json(result as unknown as { [key: string]: unknown });
}
return json({ error: "Bad request" }, { status: 400 });
}