blob: d6d2a5ad514eec5854892ac7450d53de3e08926e [file] [log] [blame]
PineaFan638eb132023-01-19 10:41:22 +00001import type { Guild, GuildMember } from 'discord.js';
TheCodedProf51296102023-01-18 22:35:02 -05002import type { NucleusClient } from '../utils/client.js';
3//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01004import express from "express";
TheCodedProf51296102023-01-18 22:35:02 -05005//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01006import bodyParser from "body-parser";
pineafan4edb7762022-06-26 19:21:04 +01007import EmojiEmbed from "../utils/generateEmojiEmbed.js";
TheCodedProf51296102023-01-18 22:35:02 -05008//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01009import structuredClone from "@ungap/structured-clone";
pineafan6702cef2022-06-13 17:52:37 +010010
pineafane625d782022-05-09 18:04:32 +010011const jsonParser = bodyParser.json();
12const app = express();
13const port = 10000;
14
TheCodedProf51296102023-01-18 22:35:02 -050015const runServer = (client: NucleusClient) => {
PineaFan638eb132023-01-19 10:41:22 +000016 app.get("/", (_req: express.Request, res: express.Response) => {
pineafan6702cef2022-06-13 17:52:37 +010017 res.status(200).send(client.ws.ping);
pineafane625d782022-05-09 18:04:32 +010018 });
19
PineaFan638eb132023-01-19 10:41:22 +000020 app.post("/verify/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010021 const code = req.params.code;
22 const secret = req.body.secret;
23 if (secret === client.config.verifySecret) {
PineaFan638eb132023-01-19 10:41:22 +000024 const guild = await client.guilds.fetch(client.verify[code]!.gID) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +010025 if (!guild) {
26 return res.status(404);
27 }
PineaFan638eb132023-01-19 10:41:22 +000028 const member = await guild.members.fetch(client.verify[code]!.uID) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +010029 if (!member) {
30 return res.status(404);
31 }
TheCodedProf51296102023-01-18 22:35:02 -050032 if (member.roles.cache.has(client.verify[code]!.rID)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010033 return res.status(200);
34 }
TheCodedProf51296102023-01-18 22:35:02 -050035 await member.roles.add(client.verify[code]!.rID);
pineafane625d782022-05-09 18:04:32 +010036
TheCodedProf51296102023-01-18 22:35:02 -050037 const interaction = client.verify[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +000038 interaction.editReply({
39 embeds: [
40 new EmojiEmbed()
41 .setTitle("Verify")
42 .setDescription("Verification complete")
43 .setStatus("Success")
44 .setEmoji("MEMBER.JOIN")
45 ],
46 components: []
47 });
48 // client.verify.filter((v: VerifySchema) => v.code !== code);
49 // delete the key by creating a new object without the key
50 client.verify = Object.keys(client.verify)
51 .filter((k) => k !== code)
52 .reduce((obj, key) => {return { ...obj, [key]: client.verify[key]}}, {});
pineafan63fc5e22022-08-04 22:04:10 +010053 const { log, NucleusColors, entry, renderUser } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010054 try {
pineafan63fc5e22022-08-04 22:04:10 +010055 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010056 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010057 type: "memberVerify",
58 displayName: "Member Verified",
59 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010060 color: NucleusColors.green,
61 emoji: "CONTROL.BLOCKTICK",
62 timestamp: new Date().getTime()
63 },
64 list: {
pineafanda6e5342022-07-03 10:03:16 +010065 memberId: entry(member.id, `\`${member.id}\``),
TheCodedProf51296102023-01-18 22:35:02 -050066 member: entry(member.id, renderUser(member.user))
pineafan412beec2022-06-29 21:46:26 +010067 },
68 hidden: {
69 guild: guild.id
70 }
pineafan63fc5e22022-08-04 22:04:10 +010071 };
pineafan412beec2022-06-29 21:46:26 +010072 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010073 } catch {
74 res.sendStatus(500);
75 }
pineafan6702cef2022-06-13 17:52:37 +010076 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010077 } else {
pineafan6702cef2022-06-13 17:52:37 +010078 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010079 }
80 });
81
PineaFan638eb132023-01-19 10:41:22 +000082 app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010083 const code = req.params.code;
84 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010085 try {
TheCodedProf51296102023-01-18 22:35:02 -050086 const interaction = client.verify[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +000087 interaction.editReply({
88 embeds: [
89 new EmojiEmbed()
90 .setTitle("Verify")
91 .setDescription(
92 "Verify was opened in another tab or window, please complete the CAPTCHA there to continue"
93 )
94 .setStatus("Success")
95 .setEmoji("MEMBER.JOIN")
96 ]
97 });
Skyler Grey75ea9172022-08-06 10:22:23 +010098 } catch {
99 return res.sendStatus(410);
100 }
pineafan63fc5e22022-08-04 22:04:10 +0100101 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +0100102 delete data.interaction;
103 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +0100104 }
pineafan6702cef2022-06-13 17:52:37 +0100105 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100106 });
pineafane625d782022-05-09 18:04:32 +0100107
PineaFan638eb132023-01-19 10:41:22 +0000108 app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100109 const code = req.params.code;
110 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +0100111 if (secret === client.config.verifySecret) {
PineaFan638eb132023-01-19 10:41:22 +0000112 const guild = await client.guilds.fetch(client.roleMenu[code]!.guild) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100113 if (!guild) {
114 return res.status(404);
115 }
PineaFan638eb132023-01-19 10:41:22 +0000116 const member = await guild.members.fetch(client.roleMenu[code]!.user) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 if (!member) {
118 return res.status(404);
119 }
pineafanda6e5342022-07-03 10:03:16 +0100120 res.sendStatus(200);
121 } else {
122 res.sendStatus(403);
123 }
124 });
125
PineaFan638eb132023-01-19 10:41:22 +0000126 app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100127 const code = req.params.code;
128 if (client.roleMenu[code] !== undefined) {
129 try {
TheCodedProf51296102023-01-18 22:35:02 -0500130 const interaction = client.roleMenu[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +0000131 interaction.editReply({
132 embeds: [
133 new EmojiEmbed()
134 .setTitle("Roles")
135 .setDescription(
136 "The role menu was opened in another tab or window, please select your roles there to continue"
137 )
138 .setStatus("Success")
139 .setEmoji("GUILD.GREEN")
140 ],
141 components: []
142 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 } catch {
144 return res.sendStatus(410);
145 }
pineafan63fc5e22022-08-04 22:04:10 +0100146 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100147 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100148 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100149 return res.status(200).send(data);
150 }
151 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100152 });
pineafanda6e5342022-07-03 10:03:16 +0100153
pineafane625d782022-05-09 18:04:32 +0100154 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100155};
pineafane625d782022-05-09 18:04:32 +0100156
Skyler Grey75ea9172022-08-06 10:22:23 +0100157export default runServer;