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'; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 5 | import structuredClone from '@ungap/structured-clone'; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame^] | 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) => { |
| 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 | |
| 18 | app.post('/verify/:code', jsonParser, async function (req, res) { |
| 19 | const code = req.params.code; |
| 20 | const secret = req.body.secret; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame^] | 21 | const { log, NucleusColors, entry, renderUser } = client.logger |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 22 | if (secret === client.config.verifySecret) { |
| 23 | let guild = await client.guilds.fetch(client.verify[code].gID); |
| 24 | if (!guild) { return res.status(404) } |
| 25 | let member = await guild.members.fetch(client.verify[code].uID); |
| 26 | if (!member) { return res.status(404) } |
| 27 | if (member.roles.cache.has(client.verify[code].rID)) { return res.status(200) } |
| 28 | await member.roles.add(client.verify[code].rID); |
| 29 | |
| 30 | let interaction = client.verify[code].interaction; |
| 31 | if (interaction) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 32 | interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 33 | .setTitle("Verify") |
| 34 | .setDescription(`Verification complete`) |
| 35 | .setStatus("Success") |
| 36 | .setEmoji("MEMBER.JOIN") |
| 37 | ], components: []}); |
| 38 | } |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame^] | 39 | try { |
| 40 | let data = { |
| 41 | meta:{ |
| 42 | type: 'memberVerify', |
| 43 | displayName: 'Member Verified', |
| 44 | calculateType: 'guildMemberVerify', |
| 45 | color: NucleusColors.green, |
| 46 | emoji: "CONTROL.BLOCKTICK", |
| 47 | timestamp: new Date().getTime() |
| 48 | }, |
| 49 | list: { |
| 50 | id: entry(member.id, `\`${member.id}\``), |
| 51 | member: entry(member.id, renderUser(member)) |
| 52 | }, |
| 53 | hidden: { |
| 54 | guild: guild.id |
| 55 | } |
| 56 | } |
| 57 | log(data); |
| 58 | } catch {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 59 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 60 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 61 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 62 | } |
| 63 | }); |
| 64 | |
| 65 | app.patch('/verify/:code', (req, res) => { |
| 66 | const code = req.params.code; |
| 67 | try { |
| 68 | let interaction = client.verify[code].interaction; |
| 69 | if (interaction) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 70 | interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 71 | .setTitle("Verify") |
| 72 | .setDescription(`Verify was opened in another tab or window, please complete the CAPTCHA there to continue`) |
| 73 | .setStatus("Success") |
| 74 | .setEmoji("MEMBER.JOIN") |
| 75 | ]}); |
| 76 | } |
| 77 | } catch {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 78 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 79 | }) |
| 80 | |
| 81 | app.get('/verify/:code', jsonParser, function (req, res) { |
| 82 | const code = req.params.code; |
| 83 | if (client.verify[code]) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 84 | let data = structuredClone(client.verify[code]) |
| 85 | delete data.interaction; |
| 86 | return res.status(200).send(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 87 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 88 | return res.sendStatus(404); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 89 | }) |
| 90 | |
| 91 | app.listen(port); |
| 92 | } |
| 93 | |
| 94 | export default runServer; |