TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 1 | import type { NucleusClient } from '../utils/client.js'; |
| 2 | //@ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 3 | import express from "express"; |
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 bodyParser from "body-parser"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 7 | //@ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 8 | import structuredClone from "@ungap/structured-clone"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 9 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 10 | const jsonParser = bodyParser.json(); |
| 11 | const app = express(); |
| 12 | const port = 10000; |
| 13 | |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 14 | const runServer = (client: NucleusClient) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | app.get("/", (req, res) => { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 16 | res.status(200).send(client.ws.ping); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 17 | }); |
| 18 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | app.post("/verify/:code", jsonParser, async function (req, res) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 20 | const code = req.params.code; |
| 21 | const secret = req.body.secret; |
| 22 | if (secret === client.config.verifySecret) { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 23 | const guild = await client.guilds.fetch(client.verify[code]!.gID); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | if (!guild) { |
| 25 | return res.status(404); |
| 26 | } |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 27 | const member = await guild.members.fetch(client.verify[code]!.uID); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 28 | if (!member) { |
| 29 | return res.status(404); |
| 30 | } |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 31 | if (member.roles.cache.has(client.verify[code]!.rID)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 32 | return res.status(200); |
| 33 | } |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 34 | await member.roles.add(client.verify[code]!.rID); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 35 | |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 36 | const interaction = client.verify[code]!.interaction; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 37 | if (interaction) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [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 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 48 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 49 | client.verify = client.verify.filter((v) => v.code !== code); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | const { log, NucleusColors, entry, renderUser } = client.logger; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 51 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 53 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | type: "memberVerify", |
| 55 | displayName: "Member Verified", |
| 56 | calculateType: "guildMemberVerify", |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 57 | color: NucleusColors.green, |
| 58 | emoji: "CONTROL.BLOCKTICK", |
| 59 | timestamp: new Date().getTime() |
| 60 | }, |
| 61 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 62 | memberId: entry(member.id, `\`${member.id}\``), |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 63 | member: entry(member.id, renderUser(member.user)) |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 64 | }, |
| 65 | hidden: { |
| 66 | guild: guild.id |
| 67 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | }; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 69 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | } catch { |
| 71 | res.sendStatus(500); |
| 72 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 73 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 74 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 75 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 76 | } |
| 77 | }); |
| 78 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | app.get("/verify/:code", jsonParser, function (req, res) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 80 | const code = req.params.code; |
| 81 | if (client.verify[code]) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 82 | try { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 83 | const interaction = client.verify[code]!.interaction; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 84 | if (interaction) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | interaction.editReply({ |
| 86 | embeds: [ |
| 87 | new EmojiEmbed() |
| 88 | .setTitle("Verify") |
| 89 | .setDescription( |
| 90 | "Verify was opened in another tab or window, please complete the CAPTCHA there to continue" |
| 91 | ) |
| 92 | .setStatus("Success") |
| 93 | .setEmoji("MEMBER.JOIN") |
| 94 | ] |
| 95 | }); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 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 | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 107 | app.post("/rolemenu/:code", jsonParser, async function (req, res) { |
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) { |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 111 | const guild = await client.guilds.fetch(client.roleMenu[code]!.guild); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | if (!guild) { |
| 113 | return res.status(404); |
| 114 | } |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame^] | 115 | const member = await guild.members.fetch(client.roleMenu[code]!.user); |
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 | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 125 | app.get("/rolemenu/:code", jsonParser, function (req, res) { |
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 | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 130 | if (interaction) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [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 | }); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 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 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 155 | app.listen(port); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 156 | }; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 157 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 158 | export default runServer; |