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:
res.json({ type: 1 });
break;
case 2:
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(
"https://api.val.town/v1/eval/"
+ encodeURIComponent(req.body.data.options[0].value),
{
"headers": { "Content-Type": "application/json" },
},
)).text(),
)
}`,
},
}));
break;
case "Undo Help":
res.json({
type: 4,
data: {
content: `\`/undo-record leaderboard:mgsr2 versus:1\``,
flags: 64,
},
});
break;
case "Result Formatter":
console.log(req.body.data.name);
let messagekey = req.body.data.target_id;
let content = req.body.data.resolved.messages[messagekey].content;
let resultString = formatInput(content);
console.log(resultString);
res.json({
type: 4,
data: {
content: `/game record leaderboard: mgsr2 result: ${resultString}`,
flags: 64,
},
});
break;
case "Result Formatter (PC Compatible)":
console.log(req.body.data.name);
let messagekeyPC = req.body.data.target_id;
let contentPC = req.body.data.resolved.messages[messagekeyPC].content;
let resultStringPC = formatInput(contentPC);
console.log(resultStringPC);
res.json({
type: 4,
data: {
content: `\`/game record leaderboard: mgsr2 result: ${resultStringPC}\``,
flags: 64,
},
});
break;
}