blob: c429452a6c8244c742298000bfadfd8a7780f260 [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;
21 if (secret === client.config.verifySecret) {
22 let guild = await client.guilds.fetch(client.verify[code].gID);
23 if (!guild) { return res.status(404) }
24 let member = await guild.members.fetch(client.verify[code].uID);
25 if (!member) { return res.status(404) }
26 if (member.roles.cache.has(client.verify[code].rID)) { return res.status(200) }
27 await member.roles.add(client.verify[code].rID);
28
29 let interaction = client.verify[code].interaction;
30 if (interaction) {
pineafan4edb7762022-06-26 19:21:04 +010031 interaction.editReply({embeds: [new EmojiEmbed()
pineafane625d782022-05-09 18:04:32 +010032 .setTitle("Verify")
33 .setDescription(`Verification complete`)
34 .setStatus("Success")
35 .setEmoji("MEMBER.JOIN")
36 ], components: []});
37 }
pineafanda6e5342022-07-03 10:03:16 +010038 delete client.verify[code];
39 const { log, NucleusColors, entry, renderUser } = client.logger
pineafan412beec2022-06-29 21:46:26 +010040 try {
41 let data = {
42 meta:{
43 type: 'memberVerify',
44 displayName: 'Member Verified',
45 calculateType: 'guildMemberVerify',
46 color: NucleusColors.green,
47 emoji: "CONTROL.BLOCKTICK",
48 timestamp: new Date().getTime()
49 },
50 list: {
pineafanda6e5342022-07-03 10:03:16 +010051 memberId: entry(member.id, `\`${member.id}\``),
pineafan412beec2022-06-29 21:46:26 +010052 member: entry(member.id, renderUser(member))
53 },
54 hidden: {
55 guild: guild.id
56 }
57 }
58 log(data);
59 } catch {}
pineafan6702cef2022-06-13 17:52:37 +010060 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010061 } else {
pineafan6702cef2022-06-13 17:52:37 +010062 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010063 }
64 });
65
pineafane625d782022-05-09 18:04:32 +010066 app.get('/verify/:code', jsonParser, function (req, res) {
67 const code = req.params.code;
68 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010069 try {
70 let interaction = client.verify[code].interaction;
71 if (interaction) {
72 interaction.editReply({embeds: [new EmojiEmbed()
73 .setTitle("Verify")
74 .setDescription(`Verify was opened in another tab or window, please complete the CAPTCHA there to continue`)
75 .setStatus("Success")
76 .setEmoji("MEMBER.JOIN")
77 ]});
78 }
79 } catch {}
pineafan6702cef2022-06-13 17:52:37 +010080 let data = structuredClone(client.verify[code])
81 delete data.interaction;
82 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +010083 }
pineafan6702cef2022-06-13 17:52:37 +010084 return res.sendStatus(404);
pineafane625d782022-05-09 18:04:32 +010085 })
86
pineafanda6e5342022-07-03 10:03:16 +010087 app.post('/rolemenu/:code', jsonParser, async function (req, res) {
88 const code = req.params.code;
89 const secret = req.body.secret;
90 const data = req.body.data;
91 if (secret === client.config.verifySecret) {
92 console.table(data)
pineafan73a7c4a2022-07-24 10:38:04 +010093 let guild = await client.guilds.fetch(client.roleMenu[code].guild);
pineafanda6e5342022-07-03 10:03:16 +010094 if (!guild) { return res.status(404) }
95 let member = await guild.members.fetch(client.roleMenu[code].user);
96 if (!member) { return res.status(404) }
97 res.sendStatus(200);
98 } else {
99 res.sendStatus(403);
100 }
101 });
102
103 app.get('/rolemenu/:code', jsonParser, function (req, res) {
104 const code = req.params.code;
105 if (client.roleMenu[code] !== undefined) {
106 try {
107 let interaction = client.roleMenu[code].interaction;
108 if (interaction) {
109 interaction.editReply({embeds: [new EmojiEmbed()
110 .setTitle("Roles")
111 .setDescription(`The role menu was opened in another tab or window, please select your roles there to continue`)
112 .setStatus("Success")
113 .setEmoji("GUILD.GREEN")
114 ], components: []});
115 }
116 } catch {}
117 let data = structuredClone(client.roleMenu[code])
118 delete data.interaction;
119 console.log(data)
120 return res.status(200).send(data);
121 }
122 return res.sendStatus(404);
123 })
124
pineafane625d782022-05-09 18:04:32 +0100125 app.listen(port);
126}
127
128export default runServer;