blob: 4b849f14f58cee0f768414d392f24927cb834237 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import type { Guild, GuildMember } from "discord.js";
2import type { NucleusClient } from "../utils/client.js";
TheCodedProf51296102023-01-18 22:35:02 -05003//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01004import express from "express";
TheCodedProf51296102023-01-18 22:35:02 -05005//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01006import bodyParser from "body-parser";
pineafan4edb7762022-06-26 19:21:04 +01007import EmojiEmbed from "../utils/generateEmojiEmbed.js";
TheCodedProf51296102023-01-18 22:35:02 -05008//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01009import structuredClone from "@ungap/structured-clone";
pineafan6702cef2022-06-13 17:52:37 +010010
pineafane625d782022-05-09 18:04:32 +010011const jsonParser = bodyParser.json();
12const app = express();
13const port = 10000;
14
TheCodedProf51296102023-01-18 22:35:02 -050015const runServer = (client: NucleusClient) => {
PineaFan638eb132023-01-19 10:41:22 +000016 app.get("/", (_req: express.Request, res: express.Response) => {
TheCodedProf48865eb2023-03-05 15:25:25 -050017 res.status(200).send(client.ws.ping.toString());
pineafane625d782022-05-09 18:04:32 +010018 });
19
PineaFan638eb132023-01-19 10:41:22 +000020 app.post("/verify/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010021 const code = req.params.code;
22 const secret = req.body.secret;
23 if (secret === client.config.verifySecret) {
Skyler Greyda16adf2023-03-05 10:22:12 +000024 const guild = (await client.guilds.fetch(client.verify[code]!.gID)) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +010025 if (!guild) {
26 return res.status(404);
27 }
Skyler Greyda16adf2023-03-05 10:22:12 +000028 const member = (await guild.members.fetch(client.verify[code]!.uID)) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +010029 if (!member) {
30 return res.status(404);
31 }
TheCodedProf51296102023-01-18 22:35:02 -050032 if (member.roles.cache.has(client.verify[code]!.rID)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010033 return res.status(200);
34 }
TheCodedProf51296102023-01-18 22:35:02 -050035 await member.roles.add(client.verify[code]!.rID);
pineafane625d782022-05-09 18:04:32 +010036
TheCodedProf51296102023-01-18 22:35:02 -050037 const interaction = client.verify[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +000038 interaction.editReply({
39 embeds: [
40 new EmojiEmbed()
41 .setTitle("Verify")
PineaFanc4d6c3f2023-01-19 12:17:25 +000042 .setDescription("Verification complete! You can now dismiss this message")
PineaFan638eb132023-01-19 10:41:22 +000043 .setStatus("Success")
44 .setEmoji("MEMBER.JOIN")
45 ],
46 components: []
47 });
PineaFan638eb132023-01-19 10:41:22 +000048 client.verify = Object.keys(client.verify)
49 .filter((k) => k !== code)
Skyler Greyda16adf2023-03-05 10:22:12 +000050 .reduce((obj, key) => {
51 return { ...obj, [key]: client.verify[key] };
52 }, {});
PineaFanc4d6c3f2023-01-19 12:17:25 +000053 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010054 try {
pineafan63fc5e22022-08-04 22:04:10 +010055 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010056 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010057 type: "memberVerify",
58 displayName: "Member Verified",
59 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010060 color: NucleusColors.green,
61 emoji: "CONTROL.BLOCKTICK",
TheCodedProf6ec331b2023-02-20 12:13:06 -050062 timestamp: Date.now()
pineafan412beec2022-06-29 21:46:26 +010063 },
64 list: {
PineaFanc4d6c3f2023-01-19 12:17:25 +000065 member: entry(member.id, renderUser(member.user)),
TheCodedProfea3e0e02023-03-04 17:55:42 -050066 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp!)),
67 verified: entry(Date.now(), renderDelta(Date.now()))
pineafan412beec2022-06-29 21:46:26 +010068 },
69 hidden: {
70 guild: guild.id
71 }
pineafan63fc5e22022-08-04 22:04:10 +010072 };
pineafan412beec2022-06-29 21:46:26 +010073 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010074 } catch {
75 res.sendStatus(500);
76 }
pineafan6702cef2022-06-13 17:52:37 +010077 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010078 } else {
pineafan6702cef2022-06-13 17:52:37 +010079 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010080 }
81 });
82
PineaFan638eb132023-01-19 10:41:22 +000083 app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010084 const code = req.params.code;
85 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010086 try {
TheCodedProf51296102023-01-18 22:35:02 -050087 const interaction = client.verify[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +000088 interaction.editReply({
89 embeds: [
90 new EmojiEmbed()
91 .setTitle("Verify")
92 .setDescription(
PineaFanc4d6c3f2023-01-19 12:17:25 +000093 "Verify was opened in another tab or window, please complete the check there to continue"
PineaFan638eb132023-01-19 10:41:22 +000094 )
95 .setStatus("Success")
96 .setEmoji("MEMBER.JOIN")
97 ]
98 });
Skyler Grey75ea9172022-08-06 10:22:23 +010099 } catch {
100 return res.sendStatus(410);
101 }
pineafan63fc5e22022-08-04 22:04:10 +0100102 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +0100103 delete data.interaction;
104 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +0100105 }
pineafan6702cef2022-06-13 17:52:37 +0100106 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100107 });
pineafane625d782022-05-09 18:04:32 +0100108
PineaFan638eb132023-01-19 10:41:22 +0000109 app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100110 const code = req.params.code;
111 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +0100112 if (secret === client.config.verifySecret) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000113 const guild = (await client.guilds.fetch(client.roleMenu[code]!.guild)) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 if (!guild) {
115 return res.status(404);
116 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000117 const member = (await guild.members.fetch(client.roleMenu[code]!.user)) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 if (!member) {
119 return res.status(404);
120 }
pineafanda6e5342022-07-03 10:03:16 +0100121 res.sendStatus(200);
122 } else {
123 res.sendStatus(403);
124 }
125 });
126
PineaFan638eb132023-01-19 10:41:22 +0000127 app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100128 const code = req.params.code;
129 if (client.roleMenu[code] !== undefined) {
130 try {
TheCodedProf51296102023-01-18 22:35:02 -0500131 const interaction = client.roleMenu[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +0000132 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 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 } catch {
145 return res.sendStatus(410);
146 }
pineafan63fc5e22022-08-04 22:04:10 +0100147 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100148 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100149 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100150 return res.status(200).send(data);
151 }
152 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100153 });
pineafanda6e5342022-07-03 10:03:16 +0100154
PineappleFan8f5cb242023-02-28 16:40:19 +0000155 app.get("/transcript/:code/human", jsonParser, async function (req: express.Request, res: express.Response) {
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500156 const code = req.params.code;
157 if (code === undefined) return res.status(400).send("No code provided");
TheCodedProf75c51be2023-03-03 17:18:18 -0500158 const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500159 if (entry === null) return res.status(404).send("Could not find a transcript by that code");
160 // Convert to a human readable format
161 const data = client.database.transcripts.toHumanReadable(entry);
TheCodedProff8ef7942023-03-03 15:32:32 -0500162 res.attachment(`${code}.txt`);
163 res.type("txt");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500164 return res.status(200).send(data);
165 });
166
PineappleFan8f5cb242023-02-28 16:40:19 +0000167 app.get("/transcript/:code", jsonParser, async function (req: express.Request, res: express.Response) {
168 const code = req.params.code;
169 if (code === undefined) return res.status(400).send("No code provided");
TheCodedProf75c51be2023-03-03 17:18:18 -0500170 const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
PineappleFan8f5cb242023-02-28 16:40:19 +0000171 if (entry === null) return res.status(404).send("Could not find a transcript by that code");
172 // Convert to a human readable format
173 return res.status(200).send(entry);
174 });
175
TheCodedProff8ef7942023-03-03 15:32:32 -0500176 app.get("/channels/:id", jsonParser, async function (req: express.Request, res: express.Response) {
177 const id = req.params.id;
178 if (id === undefined) return res.status(400).send("No id provided");
179 const channel = await client.channels.fetch(id);
TheCodedProfb7a7b992023-03-05 16:11:59 -0500180 if (channel === null) return res.status(400).send("Could not find a channel by that id");
TheCodedProff8ef7942023-03-03 15:32:32 -0500181 if (channel.isDMBased()) return res.status(400).send("Cannot get a DM channel");
182 return res.status(200).send(channel.name);
183 });
184
185 app.get("/users/:id", jsonParser, async function (req: express.Request, res: express.Response) {
186 const id = req.params.id;
187 if (id === undefined) return res.status(400).send("No id provided");
188 let user;
189 try {
190 user = await client.users.fetch(id);
191 } catch (e) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000192 console.log(e);
TheCodedProff8ef7942023-03-03 15:32:32 -0500193 return res.status(404).send("Could not find a user by that id");
194 }
195 return res.status(200).send(user.username);
196 });
197
pineafane625d782022-05-09 18:04:32 +0100198 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100199};
pineafane625d782022-05-09 18:04:32 +0100200
Skyler Grey75ea9172022-08-06 10:22:23 +0100201export default runServer;