pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 1 | import { HaikuClient } from 'jshaiku'; |
| 2 | import express from 'express'; |
| 3 | import bodyParser from 'body-parser'; |
| 4 | import generateEmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 5 | import structuredClone from '@ungap/structured-clone'; |
| 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) => { |
| 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 | |
| 17 | app.post('/verify/:code', jsonParser, async function (req, res) { |
| 18 | const code = req.params.code; |
| 19 | const secret = req.body.secret; |
| 20 | if (secret === client.config.verifySecret) { |
| 21 | let guild = await client.guilds.fetch(client.verify[code].gID); |
| 22 | if (!guild) { return res.status(404) } |
| 23 | let 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) } |
| 26 | await member.roles.add(client.verify[code].rID); |
| 27 | |
| 28 | let interaction = client.verify[code].interaction; |
| 29 | if (interaction) { |
| 30 | interaction.editReply({embeds: [new generateEmojiEmbed() |
| 31 | .setTitle("Verify") |
| 32 | .setDescription(`Verification complete`) |
| 33 | .setStatus("Success") |
| 34 | .setEmoji("MEMBER.JOIN") |
| 35 | ], components: []}); |
| 36 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 37 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 38 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 39 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 40 | } |
| 41 | }); |
| 42 | |
| 43 | app.patch('/verify/:code', (req, res) => { |
| 44 | const code = req.params.code; |
| 45 | try { |
| 46 | let interaction = client.verify[code].interaction; |
| 47 | if (interaction) { |
| 48 | interaction.editReply({embeds: [new generateEmojiEmbed() |
| 49 | .setTitle("Verify") |
| 50 | .setDescription(`Verify was opened in another tab or window, please complete the CAPTCHA there to continue`) |
| 51 | .setStatus("Success") |
| 52 | .setEmoji("MEMBER.JOIN") |
| 53 | ]}); |
| 54 | } |
| 55 | } catch {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 56 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 57 | }) |
| 58 | |
| 59 | app.get('/verify/:code', jsonParser, function (req, res) { |
| 60 | const code = req.params.code; |
| 61 | if (client.verify[code]) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 62 | let data = structuredClone(client.verify[code]) |
| 63 | delete data.interaction; |
| 64 | return res.status(200).send(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 65 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 66 | return res.sendStatus(404); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 67 | }) |
| 68 | |
| 69 | app.listen(port); |
| 70 | } |
| 71 | |
| 72 | export default runServer; |