blob: 5cc4a996e012d75898375a9fb685a6e93cc58651 [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) {
pineafan73a7c4a2022-07-24 10:38:04 +010092 let guild = await client.guilds.fetch(client.roleMenu[code].guild);
pineafanda6e5342022-07-03 10:03:16 +010093 if (!guild) { return res.status(404) }
94 let member = await guild.members.fetch(client.roleMenu[code].user);
95 if (!member) { return res.status(404) }
96 res.sendStatus(200);
97 } else {
98 res.sendStatus(403);
99 }
100 });
101
102 app.get('/rolemenu/:code', jsonParser, function (req, res) {
103 const code = req.params.code;
104 if (client.roleMenu[code] !== undefined) {
105 try {
106 let interaction = client.roleMenu[code].interaction;
107 if (interaction) {
108 interaction.editReply({embeds: [new EmojiEmbed()
109 .setTitle("Roles")
110 .setDescription(`The role menu was opened in another tab or window, please select your roles there to continue`)
111 .setStatus("Success")
112 .setEmoji("GUILD.GREEN")
113 ], components: []});
114 }
115 } catch {}
116 let data = structuredClone(client.roleMenu[code])
117 delete data.interaction;
118 console.log(data)
119 return res.status(200).send(data);
120 }
121 return res.sendStatus(404);
122 })
123
pineafane625d782022-05-09 18:04:32 +0100124 app.listen(port);
125}
126
127export default runServer;