Fork
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
import { verify_discord_signature } from "https://esm.town/v/mattx/verify_discord_signature";
import { formatInput } from "https://esm.town/v/rayman/formatInput";
import { fetch } from "https://esm.town/v/std/fetch";
import { example1 } from "https://esm.town/v/stevekrouse/example1?v=3";
import process from "node:process";
export let mgsrBotEndpoint = async (
req: express.Request,
res: express.Response,
) => {
console.log(req.get("X-Signature-Timestamp"));
console.log(req.get("X-Signature-Ed25519"));
if (!req.get("X-Signature-Timestamp") || !req.get("X-Signature-Ed25519")) {
res.status(400);
res.end("Signature headers missing!");
}
const verified = await verify_discord_signature(
process.env.discord_pubkey,
JSON.stringify(req.body),
req.get("X-Signature-Ed25519"),
req.get("X-Signature-Timestamp"),
);
if (!verified) {
res.status(401);
res.end("signature invalid");
return;
}
res.set("Content-type", "application/json");
switch (req.body.type) {
case 1: // PING interaction
res.json({ type: 1 }); // PONG
break;
case 2: // APPLICATION_COMMAND interactions
// TODO: Probably don't use a nested switch here
switch (req.body.data.name) {
case "ping":
res.json({
type: 4,
data: {
content: `Hello World! example1 is: ${example1}`,
},
});
break;
case "eval":
res.end(JSON.stringify({
type: 4,
data: {
content: `${
JSON.stringify(
await (await fetch(
Only the latest version can be previewed
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.