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