blob: 79b115e13585a389aa84bdb0db6f0af2286ae580 [file] [log] [blame]
Skyler Grey0d885222023-03-08 21:46:37 +00001#!/bin
Skyler Greyda16adf2023-03-05 10:22:12 +00002import type { Guild, GuildMember } from "discord.js";
3import type { NucleusClient } from "../utils/client.js";
TheCodedProf51296102023-01-18 22:35:02 -05004//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01005import express from "express";
TheCodedProf51296102023-01-18 22:35:02 -05006//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01007import bodyParser from "body-parser";
pineafan4edb7762022-06-26 19:21:04 +01008import EmojiEmbed from "../utils/generateEmojiEmbed.js";
TheCodedProf51296102023-01-18 22:35:02 -05009//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +010010import structuredClone from "@ungap/structured-clone";
pineafan6702cef2022-06-13 17:52:37 +010011
pineafane625d782022-05-09 18:04:32 +010012const jsonParser = bodyParser.json();
13const app = express();
14const port = 10000;
15
TheCodedProf51296102023-01-18 22:35:02 -050016const runServer = (client: NucleusClient) => {
PineaFan638eb132023-01-19 10:41:22 +000017 app.get("/", (_req: express.Request, res: express.Response) => {
TheCodedProf48865eb2023-03-05 15:25:25 -050018 res.status(200).send(client.ws.ping.toString());
pineafane625d782022-05-09 18:04:32 +010019 });
20
PineaFan638eb132023-01-19 10:41:22 +000021 app.post("/verify/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010022 const code = req.params.code;
23 const secret = req.body.secret;
24 if (secret === client.config.verifySecret) {
Skyler Greyda16adf2023-03-05 10:22:12 +000025 const guild = (await client.guilds.fetch(client.verify[code]!.gID)) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +010026 if (!guild) {
27 return res.status(404);
28 }
Skyler Greyda16adf2023-03-05 10:22:12 +000029 const member = (await guild.members.fetch(client.verify[code]!.uID)) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +010030 if (!member) {
31 return res.status(404);
32 }
TheCodedProf51296102023-01-18 22:35:02 -050033 if (member.roles.cache.has(client.verify[code]!.rID)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010034 return res.status(200);
35 }
TheCodedProf51296102023-01-18 22:35:02 -050036 await member.roles.add(client.verify[code]!.rID);
pineafane625d782022-05-09 18:04:32 +010037
TheCodedProf51296102023-01-18 22:35:02 -050038 const interaction = client.verify[code]!.interaction;
Skyler Greyf4f21c42023-03-08 14:36:29 +000039 await interaction.editReply({
PineaFan638eb132023-01-19 10:41:22 +000040 embeds: [
41 new EmojiEmbed()
42 .setTitle("Verify")
PineaFanc4d6c3f2023-01-19 12:17:25 +000043 .setDescription("Verification complete! You can now dismiss this message")
PineaFan638eb132023-01-19 10:41:22 +000044 .setStatus("Success")
45 .setEmoji("MEMBER.JOIN")
46 ],
47 components: []
48 });
PineaFan638eb132023-01-19 10:41:22 +000049 client.verify = Object.keys(client.verify)
50 .filter((k) => k !== code)
Skyler Greyda16adf2023-03-05 10:22:12 +000051 .reduce((obj, key) => {
52 return { ...obj, [key]: client.verify[key] };
53 }, {});
PineaFanc4d6c3f2023-01-19 12:17:25 +000054 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010055 try {
pineafan63fc5e22022-08-04 22:04:10 +010056 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010057 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010058 type: "memberVerify",
59 displayName: "Member Verified",
60 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010061 color: NucleusColors.green,
62 emoji: "CONTROL.BLOCKTICK",
TheCodedProf6ec331b2023-02-20 12:13:06 -050063 timestamp: Date.now()
pineafan412beec2022-06-29 21:46:26 +010064 },
65 list: {
PineaFanc4d6c3f2023-01-19 12:17:25 +000066 member: entry(member.id, renderUser(member.user)),
TheCodedProfea3e0e02023-03-04 17:55:42 -050067 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp!)),
68 verified: entry(Date.now(), renderDelta(Date.now()))
pineafan412beec2022-06-29 21:46:26 +010069 },
70 hidden: {
71 guild: guild.id
72 }
pineafan63fc5e22022-08-04 22:04:10 +010073 };
Skyler Greyf4f21c42023-03-08 14:36:29 +000074 await log(data);
pineafan63fc5e22022-08-04 22:04:10 +010075 } catch {
76 res.sendStatus(500);
77 }
pineafan6702cef2022-06-13 17:52:37 +010078 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010079 } else {
pineafan6702cef2022-06-13 17:52:37 +010080 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010081 }
82 });
83
Skyler Greyf4f21c42023-03-08 14:36:29 +000084 app.get("/verify/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010085 const code = req.params.code;
86 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010087 try {
TheCodedProf51296102023-01-18 22:35:02 -050088 const interaction = client.verify[code]!.interaction;
Skyler Greyf4f21c42023-03-08 14:36:29 +000089 await interaction.editReply({
PineaFan638eb132023-01-19 10:41:22 +000090 embeds: [
91 new EmojiEmbed()
92 .setTitle("Verify")
93 .setDescription(
PineaFanc4d6c3f2023-01-19 12:17:25 +000094 "Verify was opened in another tab or window, please complete the check there to continue"
PineaFan638eb132023-01-19 10:41:22 +000095 )
96 .setStatus("Success")
97 .setEmoji("MEMBER.JOIN")
98 ]
99 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 } catch {
101 return res.sendStatus(410);
102 }
pineafan63fc5e22022-08-04 22:04:10 +0100103 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +0100104 delete data.interaction;
105 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +0100106 }
pineafan6702cef2022-06-13 17:52:37 +0100107 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100108 });
pineafane625d782022-05-09 18:04:32 +0100109
PineaFan638eb132023-01-19 10:41:22 +0000110 app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100111 const code = req.params.code;
112 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +0100113 if (secret === client.config.verifySecret) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000114 const guild = (await client.guilds.fetch(client.roleMenu[code]!.guild)) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 if (!guild) {
116 return res.status(404);
117 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000118 const member = (await guild.members.fetch(client.roleMenu[code]!.user)) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100119 if (!member) {
120 return res.status(404);
121 }
pineafanda6e5342022-07-03 10:03:16 +0100122 res.sendStatus(200);
123 } else {
124 res.sendStatus(403);
125 }
126 });
127
Skyler Greyf4f21c42023-03-08 14:36:29 +0000128 app.get("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100129 const code = req.params.code;
130 if (client.roleMenu[code] !== undefined) {
131 try {
TheCodedProf51296102023-01-18 22:35:02 -0500132 const interaction = client.roleMenu[code]!.interaction;
Skyler Greyf4f21c42023-03-08 14:36:29 +0000133 await interaction.editReply({
PineaFan638eb132023-01-19 10:41:22 +0000134 embeds: [
135 new EmojiEmbed()
136 .setTitle("Roles")
137 .setDescription(
138 "The role menu was opened in another tab or window, please select your roles there to continue"
139 )
140 .setStatus("Success")
141 .setEmoji("GUILD.GREEN")
142 ],
143 components: []
144 });
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
PineappleFan8f5cb242023-02-28 16:40:19 +0000156 app.get("/transcript/:code/human", jsonParser, async function (req: express.Request, res: express.Response) {
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500157 const code = req.params.code;
158 if (code === undefined) return res.status(400).send("No code provided");
TheCodedProf75c51be2023-03-03 17:18:18 -0500159 const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500160 if (entry === null) return res.status(404).send("Could not find a transcript by that code");
161 // Convert to a human readable format
162 const data = client.database.transcripts.toHumanReadable(entry);
TheCodedProff8ef7942023-03-03 15:32:32 -0500163 res.attachment(`${code}.txt`);
164 res.type("txt");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500165 return res.status(200).send(data);
166 });
167
PineappleFan8f5cb242023-02-28 16:40:19 +0000168 app.get("/transcript/:code", jsonParser, async function (req: express.Request, res: express.Response) {
169 const code = req.params.code;
170 if (code === undefined) return res.status(400).send("No code provided");
TheCodedProf75c51be2023-03-03 17:18:18 -0500171 const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string);
PineappleFan8f5cb242023-02-28 16:40:19 +0000172 if (entry === null) return res.status(404).send("Could not find a transcript by that code");
173 // Convert to a human readable format
174 return res.status(200).send(entry);
175 });
176
TheCodedProff8ef7942023-03-03 15:32:32 -0500177 app.get("/channels/:id", jsonParser, async function (req: express.Request, res: express.Response) {
178 const id = req.params.id;
179 if (id === undefined) return res.status(400).send("No id provided");
180 const channel = await client.channels.fetch(id);
TheCodedProfb7a7b992023-03-05 16:11:59 -0500181 if (channel === null) return res.status(400).send("Could not find a channel by that id");
TheCodedProff8ef7942023-03-03 15:32:32 -0500182 if (channel.isDMBased()) return res.status(400).send("Cannot get a DM channel");
183 return res.status(200).send(channel.name);
184 });
185
186 app.get("/users/:id", jsonParser, async function (req: express.Request, res: express.Response) {
187 const id = req.params.id;
188 if (id === undefined) return res.status(400).send("No id provided");
189 let user;
190 try {
191 user = await client.users.fetch(id);
192 } catch (e) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000193 console.log(e);
TheCodedProff8ef7942023-03-03 15:32:32 -0500194 return res.status(404).send("Could not find a user by that id");
195 }
196 return res.status(200).send(user.username);
197 });
198
pineafane625d782022-05-09 18:04:32 +0100199 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100200};
pineafane625d782022-05-09 18:04:32 +0100201
Skyler Grey75ea9172022-08-06 10:22:23 +0100202export default runServer;