blob: 8d57f3d4c847e9143ec1084093bfbdbc437f47a2 [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)),
TheCodedProfea3e0e02023-03-04 17:55:42 -050064 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp!)),
65 verified: entry(Date.now(), renderDelta(Date.now()))
pineafan412beec2022-06-29 21:46:26 +010066 },
67 hidden: {
68 guild: guild.id
69 }
pineafan63fc5e22022-08-04 22:04:10 +010070 };
pineafan412beec2022-06-29 21:46:26 +010071 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010072 } catch {
73 res.sendStatus(500);
74 }
pineafan6702cef2022-06-13 17:52:37 +010075 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010076 } else {
pineafan6702cef2022-06-13 17:52:37 +010077 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010078 }
79 });
80
PineaFan638eb132023-01-19 10:41:22 +000081 app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010082 const code = req.params.code;
83 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010084 try {
TheCodedProf51296102023-01-18 22:35:02 -050085 const interaction = client.verify[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +000086 interaction.editReply({
87 embeds: [
88 new EmojiEmbed()
89 .setTitle("Verify")
90 .setDescription(
PineaFanc4d6c3f2023-01-19 12:17:25 +000091 "Verify was opened in another tab or window, please complete the check there to continue"
PineaFan638eb132023-01-19 10:41:22 +000092 )
93 .setStatus("Success")
94 .setEmoji("MEMBER.JOIN")
95 ]
96 });
Skyler Grey75ea9172022-08-06 10:22:23 +010097 } catch {
98 return res.sendStatus(410);
99 }
pineafan63fc5e22022-08-04 22:04:10 +0100100 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +0100101 delete data.interaction;
102 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +0100103 }
pineafan6702cef2022-06-13 17:52:37 +0100104 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100105 });
pineafane625d782022-05-09 18:04:32 +0100106
PineaFan638eb132023-01-19 10:41:22 +0000107 app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100108 const code = req.params.code;
109 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +0100110 if (secret === client.config.verifySecret) {
PineaFan638eb132023-01-19 10:41:22 +0000111 const guild = await client.guilds.fetch(client.roleMenu[code]!.guild) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 if (!guild) {
113 return res.status(404);
114 }
PineaFan638eb132023-01-19 10:41:22 +0000115 const member = await guild.members.fetch(client.roleMenu[code]!.user) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 if (!member) {
117 return res.status(404);
118 }
pineafanda6e5342022-07-03 10:03:16 +0100119 res.sendStatus(200);
120 } else {
121 res.sendStatus(403);
122 }
123 });
124
PineaFan638eb132023-01-19 10:41:22 +0000125 app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100126 const code = req.params.code;
127 if (client.roleMenu[code] !== undefined) {
128 try {
TheCodedProf51296102023-01-18 22:35:02 -0500129 const interaction = client.roleMenu[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +0000130 interaction.editReply({
131 embeds: [
132 new EmojiEmbed()
133 .setTitle("Roles")
134 .setDescription(
135 "The role menu was opened in another tab or window, please select your roles there to continue"
136 )
137 .setStatus("Success")
138 .setEmoji("GUILD.GREEN")
139 ],
140 components: []
141 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 } catch {
143 return res.sendStatus(410);
144 }
pineafan63fc5e22022-08-04 22:04:10 +0100145 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100146 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100147 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100148 return res.status(200).send(data);
149 }
150 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100151 });
pineafanda6e5342022-07-03 10:03:16 +0100152
PineappleFan8f5cb242023-02-28 16:40:19 +0000153 app.get("/transcript/:code/human", jsonParser, async function (req: express.Request, res: express.Response) {
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500154 const code = req.params.code;
155 if (code === undefined) return res.status(400).send("No code provided");
TheCodedProf75c51be2023-03-03 17:18:18 -0500156 const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500157 if (entry === null) return res.status(404).send("Could not find a transcript by that code");
158 // Convert to a human readable format
159 const data = client.database.transcripts.toHumanReadable(entry);
TheCodedProff8ef7942023-03-03 15:32:32 -0500160 res.attachment(`${code}.txt`);
161 res.type("txt");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500162 return res.status(200).send(data);
163 });
164
PineappleFan8f5cb242023-02-28 16:40:19 +0000165 app.get("/transcript/:code", jsonParser, async function (req: express.Request, res: express.Response) {
166 const code = req.params.code;
167 if (code === undefined) return res.status(400).send("No code provided");
TheCodedProf75c51be2023-03-03 17:18:18 -0500168 const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
PineappleFan8f5cb242023-02-28 16:40:19 +0000169 if (entry === null) return res.status(404).send("Could not find a transcript by that code");
170 // Convert to a human readable format
171 return res.status(200).send(entry);
172 });
173
TheCodedProff8ef7942023-03-03 15:32:32 -0500174 app.get("/channels/:id", jsonParser, async function (req: express.Request, res: express.Response) {
175 const id = req.params.id;
176 if (id === undefined) return res.status(400).send("No id provided");
177 const channel = await client.channels.fetch(id);
178 if (channel === null) return res.status(404).send("Could not find a channel by that id");
179 if (channel.isDMBased()) return res.status(400).send("Cannot get a DM channel");
180 return res.status(200).send(channel.name);
181 });
182
183 app.get("/users/:id", jsonParser, async function (req: express.Request, res: express.Response) {
184 const id = req.params.id;
185 if (id === undefined) return res.status(400).send("No id provided");
186 let user;
187 try {
188 user = await client.users.fetch(id);
189 } catch (e) {
190 console.log(e)
191 return res.status(404).send("Could not find a user by that id");
192 }
193 return res.status(200).send(user.username);
194 });
195
pineafane625d782022-05-09 18:04:32 +0100196 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100197};
pineafane625d782022-05-09 18:04:32 +0100198
Skyler Grey75ea9172022-08-06 10:22:23 +0100199export default runServer;