blob: b94bbe01f8c53500d2b35519a4c97b0b09320bd0 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import type { HaikuClient } from "jshaiku";
2import express from "express";
3import bodyParser from "body-parser";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan63fc5e22022-08-04 22:04:10 +01005import structuredClone from "@ungap/structured-clone";
pineafan6702cef2022-06-13 17:52:37 +01006
pineafane625d782022-05-09 18:04:32 +01007
8const jsonParser = bodyParser.json();
9const app = express();
10const port = 10000;
11
12const runServer = (client: HaikuClient) => {
pineafan63fc5e22022-08-04 22:04:10 +010013 app.get("/", (req, res) => {
pineafan6702cef2022-06-13 17:52:37 +010014 res.status(200).send(client.ws.ping);
pineafane625d782022-05-09 18:04:32 +010015 });
16
pineafan63fc5e22022-08-04 22:04:10 +010017 app.post("/verify/:code", jsonParser, async function (req, res) {
pineafane625d782022-05-09 18:04:32 +010018 const code = req.params.code;
19 const secret = req.body.secret;
20 if (secret === client.config.verifySecret) {
pineafan63fc5e22022-08-04 22:04:10 +010021 const guild = await client.guilds.fetch(client.verify[code].gID);
22 if (!guild) { return res.status(404); }
23 const member = await guild.members.fetch(client.verify[code].uID);
24 if (!member) { return res.status(404); }
25 if (member.roles.cache.has(client.verify[code].rID)) { return res.status(200); }
pineafane625d782022-05-09 18:04:32 +010026 await member.roles.add(client.verify[code].rID);
27
pineafan63fc5e22022-08-04 22:04:10 +010028 const interaction = client.verify[code].interaction;
pineafane625d782022-05-09 18:04:32 +010029 if (interaction) {
pineafan4edb7762022-06-26 19:21:04 +010030 interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010031 .setTitle("Verify")
pineafan63fc5e22022-08-04 22:04:10 +010032 .setDescription("Verification complete")
pineafane625d782022-05-09 18:04:32 +010033 .setStatus("Success")
34 .setEmoji("MEMBER.JOIN")
35 ], components: []});
36 }
pineafanda6e5342022-07-03 10:03:16 +010037 delete client.verify[code];
pineafan63fc5e22022-08-04 22:04:10 +010038 const { log, NucleusColors, entry, renderUser } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010039 try {
pineafan63fc5e22022-08-04 22:04:10 +010040 const data = {
pineafan412beec2022-06-29 21:46:26 +010041 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010042 type: "memberVerify",
43 displayName: "Member Verified",
44 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010045 color: NucleusColors.green,
46 emoji: "CONTROL.BLOCKTICK",
47 timestamp: new Date().getTime()
48 },
49 list: {
pineafanda6e5342022-07-03 10:03:16 +010050 memberId: entry(member.id, `\`${member.id}\``),
pineafan412beec2022-06-29 21:46:26 +010051 member: entry(member.id, renderUser(member))
52 },
53 hidden: {
54 guild: guild.id
55 }
pineafan63fc5e22022-08-04 22:04:10 +010056 };
pineafan412beec2022-06-29 21:46:26 +010057 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010058 } catch {
59 res.sendStatus(500);
60 }
pineafan6702cef2022-06-13 17:52:37 +010061 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010062 } else {
pineafan6702cef2022-06-13 17:52:37 +010063 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010064 }
65 });
66
pineafan63fc5e22022-08-04 22:04:10 +010067 app.get("/verify/:code", jsonParser, function (req, res) {
pineafane625d782022-05-09 18:04:32 +010068 const code = req.params.code;
69 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010070 try {
pineafan63fc5e22022-08-04 22:04:10 +010071 const interaction = client.verify[code].interaction;
pineafanda6e5342022-07-03 10:03:16 +010072 if (interaction) {
73 interaction.editReply({embeds: [new EmojiEmbed()
74 .setTitle("Verify")
pineafan63fc5e22022-08-04 22:04:10 +010075 .setDescription("Verify was opened in another tab or window, please complete the CAPTCHA there to continue")
pineafanda6e5342022-07-03 10:03:16 +010076 .setStatus("Success")
77 .setEmoji("MEMBER.JOIN")
78 ]});
79 }
pineafan63fc5e22022-08-04 22:04:10 +010080 } catch { return res.sendStatus(410); }
81 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +010082 delete data.interaction;
83 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +010084 }
pineafan6702cef2022-06-13 17:52:37 +010085 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +010086 });
pineafane625d782022-05-09 18:04:32 +010087
pineafan63fc5e22022-08-04 22:04:10 +010088 app.post("/rolemenu/:code", jsonParser, async function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +010089 const code = req.params.code;
90 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +010091 if (secret === client.config.verifySecret) {
pineafan63fc5e22022-08-04 22:04:10 +010092 const guild = await client.guilds.fetch(client.roleMenu[code].guild);
93 if (!guild) { return res.status(404); }
94 const member = await guild.members.fetch(client.roleMenu[code].user);
95 if (!member) { return res.status(404); }
pineafanda6e5342022-07-03 10:03:16 +010096 res.sendStatus(200);
97 } else {
98 res.sendStatus(403);
99 }
100 });
101
pineafan63fc5e22022-08-04 22:04:10 +0100102 app.get("/rolemenu/:code", jsonParser, function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +0100103 const code = req.params.code;
104 if (client.roleMenu[code] !== undefined) {
105 try {
pineafan63fc5e22022-08-04 22:04:10 +0100106 const interaction = client.roleMenu[code].interaction;
pineafanda6e5342022-07-03 10:03:16 +0100107 if (interaction) {
108 interaction.editReply({embeds: [new EmojiEmbed()
109 .setTitle("Roles")
pineafan63fc5e22022-08-04 22:04:10 +0100110 .setDescription("The role menu was opened in another tab or window, please select your roles there to continue")
pineafanda6e5342022-07-03 10:03:16 +0100111 .setStatus("Success")
112 .setEmoji("GUILD.GREEN")
113 ], components: []});
114 }
pineafan63fc5e22022-08-04 22:04:10 +0100115 } catch { return res.sendStatus(410); }
116 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100117 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100118 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100119 return res.status(200).send(data);
120 }
121 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100122 });
pineafanda6e5342022-07-03 10:03:16 +0100123
pineafane625d782022-05-09 18:04:32 +0100124 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100125};
pineafane625d782022-05-09 18:04:32 +0100126
127export default runServer;