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
import { verify_discord_signature } from "https://esm.town/v/mattx/verify_discord_signature?v=8";
import { insertIntoDiscordQueue } from "https://esm.town/v/rayman/insertIntoDiscordQueue1";
import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
import { example1 } from "https://esm.town/v/stevekrouse/example1?v=3";
import process from "node:process";
export let valBotEndpoint = async (
req: express.Request,
res: express.Response,
) => {
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.valbot_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");
console.log(req);
console.log(res);
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
// This switch uses the name of your commands. The code below is based on a registered
// slash command called 'ping'
switch (req.body.data.name) {
case "ping":
res.json({
channel_id: "1142405377896497183",
type: 4,
data: {
channel_id: "1142405377896497183",
content: `Hello World! example1 is: ${example1}`,
},
});
break;
case "poke":
console.log(req.body);
let targetID = req.body.data.target_id;
let poker = `<@${req.body.member.user.id}>`;
let pokee = `<@${req.body.data.resolved.users[targetID].id}>`;
console.log([pokee, poker]);
res.json({
type: 4,
channel_id: "1142405377896497183",
data: {
// Tells the recipient who made this gesture to them.
type: 0,
channel_id: "1142405377896497183",
flags: 2,
content: `${pokee} has been poked by ${poker}`,
},
});
break;
case "queue":
// console.log(req.body);
let queueMember = [req.body.member.user.id, req.body.member.user.username];
let queueList = await sqlite.execute(`SELECT
*
FROM
discord_q;`);
// console.log(queueMember);
console.log(queueList.rows);
insertIntoDiscordQueue(queueMember[1], queueMember[0], 1200);
res.json({
type: 4,
channel_id: "1142405377896497183",
data: {
// type: 0,
channel_id: "1142405377896497183",
flags: 2,
content: "",
// <@&1174180748203405433>
"embeds": [
{
"type": "rich",
"title": `Ranked Ladder Queue`,
"description": `Players looking for games or currently playing:
${
[...queueList.rows].map(e => `
**Player:** <@${e[1]}> **Rating:** ${e[2]}
`).join(" ")
}
`,
"color": 0xafa825,
},
],
"allowed_mentions": {
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!
November 30, 2023