PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 1 | import type { Guild, GuildMember } from 'discord.js'; |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 2 | import type { NucleusClient } from '../utils/client.js'; |
| 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) => { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 17 | res.status(200).send(client.ws.ping); |
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) { |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +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 | } |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +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) |
| 50 | .reduce((obj, key) => {return { ...obj, [key]: client.verify[key]}}, {}); |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 51 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 52 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 54 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | type: "memberVerify", |
| 56 | displayName: "Member Verified", |
| 57 | calculateType: "guildMemberVerify", |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 58 | color: NucleusColors.green, |
| 59 | emoji: "CONTROL.BLOCKTICK", |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 60 | timestamp: Date.now() |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 61 | }, |
| 62 | list: { |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 63 | member: entry(member.id, renderUser(member.user)), |
| 64 | verified: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp!)) |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 65 | }, |
| 66 | hidden: { |
| 67 | guild: guild.id |
| 68 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 69 | }; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 70 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 71 | } catch { |
| 72 | res.sendStatus(500); |
| 73 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 74 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 75 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 76 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 77 | } |
| 78 | }); |
| 79 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 80 | app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 81 | const code = req.params.code; |
| 82 | if (client.verify[code]) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 83 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 84 | const interaction = client.verify[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 85 | interaction.editReply({ |
| 86 | embeds: [ |
| 87 | new EmojiEmbed() |
| 88 | .setTitle("Verify") |
| 89 | .setDescription( |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 90 | "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] | 91 | ) |
| 92 | .setStatus("Success") |
| 93 | .setEmoji("MEMBER.JOIN") |
| 94 | ] |
| 95 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 96 | } catch { |
| 97 | return res.sendStatus(410); |
| 98 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 99 | const data = structuredClone(client.verify[code]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 100 | delete data.interaction; |
| 101 | return res.status(200).send(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 102 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 103 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 104 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 105 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 106 | app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 107 | const code = req.params.code; |
| 108 | const secret = req.body.secret; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 109 | if (secret === client.config.verifySecret) { |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 110 | 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] | 111 | if (!guild) { |
| 112 | return res.status(404); |
| 113 | } |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 114 | 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] | 115 | if (!member) { |
| 116 | return res.status(404); |
| 117 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 118 | res.sendStatus(200); |
| 119 | } else { |
| 120 | res.sendStatus(403); |
| 121 | } |
| 122 | }); |
| 123 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 124 | app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 125 | const code = req.params.code; |
| 126 | if (client.roleMenu[code] !== undefined) { |
| 127 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 128 | const interaction = client.roleMenu[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 129 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 141 | } catch { |
| 142 | return res.sendStatus(410); |
| 143 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 144 | const data = structuredClone(client.roleMenu[code]); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 145 | delete data.interaction; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 146 | console.log(data); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 147 | return res.status(200).send(data); |
| 148 | } |
| 149 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 150 | }); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 151 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 152 | app.get("/transcript/:code/human", jsonParser, async function (req: express.Request, res: express.Response) { |
| 153 | const code = req.params.code; |
| 154 | if (code === undefined) return res.status(400).send("No code provided"); |
Samuel Shuert | cc63dee | 2023-03-03 18:54:29 -0500 | [diff] [blame] | 155 | const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 156 | 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); |
| 159 | res.attachment(`${code}.txt`); |
| 160 | res.type("txt"); |
| 161 | return res.status(200).send(data); |
| 162 | }); |
| 163 | |
| 164 | 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"); |
Samuel Shuert | cc63dee | 2023-03-03 18:54:29 -0500 | [diff] [blame] | 167 | const entry = await client.database.transcripts.read(code, req.query.key as string, req.query.iv as string); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 168 | 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 | |
| 173 | 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 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 195 | app.listen(port); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 196 | }; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 197 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 198 | export default runServer; |