Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import type { Guild, GuildMember } from "discord.js"; |
| 2 | import type { NucleusClient } from "../utils/client.js"; |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 3 | //@ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | import express from "express"; |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 5 | //@ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 6 | import bodyParser from "body-parser"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 7 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 8 | //@ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 9 | import structuredClone from "@ungap/structured-clone"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 10 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 11 | const jsonParser = bodyParser.json(); |
| 12 | const app = express(); |
| 13 | const port = 10000; |
| 14 | |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 15 | const runServer = (client: NucleusClient) => { |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 16 | app.get("/", (_req: express.Request, res: express.Response) => { |
TheCodedProf | 48865eb | 2023-03-05 15:25:25 -0500 | [diff] [blame] | 17 | res.status(200).send(client.ws.ping.toString()); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 18 | }); |
| 19 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 20 | app.post("/verify/:code", jsonParser, async function (req: express.Request, res: express.Response) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 21 | const code = req.params.code; |
| 22 | const secret = req.body.secret; |
| 23 | if (secret === client.config.verifySecret) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 24 | const guild = (await client.guilds.fetch(client.verify[code]!.gID)) as Guild | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 25 | if (!guild) { |
| 26 | return res.status(404); |
| 27 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 28 | const member = (await guild.members.fetch(client.verify[code]!.uID)) as GuildMember | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 29 | if (!member) { |
| 30 | return res.status(404); |
| 31 | } |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 32 | if (member.roles.cache.has(client.verify[code]!.rID)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 33 | return res.status(200); |
| 34 | } |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 35 | await member.roles.add(client.verify[code]!.rID); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 36 | |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 37 | const interaction = client.verify[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 38 | interaction.editReply({ |
| 39 | embeds: [ |
| 40 | new EmojiEmbed() |
| 41 | .setTitle("Verify") |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 42 | .setDescription("Verification complete! You can now dismiss this message") |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 43 | .setStatus("Success") |
| 44 | .setEmoji("MEMBER.JOIN") |
| 45 | ], |
| 46 | components: [] |
| 47 | }); |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 48 | client.verify = Object.keys(client.verify) |
| 49 | .filter((k) => k !== code) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 50 | .reduce((obj, key) => { |
| 51 | return { ...obj, [key]: client.verify[key] }; |
| 52 | }, {}); |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 53 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 54 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | type: "memberVerify", |
| 58 | displayName: "Member Verified", |
| 59 | calculateType: "guildMemberVerify", |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 60 | color: NucleusColors.green, |
| 61 | emoji: "CONTROL.BLOCKTICK", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 62 | timestamp: Date.now() |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 63 | }, |
| 64 | list: { |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 65 | member: entry(member.id, renderUser(member.user)), |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame] | 66 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp!)), |
| 67 | verified: entry(Date.now(), renderDelta(Date.now())) |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 68 | }, |
| 69 | hidden: { |
| 70 | guild: guild.id |
| 71 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | }; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 73 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | } catch { |
| 75 | res.sendStatus(500); |
| 76 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 77 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 78 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 79 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 80 | } |
| 81 | }); |
| 82 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 83 | app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 84 | const code = req.params.code; |
| 85 | if (client.verify[code]) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 86 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 87 | const interaction = client.verify[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 88 | interaction.editReply({ |
| 89 | embeds: [ |
| 90 | new EmojiEmbed() |
| 91 | .setTitle("Verify") |
| 92 | .setDescription( |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 93 | "Verify was opened in another tab or window, please complete the check there to continue" |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 94 | ) |
| 95 | .setStatus("Success") |
| 96 | .setEmoji("MEMBER.JOIN") |
| 97 | ] |
| 98 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 99 | } catch { |
| 100 | return res.sendStatus(410); |
| 101 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | const data = structuredClone(client.verify[code]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 103 | delete data.interaction; |
| 104 | return res.status(200).send(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 105 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 106 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 107 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 108 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 109 | app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 110 | const code = req.params.code; |
| 111 | const secret = req.body.secret; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 112 | if (secret === client.config.verifySecret) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 113 | const guild = (await client.guilds.fetch(client.roleMenu[code]!.guild)) as Guild | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 114 | if (!guild) { |
| 115 | return res.status(404); |
| 116 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 117 | const member = (await guild.members.fetch(client.roleMenu[code]!.user)) as GuildMember | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 118 | if (!member) { |
| 119 | return res.status(404); |
| 120 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 121 | res.sendStatus(200); |
| 122 | } else { |
| 123 | res.sendStatus(403); |
| 124 | } |
| 125 | }); |
| 126 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 127 | app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 128 | const code = req.params.code; |
| 129 | if (client.roleMenu[code] !== undefined) { |
| 130 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 131 | const interaction = client.roleMenu[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 132 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | } catch { |
| 145 | return res.sendStatus(410); |
| 146 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 147 | const data = structuredClone(client.roleMenu[code]); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 148 | delete data.interaction; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 149 | console.log(data); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 150 | return res.status(200).send(data); |
| 151 | } |
| 152 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | }); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 154 | |
PineappleFan | 8f5cb24 | 2023-02-28 16:40:19 +0000 | [diff] [blame] | 155 | app.get("/transcript/:code/human", jsonParser, async function (req: express.Request, res: express.Response) { |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 156 | const code = req.params.code; |
| 157 | if (code === undefined) return res.status(400).send("No code provided"); |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 158 | const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 159 | 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); |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 162 | res.attachment(`${code}.txt`); |
| 163 | res.type("txt"); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 164 | return res.status(200).send(data); |
| 165 | }); |
| 166 | |
PineappleFan | 8f5cb24 | 2023-02-28 16:40:19 +0000 | [diff] [blame] | 167 | 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"); |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 170 | const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string); |
PineappleFan | 8f5cb24 | 2023-02-28 16:40:19 +0000 | [diff] [blame] | 171 | 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 | |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 176 | 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); |
TheCodedProf | b7a7b99 | 2023-03-05 16:11:59 -0500 | [diff] [blame^] | 180 | if (channel === null) return res.status(400).send("Could not find a channel by that id"); |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 181 | 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 Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 192 | console.log(e); |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 193 | return res.status(404).send("Could not find a user by that id"); |
| 194 | } |
| 195 | return res.status(200).send(user.username); |
| 196 | }); |
| 197 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 198 | app.listen(port); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 199 | }; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 200 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 201 | export default runServer; |