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