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") |
| 42 | .setDescription("Verification complete") |
| 43 | .setStatus("Success") |
| 44 | .setEmoji("MEMBER.JOIN") |
| 45 | ], |
| 46 | components: [] |
| 47 | }); |
| 48 | // client.verify.filter((v: VerifySchema) => v.code !== code); |
| 49 | // delete the key by creating a new object without the key |
| 50 | client.verify = Object.keys(client.verify) |
| 51 | .filter((k) => k !== code) |
| 52 | .reduce((obj, key) => {return { ...obj, [key]: client.verify[key]}}, {}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | const { log, NucleusColors, entry, renderUser } = 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", |
| 62 | timestamp: new Date().getTime() |
| 63 | }, |
| 64 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 65 | memberId: entry(member.id, `\`${member.id}\``), |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 66 | member: entry(member.id, renderUser(member.user)) |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 67 | }, |
| 68 | hidden: { |
| 69 | guild: guild.id |
| 70 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 71 | }; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 72 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 73 | } catch { |
| 74 | res.sendStatus(500); |
| 75 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 76 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 77 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 78 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 79 | } |
| 80 | }); |
| 81 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 82 | app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 83 | const code = req.params.code; |
| 84 | if (client.verify[code]) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 85 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 86 | const interaction = client.verify[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 87 | interaction.editReply({ |
| 88 | embeds: [ |
| 89 | new EmojiEmbed() |
| 90 | .setTitle("Verify") |
| 91 | .setDescription( |
| 92 | "Verify was opened in another tab or window, please complete the CAPTCHA there to continue" |
| 93 | ) |
| 94 | .setStatus("Success") |
| 95 | .setEmoji("MEMBER.JOIN") |
| 96 | ] |
| 97 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 98 | } catch { |
| 99 | return res.sendStatus(410); |
| 100 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 101 | const data = structuredClone(client.verify[code]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 102 | delete data.interaction; |
| 103 | return res.status(200).send(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 104 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 105 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 106 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 107 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 108 | app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 109 | const code = req.params.code; |
| 110 | const secret = req.body.secret; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 111 | if (secret === client.config.verifySecret) { |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 112 | 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] | 113 | if (!guild) { |
| 114 | return res.status(404); |
| 115 | } |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 116 | 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] | 117 | if (!member) { |
| 118 | return res.status(404); |
| 119 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 120 | res.sendStatus(200); |
| 121 | } else { |
| 122 | res.sendStatus(403); |
| 123 | } |
| 124 | }); |
| 125 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 126 | app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 127 | const code = req.params.code; |
| 128 | if (client.roleMenu[code] !== undefined) { |
| 129 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 130 | const interaction = client.roleMenu[code]!.interaction; |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 131 | interaction.editReply({ |
| 132 | embeds: [ |
| 133 | new EmojiEmbed() |
| 134 | .setTitle("Roles") |
| 135 | .setDescription( |
| 136 | "The role menu was opened in another tab or window, please select your roles there to continue" |
| 137 | ) |
| 138 | .setStatus("Success") |
| 139 | .setEmoji("GUILD.GREEN") |
| 140 | ], |
| 141 | components: [] |
| 142 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 143 | } catch { |
| 144 | return res.sendStatus(410); |
| 145 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 146 | const data = structuredClone(client.roleMenu[code]); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 147 | delete data.interaction; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 148 | console.log(data); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 149 | return res.status(200).send(data); |
| 150 | } |
| 151 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 152 | }); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 153 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 154 | app.listen(port); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 155 | }; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 156 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 157 | export default runServer; |