blob: c8b7b14d07000bc53d888620ab33ee4aa349b6d8 [file] [log] [blame]
PineaFan638eb132023-01-19 10:41:22 +00001import type { Guild, GuildMember } from 'discord.js';
TheCodedProf51296102023-01-18 22:35:02 -05002import type { NucleusClient } from '../utils/client.js';
3//@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) => {
pineafan6702cef2022-06-13 17:52:37 +010017 res.status(200).send(client.ws.ping);
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) {
PineaFan638eb132023-01-19 10:41:22 +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 }
PineaFan638eb132023-01-19 10:41:22 +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)
50 .reduce((obj, key) => {return { ...obj, [key]: client.verify[key]}}, {});
PineaFanc4d6c3f2023-01-19 12:17:25 +000051 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010052 try {
pineafan63fc5e22022-08-04 22:04:10 +010053 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010054 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010055 type: "memberVerify",
56 displayName: "Member Verified",
57 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010058 color: NucleusColors.green,
59 emoji: "CONTROL.BLOCKTICK",
TheCodedProf6ec331b2023-02-20 12:13:06 -050060 timestamp: Date.now()
pineafan412beec2022-06-29 21:46:26 +010061 },
62 list: {
PineaFanc4d6c3f2023-01-19 12:17:25 +000063 member: entry(member.id, renderUser(member.user)),
64 verified: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp!))
pineafan412beec2022-06-29 21:46:26 +010065 },
66 hidden: {
67 guild: guild.id
68 }
pineafan63fc5e22022-08-04 22:04:10 +010069 };
pineafan412beec2022-06-29 21:46:26 +010070 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010071 } catch {
72 res.sendStatus(500);
73 }
pineafan6702cef2022-06-13 17:52:37 +010074 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010075 } else {
pineafan6702cef2022-06-13 17:52:37 +010076 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010077 }
78 });
79
PineaFan638eb132023-01-19 10:41:22 +000080 app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010081 const code = req.params.code;
82 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010083 try {
TheCodedProf51296102023-01-18 22:35:02 -050084 const interaction = client.verify[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +000085 interaction.editReply({
86 embeds: [
87 new EmojiEmbed()
88 .setTitle("Verify")
89 .setDescription(
PineaFanc4d6c3f2023-01-19 12:17:25 +000090 "Verify was opened in another tab or window, please complete the check there to continue"
PineaFan638eb132023-01-19 10:41:22 +000091 )
92 .setStatus("Success")
93 .setEmoji("MEMBER.JOIN")
94 ]
95 });
Skyler Grey75ea9172022-08-06 10:22:23 +010096 } catch {
97 return res.sendStatus(410);
98 }
pineafan63fc5e22022-08-04 22:04:10 +010099 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +0100100 delete data.interaction;
101 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +0100102 }
pineafan6702cef2022-06-13 17:52:37 +0100103 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100104 });
pineafane625d782022-05-09 18:04:32 +0100105
PineaFan638eb132023-01-19 10:41:22 +0000106 app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100107 const code = req.params.code;
108 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +0100109 if (secret === client.config.verifySecret) {
PineaFan638eb132023-01-19 10:41:22 +0000110 const guild = await client.guilds.fetch(client.roleMenu[code]!.guild) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 if (!guild) {
112 return res.status(404);
113 }
PineaFan638eb132023-01-19 10:41:22 +0000114 const member = await guild.members.fetch(client.roleMenu[code]!.user) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 if (!member) {
116 return res.status(404);
117 }
pineafanda6e5342022-07-03 10:03:16 +0100118 res.sendStatus(200);
119 } else {
120 res.sendStatus(403);
121 }
122 });
123
PineaFan638eb132023-01-19 10:41:22 +0000124 app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100125 const code = req.params.code;
126 if (client.roleMenu[code] !== undefined) {
127 try {
TheCodedProf51296102023-01-18 22:35:02 -0500128 const interaction = client.roleMenu[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +0000129 interaction.editReply({
130 embeds: [
131 new EmojiEmbed()
132 .setTitle("Roles")
133 .setDescription(
134 "The role menu was opened in another tab or window, please select your roles there to continue"
135 )
136 .setStatus("Success")
137 .setEmoji("GUILD.GREEN")
138 ],
139 components: []
140 });
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
PineappleFan8f5cb242023-02-28 16:40:19 +0000152 app.get("/transcript/:code/human", jsonParser, async function (req: express.Request, res: express.Response) {
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500153 const code = req.params.code;
154 if (code === undefined) return res.status(400).send("No code provided");
TheCodedProf75c51be2023-03-03 17:18:18 -0500155 const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500156 if (entry === null) return res.status(404).send("Could not find a transcript by that code");
157 // Convert to a human readable format
158 const data = client.database.transcripts.toHumanReadable(entry);
TheCodedProff8ef7942023-03-03 15:32:32 -0500159 res.attachment(`${code}.txt`);
160 res.type("txt");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500161 return res.status(200).send(data);
162 });
163
PineappleFan8f5cb242023-02-28 16:40:19 +0000164 app.get("/transcript/:code", jsonParser, async function (req: express.Request, res: express.Response) {
165 const code = req.params.code;
166 if (code === undefined) return res.status(400).send("No code provided");
TheCodedProf75c51be2023-03-03 17:18:18 -0500167 const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
PineappleFan8f5cb242023-02-28 16:40:19 +0000168 if (entry === null) return res.status(404).send("Could not find a transcript by that code");
169 // Convert to a human readable format
170 return res.status(200).send(entry);
171 });
172
TheCodedProff8ef7942023-03-03 15:32:32 -0500173 app.get("/channels/:id", jsonParser, async function (req: express.Request, res: express.Response) {
174 const id = req.params.id;
175 if (id === undefined) return res.status(400).send("No id provided");
176 const channel = await client.channels.fetch(id);
177 if (channel === null) return res.status(404).send("Could not find a channel by that id");
178 if (channel.isDMBased()) return res.status(400).send("Cannot get a DM channel");
179 return res.status(200).send(channel.name);
180 });
181
182 app.get("/users/:id", jsonParser, async function (req: express.Request, res: express.Response) {
183 const id = req.params.id;
184 if (id === undefined) return res.status(400).send("No id provided");
185 let user;
186 try {
187 user = await client.users.fetch(id);
188 } catch (e) {
189 console.log(e)
190 return res.status(404).send("Could not find a user by that id");
191 }
192 return res.status(200).send(user.username);
193 });
194
pineafane625d782022-05-09 18:04:32 +0100195 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100196};
pineafane625d782022-05-09 18:04:32 +0100197
Skyler Grey75ea9172022-08-06 10:22:23 +0100198export default runServer;