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"; |
| 5 | |
| 6 | const jsonParser = bodyParser.json(); |
| 7 | const app = express(); |
| 8 | const port = 10000; |
| 9 | |
| 10 | const runServer = (client: HaikuClient) => { |
| 11 | app.get('/', (req, res) => { |
| 12 | res.send(client.ws.ping); |
| 13 | }); |
| 14 | |
| 15 | app.post('/verify/:code', jsonParser, async function (req, res) { |
| 16 | const code = req.params.code; |
| 17 | const secret = req.body.secret; |
| 18 | if (secret === client.config.verifySecret) { |
| 19 | let guild = await client.guilds.fetch(client.verify[code].gID); |
| 20 | if (!guild) { return res.status(404) } |
| 21 | let member = await guild.members.fetch(client.verify[code].uID); |
| 22 | if (!member) { return res.status(404) } |
| 23 | if (member.roles.cache.has(client.verify[code].rID)) { return res.status(200) } |
| 24 | await member.roles.add(client.verify[code].rID); |
| 25 | |
| 26 | let interaction = client.verify[code].interaction; |
| 27 | if (interaction) { |
| 28 | interaction.editReply({embeds: [new generateEmojiEmbed() |
| 29 | .setTitle("Verify") |
| 30 | .setDescription(`Verification complete`) |
| 31 | .setStatus("Success") |
| 32 | .setEmoji("MEMBER.JOIN") |
| 33 | ], components: []}); |
| 34 | } |
| 35 | res.status(200).send(); |
| 36 | } else { |
| 37 | res.status(403).send(); |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | app.patch('/verify/:code', (req, res) => { |
| 42 | const code = req.params.code; |
| 43 | try { |
| 44 | let interaction = client.verify[code].interaction; |
| 45 | if (interaction) { |
| 46 | interaction.editReply({embeds: [new generateEmojiEmbed() |
| 47 | .setTitle("Verify") |
| 48 | .setDescription(`Verify was opened in another tab or window, please complete the CAPTCHA there to continue`) |
| 49 | .setStatus("Success") |
| 50 | .setEmoji("MEMBER.JOIN") |
| 51 | ]}); |
| 52 | } |
| 53 | } catch {} |
| 54 | res.status(200).send(); |
| 55 | }) |
| 56 | |
| 57 | app.get('/verify/:code', jsonParser, function (req, res) { |
| 58 | const code = req.params.code; |
| 59 | if (client.verify[code]) { |
| 60 | let data = structuredClone(client.verify[code]) |
| 61 | delete data.interaction; |
| 62 | return res.status(200).send(data); |
| 63 | } |
| 64 | return res.status(404).send(); |
| 65 | }) |
| 66 | |
| 67 | app.listen(port); |
| 68 | } |
| 69 | |
| 70 | export default runServer; |