blob: 2c4babaacc3acddcc7af93878e6545e9d2f460da [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 Grey75ea9172022-08-06 10:22:23 +0100108 const guild = await client.guilds.fetch(
109 client.roleMenu[code].guild
110 );
111 if (!guild) {
112 return res.status(404);
113 }
114 const member = await guild.members.fetch(
115 client.roleMenu[code].user
116 );
117 if (!member) {
118 return res.status(404);
119 }
pineafanda6e5342022-07-03 10:03:16 +0100120 res.sendStatus(200);
121 } else {
122 res.sendStatus(403);
123 }
124 });
125
pineafan63fc5e22022-08-04 22:04:10 +0100126 app.get("/rolemenu/:code", jsonParser, function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +0100127 const code = req.params.code;
128 if (client.roleMenu[code] !== undefined) {
129 try {
pineafan63fc5e22022-08-04 22:04:10 +0100130 const interaction = client.roleMenu[code].interaction;
pineafanda6e5342022-07-03 10:03:16 +0100131 if (interaction) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 interaction.editReply({
133 embeds: [
134 new EmojiEmbed()
135 .setTitle("Roles")
136 .setDescription(
137 "The role menu was opened in another tab or window, please select your roles there to continue"
138 )
139 .setStatus("Success")
140 .setEmoji("GUILD.GREEN")
141 ],
142 components: []
143 });
pineafanda6e5342022-07-03 10:03:16 +0100144 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 } catch {
146 return res.sendStatus(410);
147 }
pineafan63fc5e22022-08-04 22:04:10 +0100148 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100149 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100150 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100151 return res.status(200).send(data);
152 }
153 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100154 });
pineafanda6e5342022-07-03 10:03:16 +0100155
pineafane625d782022-05-09 18:04:32 +0100156 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100157};
pineafane625d782022-05-09 18:04:32 +0100158
Skyler Grey75ea9172022-08-06 10:22:23 +0100159export default runServer;