blob: 07131fff500352d1f1121894d40152228585fcb6 [file] [log] [blame]
pineafane625d782022-05-09 18:04:32 +01001import { HaikuClient } from 'jshaiku';
2import express from 'express';
3import bodyParser from 'body-parser';
4import generateEmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan6702cef2022-06-13 17:52:37 +01005import structuredClone from '@ungap/structured-clone';
6
pineafane625d782022-05-09 18:04:32 +01007
8const jsonParser = bodyParser.json();
9const app = express();
10const port = 10000;
11
12const runServer = (client: HaikuClient) => {
13 app.get('/', (req, res) => {
pineafan6702cef2022-06-13 17:52:37 +010014 res.status(200).send(client.ws.ping);
pineafane625d782022-05-09 18:04:32 +010015 });
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 }
pineafan6702cef2022-06-13 17:52:37 +010037 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010038 } else {
pineafan6702cef2022-06-13 17:52:37 +010039 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010040 }
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 {}
pineafan6702cef2022-06-13 17:52:37 +010056 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010057 })
58
59 app.get('/verify/:code', jsonParser, function (req, res) {
60 const code = req.params.code;
61 if (client.verify[code]) {
pineafan6702cef2022-06-13 17:52:37 +010062 let data = structuredClone(client.verify[code])
63 delete data.interaction;
64 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +010065 }
pineafan6702cef2022-06-13 17:52:37 +010066 return res.sendStatus(404);
pineafane625d782022-05-09 18:04:32 +010067 })
68
69 app.listen(port);
70}
71
72export default runServer;