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", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -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)), |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame] | 64 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp!)), |
| 65 | verified: entry(Date.now(), renderDelta(Date.now())) |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 66 | }, |
| 67 | hidden: { |
| 68 | guild: guild.id |
| 69 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | }; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 71 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | } catch { |
| 73 | res.sendStatus(500); |
| 74 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 75 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 76 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 77 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 78 | } |
| 79 | }); |
| 80 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 81 | app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 82 | const code = req.params.code; |
| 83 | if (client.verify[code]) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 84 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 85 | const interaction = client.verify[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 86 | interaction.editReply({ |
| 87 | embeds: [ |
| 88 | new EmojiEmbed() |
| 89 | .setTitle("Verify") |
| 90 | .setDescription( |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 91 | "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] | 92 | ) |
| 93 | .setStatus("Success") |
| 94 | .setEmoji("MEMBER.JOIN") |
| 95 | ] |
| 96 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | } catch { |
| 98 | return res.sendStatus(410); |
| 99 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | const data = structuredClone(client.verify[code]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 101 | delete data.interaction; |
| 102 | return res.status(200).send(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 103 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 104 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 105 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 106 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 107 | app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 108 | const code = req.params.code; |
| 109 | const secret = req.body.secret; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 110 | if (secret === client.config.verifySecret) { |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 111 | 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] | 112 | if (!guild) { |
| 113 | return res.status(404); |
| 114 | } |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 115 | 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] | 116 | if (!member) { |
| 117 | return res.status(404); |
| 118 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 119 | res.sendStatus(200); |
| 120 | } else { |
| 121 | res.sendStatus(403); |
| 122 | } |
| 123 | }); |
| 124 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 125 | app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 126 | const code = req.params.code; |
| 127 | if (client.roleMenu[code] !== undefined) { |
| 128 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 129 | const interaction = client.roleMenu[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 130 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 142 | } catch { |
| 143 | return res.sendStatus(410); |
| 144 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 145 | const data = structuredClone(client.roleMenu[code]); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 146 | delete data.interaction; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 147 | console.log(data); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 148 | return res.status(200).send(data); |
| 149 | } |
| 150 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 151 | }); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 152 | |
PineappleFan | 8f5cb24 | 2023-02-28 16:40:19 +0000 | [diff] [blame] | 153 | 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] | 154 | const code = req.params.code; |
| 155 | if (code === undefined) return res.status(400).send("No code provided"); |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 156 | 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] | 157 | 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); |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 160 | res.attachment(`${code}.txt`); |
| 161 | res.type("txt"); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 162 | return res.status(200).send(data); |
| 163 | }); |
| 164 | |
PineappleFan | 8f5cb24 | 2023-02-28 16:40:19 +0000 | [diff] [blame] | 165 | 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"); |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 168 | 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] | 169 | 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 | |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 174 | 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 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 196 | app.listen(port); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 197 | }; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 198 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 199 | export default runServer; |