blob: c24327d607288ab2cdf1dc9328d1d6de58f834fd [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")
PineaFanc4d6c3f2023-01-19 12:17:25 +000042 .setDescription("Verification complete! You can now dismiss this message")
PineaFan638eb132023-01-19 10:41:22 +000043 .setStatus("Success")
44 .setEmoji("MEMBER.JOIN")
45 ],
46 components: []
47 });
PineaFan638eb132023-01-19 10:41:22 +000048 client.verify = Object.keys(client.verify)
49 .filter((k) => k !== code)
50 .reduce((obj, key) => {return { ...obj, [key]: client.verify[key]}}, {});
PineaFanc4d6c3f2023-01-19 12:17:25 +000051 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010052 try {
pineafan63fc5e22022-08-04 22:04:10 +010053 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010054 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010055 type: "memberVerify",
56 displayName: "Member Verified",
57 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010058 color: NucleusColors.green,
59 emoji: "CONTROL.BLOCKTICK",
60 timestamp: new Date().getTime()
61 },
62 list: {
PineaFanc4d6c3f2023-01-19 12:17:25 +000063 member: entry(member.id, renderUser(member.user)),
64 verified: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp!))
pineafan412beec2022-06-29 21:46:26 +010065 },
66 hidden: {
67 guild: guild.id
68 }
pineafan63fc5e22022-08-04 22:04:10 +010069 };
pineafan412beec2022-06-29 21:46:26 +010070 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010071 } catch {
72 res.sendStatus(500);
73 }
pineafan6702cef2022-06-13 17:52:37 +010074 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010075 } else {
pineafan6702cef2022-06-13 17:52:37 +010076 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010077 }
78 });
79
PineaFan638eb132023-01-19 10:41:22 +000080 app.get("/verify/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafane625d782022-05-09 18:04:32 +010081 const code = req.params.code;
82 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010083 try {
TheCodedProf51296102023-01-18 22:35:02 -050084 const interaction = client.verify[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +000085 interaction.editReply({
86 embeds: [
87 new EmojiEmbed()
88 .setTitle("Verify")
89 .setDescription(
PineaFanc4d6c3f2023-01-19 12:17:25 +000090 "Verify was opened in another tab or window, please complete the check there to continue"
PineaFan638eb132023-01-19 10:41:22 +000091 )
92 .setStatus("Success")
93 .setEmoji("MEMBER.JOIN")
94 ]
95 });
Skyler Grey75ea9172022-08-06 10:22:23 +010096 } catch {
97 return res.sendStatus(410);
98 }
pineafan63fc5e22022-08-04 22:04:10 +010099 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +0100100 delete data.interaction;
101 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +0100102 }
pineafan6702cef2022-06-13 17:52:37 +0100103 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100104 });
pineafane625d782022-05-09 18:04:32 +0100105
PineaFan638eb132023-01-19 10:41:22 +0000106 app.post("/rolemenu/:code", jsonParser, async function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100107 const code = req.params.code;
108 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +0100109 if (secret === client.config.verifySecret) {
PineaFan638eb132023-01-19 10:41:22 +0000110 const guild = await client.guilds.fetch(client.roleMenu[code]!.guild) as Guild | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 if (!guild) {
112 return res.status(404);
113 }
PineaFan638eb132023-01-19 10:41:22 +0000114 const member = await guild.members.fetch(client.roleMenu[code]!.user) as GuildMember | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 if (!member) {
116 return res.status(404);
117 }
pineafanda6e5342022-07-03 10:03:16 +0100118 res.sendStatus(200);
119 } else {
120 res.sendStatus(403);
121 }
122 });
123
PineaFan638eb132023-01-19 10:41:22 +0000124 app.get("/rolemenu/:code", jsonParser, function (req: express.Request, res: express.Response) {
pineafanda6e5342022-07-03 10:03:16 +0100125 const code = req.params.code;
126 if (client.roleMenu[code] !== undefined) {
127 try {
TheCodedProf51296102023-01-18 22:35:02 -0500128 const interaction = client.roleMenu[code]!.interaction;
PineaFan638eb132023-01-19 10:41:22 +0000129 interaction.editReply({
130 embeds: [
131 new EmojiEmbed()
132 .setTitle("Roles")
133 .setDescription(
134 "The role menu was opened in another tab or window, please select your roles there to continue"
135 )
136 .setStatus("Success")
137 .setEmoji("GUILD.GREEN")
138 ],
139 components: []
140 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 } catch {
142 return res.sendStatus(410);
143 }
pineafan63fc5e22022-08-04 22:04:10 +0100144 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100145 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100146 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100147 return res.status(200).send(data);
148 }
149 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100150 });
pineafanda6e5342022-07-03 10:03:16 +0100151
pineafane625d782022-05-09 18:04:32 +0100152 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100153};
pineafane625d782022-05-09 18:04:32 +0100154
Skyler Grey75ea9172022-08-06 10:22:23 +0100155export default runServer;