blob: f77fe7086884080dab0ec2e0ca6feebd11822247 [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 +01007const jsonParser = bodyParser.json();
8const app = express();
9const port = 10000;
10
11const runServer = (client: HaikuClient) => {
pineafan63fc5e22022-08-04 22:04:10 +010012 app.get("/", (req, res) => {
pineafan6702cef2022-06-13 17:52:37 +010013 res.status(200).send(client.ws.ping);
pineafane625d782022-05-09 18:04:32 +010014 });
15
pineafan63fc5e22022-08-04 22:04:10 +010016 app.post("/verify/:code", jsonParser, async function (req, res) {
pineafane625d782022-05-09 18:04:32 +010017 const code = req.params.code;
18 const secret = req.body.secret;
19 if (secret === client.config.verifySecret) {
pineafan63fc5e22022-08-04 22:04:10 +010020 const guild = await client.guilds.fetch(client.verify[code].gID);
Skyler Grey75ea9172022-08-06 10:22:23 +010021 if (!guild) {
22 return res.status(404);
23 }
pineafan63fc5e22022-08-04 22:04:10 +010024 const member = await guild.members.fetch(client.verify[code].uID);
Skyler Grey75ea9172022-08-06 10:22:23 +010025 if (!member) {
26 return res.status(404);
27 }
28 if (member.roles.cache.has(client.verify[code].rID)) {
29 return res.status(200);
30 }
pineafane625d782022-05-09 18:04:32 +010031 await member.roles.add(client.verify[code].rID);
32
pineafan63fc5e22022-08-04 22:04:10 +010033 const interaction = client.verify[code].interaction;
pineafane625d782022-05-09 18:04:32 +010034 if (interaction) {
Skyler Grey75ea9172022-08-06 10:22:23 +010035 interaction.editReply({
36 embeds: [
37 new EmojiEmbed()
38 .setTitle("Verify")
39 .setDescription("Verification complete")
40 .setStatus("Success")
41 .setEmoji("MEMBER.JOIN")
42 ],
43 components: []
44 });
pineafane625d782022-05-09 18:04:32 +010045 }
pineafanda6e5342022-07-03 10:03:16 +010046 delete client.verify[code];
pineafan63fc5e22022-08-04 22:04:10 +010047 const { log, NucleusColors, entry, renderUser } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010048 try {
pineafan63fc5e22022-08-04 22:04:10 +010049 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010050 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010051 type: "memberVerify",
52 displayName: "Member Verified",
53 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010054 color: NucleusColors.green,
55 emoji: "CONTROL.BLOCKTICK",
56 timestamp: new Date().getTime()
57 },
58 list: {
pineafanda6e5342022-07-03 10:03:16 +010059 memberId: entry(member.id, `\`${member.id}\``),
pineafan412beec2022-06-29 21:46:26 +010060 member: entry(member.id, renderUser(member))
61 },
62 hidden: {
63 guild: guild.id
64 }
pineafan63fc5e22022-08-04 22:04:10 +010065 };
pineafan412beec2022-06-29 21:46:26 +010066 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010067 } catch {
68 res.sendStatus(500);
69 }
pineafan6702cef2022-06-13 17:52:37 +010070 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010071 } else {
pineafan6702cef2022-06-13 17:52:37 +010072 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010073 }
74 });
75
pineafan63fc5e22022-08-04 22:04:10 +010076 app.get("/verify/:code", jsonParser, function (req, res) {
pineafane625d782022-05-09 18:04:32 +010077 const code = req.params.code;
78 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010079 try {
pineafan63fc5e22022-08-04 22:04:10 +010080 const interaction = client.verify[code].interaction;
pineafanda6e5342022-07-03 10:03:16 +010081 if (interaction) {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 interaction.editReply({
83 embeds: [
84 new EmojiEmbed()
85 .setTitle("Verify")
86 .setDescription(
87 "Verify was opened in another tab or window, please complete the CAPTCHA there to continue"
88 )
89 .setStatus("Success")
90 .setEmoji("MEMBER.JOIN")
91 ]
92 });
pineafanda6e5342022-07-03 10:03:16 +010093 }
Skyler Grey75ea9172022-08-06 10:22:23 +010094 } catch {
95 return res.sendStatus(410);
96 }
pineafan63fc5e22022-08-04 22:04:10 +010097 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +010098 delete data.interaction;
99 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +0100100 }
pineafan6702cef2022-06-13 17:52:37 +0100101 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100102 });
pineafane625d782022-05-09 18:04:32 +0100103
pineafan63fc5e22022-08-04 22:04:10 +0100104 app.post("/rolemenu/:code", jsonParser, async function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +0100105 const code = req.params.code;
106 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +0100107 if (secret === client.config.verifySecret) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100108 const guild = await client.guilds.fetch(client.roleMenu[code].guild);
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 if (!guild) {
110 return res.status(404);
111 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100112 const member = await guild.members.fetch(client.roleMenu[code].user);
Skyler Grey75ea9172022-08-06 10:22:23 +0100113 if (!member) {
114 return res.status(404);
115 }
pineafanda6e5342022-07-03 10:03:16 +0100116 res.sendStatus(200);
117 } else {
118 res.sendStatus(403);
119 }
120 });
121
pineafan63fc5e22022-08-04 22:04:10 +0100122 app.get("/rolemenu/:code", jsonParser, function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +0100123 const code = req.params.code;
124 if (client.roleMenu[code] !== undefined) {
125 try {
pineafan63fc5e22022-08-04 22:04:10 +0100126 const interaction = client.roleMenu[code].interaction;
pineafanda6e5342022-07-03 10:03:16 +0100127 if (interaction) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 interaction.editReply({
129 embeds: [
130 new EmojiEmbed()
131 .setTitle("Roles")
132 .setDescription(
133 "The role menu was opened in another tab or window, please select your roles there to continue"
134 )
135 .setStatus("Success")
136 .setEmoji("GUILD.GREEN")
137 ],
138 components: []
139 });
pineafanda6e5342022-07-03 10:03:16 +0100140 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 } catch {
142 return res.sendStatus(410);
143 }
pineafan63fc5e22022-08-04 22:04:10 +0100144 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100145 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100146 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100147 return res.status(200).send(data);
148 }
149 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100150 });
pineafanda6e5342022-07-03 10:03:16 +0100151
pineafane625d782022-05-09 18:04:32 +0100152 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100153};
pineafane625d782022-05-09 18:04:32 +0100154
Skyler Grey75ea9172022-08-06 10:22:23 +0100155export default runServer;