blob: 17505092c764f8648161cf2cf63a03d82ac55510 [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";
6import client from "../utils/client.js";
pineafan6702cef2022-06-13 17:52:37 +01007
pineafane625d782022-05-09 18:04:32 +01008
9const jsonParser = bodyParser.json();
10const app = express();
11const port = 10000;
12
13const runServer = (client: HaikuClient) => {
pineafan63fc5e22022-08-04 22:04:10 +010014 app.get("/", (req, res) => {
pineafan6702cef2022-06-13 17:52:37 +010015 res.status(200).send(client.ws.ping);
pineafane625d782022-05-09 18:04:32 +010016 });
17
pineafan63fc5e22022-08-04 22:04:10 +010018 app.post("/verify/:code", jsonParser, async function (req, res) {
pineafane625d782022-05-09 18:04:32 +010019 const code = req.params.code;
20 const secret = req.body.secret;
21 if (secret === client.config.verifySecret) {
pineafan63fc5e22022-08-04 22:04:10 +010022 const guild = await client.guilds.fetch(client.verify[code].gID);
23 if (!guild) { return res.status(404); }
24 const member = await guild.members.fetch(client.verify[code].uID);
25 if (!member) { return res.status(404); }
26 if (member.roles.cache.has(client.verify[code].rID)) { return res.status(200); }
pineafane625d782022-05-09 18:04:32 +010027 await member.roles.add(client.verify[code].rID);
28
pineafan63fc5e22022-08-04 22:04:10 +010029 const interaction = client.verify[code].interaction;
pineafane625d782022-05-09 18:04:32 +010030 if (interaction) {
pineafan4edb7762022-06-26 19:21:04 +010031 interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010032 .setTitle("Verify")
pineafan63fc5e22022-08-04 22:04:10 +010033 .setDescription("Verification complete")
pineafane625d782022-05-09 18:04:32 +010034 .setStatus("Success")
35 .setEmoji("MEMBER.JOIN")
36 ], components: []});
37 }
pineafanda6e5342022-07-03 10:03:16 +010038 delete client.verify[code];
pineafan63fc5e22022-08-04 22:04:10 +010039 const { log, NucleusColors, entry, renderUser } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010040 try {
pineafan63fc5e22022-08-04 22:04:10 +010041 const data = {
pineafan412beec2022-06-29 21:46:26 +010042 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010043 type: "memberVerify",
44 displayName: "Member Verified",
45 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010046 color: NucleusColors.green,
47 emoji: "CONTROL.BLOCKTICK",
48 timestamp: new Date().getTime()
49 },
50 list: {
pineafanda6e5342022-07-03 10:03:16 +010051 memberId: entry(member.id, `\`${member.id}\``),
pineafan412beec2022-06-29 21:46:26 +010052 member: entry(member.id, renderUser(member))
53 },
54 hidden: {
55 guild: guild.id
56 }
pineafan63fc5e22022-08-04 22:04:10 +010057 };
pineafan412beec2022-06-29 21:46:26 +010058 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010059 } catch {
60 res.sendStatus(500);
61 }
pineafan6702cef2022-06-13 17:52:37 +010062 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010063 } else {
pineafan6702cef2022-06-13 17:52:37 +010064 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010065 }
66 });
67
pineafan63fc5e22022-08-04 22:04:10 +010068 app.get("/verify/:code", jsonParser, function (req, res) {
pineafane625d782022-05-09 18:04:32 +010069 const code = req.params.code;
70 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010071 try {
pineafan63fc5e22022-08-04 22:04:10 +010072 const interaction = client.verify[code].interaction;
pineafanda6e5342022-07-03 10:03:16 +010073 if (interaction) {
74 interaction.editReply({embeds: [new EmojiEmbed()
75 .setTitle("Verify")
pineafan63fc5e22022-08-04 22:04:10 +010076 .setDescription("Verify was opened in another tab or window, please complete the CAPTCHA there to continue")
pineafanda6e5342022-07-03 10:03:16 +010077 .setStatus("Success")
78 .setEmoji("MEMBER.JOIN")
79 ]});
80 }
pineafan63fc5e22022-08-04 22:04:10 +010081 } catch { return res.sendStatus(410); }
82 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +010083 delete data.interaction;
84 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +010085 }
pineafan6702cef2022-06-13 17:52:37 +010086 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +010087 });
pineafane625d782022-05-09 18:04:32 +010088
pineafan63fc5e22022-08-04 22:04:10 +010089 app.post("/rolemenu/:code", jsonParser, async function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +010090 const code = req.params.code;
91 const secret = req.body.secret;
92 const data = req.body.data;
93 if (secret === client.config.verifySecret) {
pineafan63fc5e22022-08-04 22:04:10 +010094 const guild = await client.guilds.fetch(client.roleMenu[code].guild);
95 if (!guild) { return res.status(404); }
96 const member = await guild.members.fetch(client.roleMenu[code].user);
97 if (!member) { return res.status(404); }
pineafanda6e5342022-07-03 10:03:16 +010098 res.sendStatus(200);
99 } else {
100 res.sendStatus(403);
101 }
102 });
103
pineafan63fc5e22022-08-04 22:04:10 +0100104 app.get("/rolemenu/:code", jsonParser, function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +0100105 const code = req.params.code;
106 if (client.roleMenu[code] !== undefined) {
107 try {
pineafan63fc5e22022-08-04 22:04:10 +0100108 const interaction = client.roleMenu[code].interaction;
pineafanda6e5342022-07-03 10:03:16 +0100109 if (interaction) {
110 interaction.editReply({embeds: [new EmojiEmbed()
111 .setTitle("Roles")
pineafan63fc5e22022-08-04 22:04:10 +0100112 .setDescription("The role menu was opened in another tab or window, please select your roles there to continue")
pineafanda6e5342022-07-03 10:03:16 +0100113 .setStatus("Success")
114 .setEmoji("GUILD.GREEN")
115 ], components: []});
116 }
pineafan63fc5e22022-08-04 22:04:10 +0100117 } catch { return res.sendStatus(410); }
118 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100119 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100120 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100121 return res.status(200).send(data);
122 }
123 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100124 });
pineafanda6e5342022-07-03 10:03:16 +0100125
pineafane625d782022-05-09 18:04:32 +0100126 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100127};
pineafane625d782022-05-09 18:04:32 +0100128
129export default runServer;