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