blob: 00c866da8db6c88344eed07ea3c52625bb311f39 [file] [log] [blame]
pineafane625d782022-05-09 18:04:32 +01001import { HaikuClient } from 'jshaiku';
2import express from 'express';
3import bodyParser from 'body-parser';
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan6702cef2022-06-13 17:52:37 +01005import structuredClone from '@ungap/structured-clone';
pineafan412beec2022-06-29 21:46:26 +01006import client from '../utils/client.js';
pineafan6702cef2022-06-13 17:52:37 +01007
pineafane625d782022-05-09 18:04:32 +01008
9const jsonParser = bodyParser.json();
10const app = express();
11const port = 10000;
12
13const runServer = (client: HaikuClient) => {
14 app.get('/', (req, res) => {
pineafan6702cef2022-06-13 17:52:37 +010015 res.status(200).send(client.ws.ping);
pineafane625d782022-05-09 18:04:32 +010016 });
17
18 app.post('/verify/:code', jsonParser, async function (req, res) {
19 const code = req.params.code;
20 const secret = req.body.secret;
pineafan412beec2022-06-29 21:46:26 +010021 const { log, NucleusColors, entry, renderUser } = client.logger
pineafane625d782022-05-09 18:04:32 +010022 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) {
pineafan4edb7762022-06-26 19:21:04 +010032 interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010033 .setTitle("Verify")
34 .setDescription(`Verification complete`)
35 .setStatus("Success")
36 .setEmoji("MEMBER.JOIN")
37 ], components: []});
38 }
pineafan412beec2022-06-29 21:46:26 +010039 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 {}
pineafan6702cef2022-06-13 17:52:37 +010059 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010060 } else {
pineafan6702cef2022-06-13 17:52:37 +010061 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010062 }
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) {
pineafan4edb7762022-06-26 19:21:04 +010070 interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010071 .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 {}
pineafan6702cef2022-06-13 17:52:37 +010078 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010079 })
80
81 app.get('/verify/:code', jsonParser, function (req, res) {
82 const code = req.params.code;
83 if (client.verify[code]) {
pineafan6702cef2022-06-13 17:52:37 +010084 let data = structuredClone(client.verify[code])
85 delete data.interaction;
86 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +010087 }
pineafan6702cef2022-06-13 17:52:37 +010088 return res.sendStatus(404);
pineafane625d782022-05-09 18:04:32 +010089 })
90
91 app.listen(port);
92}
93
94export default runServer;