PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame^] | 1 | import type { Client } from 'discord.js'; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 2 | import express from "express"; |
| 3 | import bodyParser from "body-parser"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | import structuredClone from "@ungap/structured-clone"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 6 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 7 | const jsonParser = bodyParser.json(); |
| 8 | const app = express(); |
| 9 | const port = 10000; |
| 10 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame^] | 11 | const runServer = (client: Client) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | app.get("/", (req, res) => { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 13 | res.status(200).send(client.ws.ping); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 14 | }); |
| 15 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | app.post("/verify/:code", jsonParser, async function (req, res) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 17 | const code = req.params.code; |
| 18 | const secret = req.body.secret; |
| 19 | if (secret === client.config.verifySecret) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | const guild = await client.guilds.fetch(client.verify[code].gID); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 21 | if (!guild) { |
| 22 | return res.status(404); |
| 23 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | const member = await guild.members.fetch(client.verify[code].uID); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 25 | if (!member) { |
| 26 | return res.status(404); |
| 27 | } |
| 28 | if (member.roles.cache.has(client.verify[code].rID)) { |
| 29 | return res.status(200); |
| 30 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 31 | await member.roles.add(client.verify[code].rID); |
| 32 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | const interaction = client.verify[code].interaction; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 34 | if (interaction) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 35 | interaction.editReply({ |
| 36 | embeds: [ |
| 37 | new EmojiEmbed() |
| 38 | .setTitle("Verify") |
| 39 | .setDescription("Verification complete") |
| 40 | .setStatus("Success") |
| 41 | .setEmoji("MEMBER.JOIN") |
| 42 | ], |
| 43 | components: [] |
| 44 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 45 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 46 | client.verify = client.verify.filter((v) => v.code !== code); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 47 | const { log, NucleusColors, entry, renderUser } = client.logger; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 48 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 50 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | type: "memberVerify", |
| 52 | displayName: "Member Verified", |
| 53 | calculateType: "guildMemberVerify", |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 54 | color: NucleusColors.green, |
| 55 | emoji: "CONTROL.BLOCKTICK", |
| 56 | timestamp: new Date().getTime() |
| 57 | }, |
| 58 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 59 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 60 | member: entry(member.id, renderUser(member)) |
| 61 | }, |
| 62 | hidden: { |
| 63 | guild: guild.id |
| 64 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 65 | }; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 66 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 67 | } catch { |
| 68 | res.sendStatus(500); |
| 69 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 70 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 71 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 72 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 73 | } |
| 74 | }); |
| 75 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | app.get("/verify/:code", jsonParser, function (req, res) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 77 | const code = req.params.code; |
| 78 | if (client.verify[code]) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 79 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | const interaction = client.verify[code].interaction; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 81 | if (interaction) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | interaction.editReply({ |
| 83 | embeds: [ |
| 84 | new EmojiEmbed() |
| 85 | .setTitle("Verify") |
| 86 | .setDescription( |
| 87 | "Verify was opened in another tab or window, please complete the CAPTCHA there to continue" |
| 88 | ) |
| 89 | .setStatus("Success") |
| 90 | .setEmoji("MEMBER.JOIN") |
| 91 | ] |
| 92 | }); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 93 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 94 | } catch { |
| 95 | return res.sendStatus(410); |
| 96 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 97 | const data = structuredClone(client.verify[code]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 98 | delete data.interaction; |
| 99 | return res.status(200).send(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 100 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 101 | return res.sendStatus(404); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 103 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 104 | app.post("/rolemenu/:code", jsonParser, async function (req, res) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 105 | const code = req.params.code; |
| 106 | const secret = req.body.secret; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 107 | if (secret === client.config.verifySecret) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 108 | const guild = await client.guilds.fetch(client.roleMenu[code].guild); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | if (!guild) { |
| 110 | return res.status(404); |
| 111 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 112 | const member = await guild.members.fetch(client.roleMenu[code].user); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | if (!member) { |
| 114 | return res.status(404); |
| 115 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 116 | res.sendStatus(200); |
| 117 | } else { |
| 118 | res.sendStatus(403); |
| 119 | } |
| 120 | }); |
| 121 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 122 | app.get("/rolemenu/:code", jsonParser, function (req, res) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 123 | const code = req.params.code; |
| 124 | if (client.roleMenu[code] !== undefined) { |
| 125 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | const interaction = client.roleMenu[code].interaction; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 127 | if (interaction) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | interaction.editReply({ |
| 129 | embeds: [ |
| 130 | new EmojiEmbed() |
| 131 | .setTitle("Roles") |
| 132 | .setDescription( |
| 133 | "The role menu was opened in another tab or window, please select your roles there to continue" |
| 134 | ) |
| 135 | .setStatus("Success") |
| 136 | .setEmoji("GUILD.GREEN") |
| 137 | ], |
| 138 | components: [] |
| 139 | }); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 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 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 152 | app.listen(port); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | }; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 154 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 155 | export default runServer; |