blob: d0df6396dbbecf9d6a6b7bd55a7a0cd310c65328 [file] [log] [blame]
TheCodedProf51296102023-01-18 22:35:02 -05001import type { NucleusClient } from '../utils/client.js';
2//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01003import express from "express";
TheCodedProf51296102023-01-18 22:35:02 -05004//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01005import bodyParser from "body-parser";
pineafan4edb7762022-06-26 19:21:04 +01006import EmojiEmbed from "../utils/generateEmojiEmbed.js";
TheCodedProf51296102023-01-18 22:35:02 -05007//@ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01008import structuredClone from "@ungap/structured-clone";
pineafan6702cef2022-06-13 17:52:37 +01009
pineafane625d782022-05-09 18:04:32 +010010const jsonParser = bodyParser.json();
11const app = express();
12const port = 10000;
13
TheCodedProf51296102023-01-18 22:35:02 -050014const runServer = (client: NucleusClient) => {
pineafan63fc5e22022-08-04 22:04:10 +010015 app.get("/", (req, res) => {
pineafan6702cef2022-06-13 17:52:37 +010016 res.status(200).send(client.ws.ping);
pineafane625d782022-05-09 18:04:32 +010017 });
18
pineafan63fc5e22022-08-04 22:04:10 +010019 app.post("/verify/:code", jsonParser, async function (req, res) {
pineafane625d782022-05-09 18:04:32 +010020 const code = req.params.code;
21 const secret = req.body.secret;
22 if (secret === client.config.verifySecret) {
TheCodedProf51296102023-01-18 22:35:02 -050023 const guild = await client.guilds.fetch(client.verify[code]!.gID);
Skyler Grey75ea9172022-08-06 10:22:23 +010024 if (!guild) {
25 return res.status(404);
26 }
TheCodedProf51296102023-01-18 22:35:02 -050027 const member = await guild.members.fetch(client.verify[code]!.uID);
Skyler Grey75ea9172022-08-06 10:22:23 +010028 if (!member) {
29 return res.status(404);
30 }
TheCodedProf51296102023-01-18 22:35:02 -050031 if (member.roles.cache.has(client.verify[code]!.rID)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010032 return res.status(200);
33 }
TheCodedProf51296102023-01-18 22:35:02 -050034 await member.roles.add(client.verify[code]!.rID);
pineafane625d782022-05-09 18:04:32 +010035
TheCodedProf51296102023-01-18 22:35:02 -050036 const interaction = client.verify[code]!.interaction;
pineafane625d782022-05-09 18:04:32 +010037 if (interaction) {
Skyler Grey75ea9172022-08-06 10:22:23 +010038 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 });
pineafane625d782022-05-09 18:04:32 +010048 }
pineafan3a02ea32022-08-11 21:35:04 +010049 client.verify = client.verify.filter((v) => v.code !== code);
pineafan63fc5e22022-08-04 22:04:10 +010050 const { log, NucleusColors, entry, renderUser } = client.logger;
pineafan412beec2022-06-29 21:46:26 +010051 try {
pineafan63fc5e22022-08-04 22:04:10 +010052 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010053 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010054 type: "memberVerify",
55 displayName: "Member Verified",
56 calculateType: "guildMemberVerify",
pineafan412beec2022-06-29 21:46:26 +010057 color: NucleusColors.green,
58 emoji: "CONTROL.BLOCKTICK",
59 timestamp: new Date().getTime()
60 },
61 list: {
pineafanda6e5342022-07-03 10:03:16 +010062 memberId: entry(member.id, `\`${member.id}\``),
TheCodedProf51296102023-01-18 22:35:02 -050063 member: entry(member.id, renderUser(member.user))
pineafan412beec2022-06-29 21:46:26 +010064 },
65 hidden: {
66 guild: guild.id
67 }
pineafan63fc5e22022-08-04 22:04:10 +010068 };
pineafan412beec2022-06-29 21:46:26 +010069 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010070 } catch {
71 res.sendStatus(500);
72 }
pineafan6702cef2022-06-13 17:52:37 +010073 res.sendStatus(200);
pineafane625d782022-05-09 18:04:32 +010074 } else {
pineafan6702cef2022-06-13 17:52:37 +010075 res.sendStatus(403);
pineafane625d782022-05-09 18:04:32 +010076 }
77 });
78
pineafan63fc5e22022-08-04 22:04:10 +010079 app.get("/verify/:code", jsonParser, function (req, res) {
pineafane625d782022-05-09 18:04:32 +010080 const code = req.params.code;
81 if (client.verify[code]) {
pineafanda6e5342022-07-03 10:03:16 +010082 try {
TheCodedProf51296102023-01-18 22:35:02 -050083 const interaction = client.verify[code]!.interaction;
pineafanda6e5342022-07-03 10:03:16 +010084 if (interaction) {
Skyler Grey75ea9172022-08-06 10:22:23 +010085 interaction.editReply({
86 embeds: [
87 new EmojiEmbed()
88 .setTitle("Verify")
89 .setDescription(
90 "Verify was opened in another tab or window, please complete the CAPTCHA there to continue"
91 )
92 .setStatus("Success")
93 .setEmoji("MEMBER.JOIN")
94 ]
95 });
pineafanda6e5342022-07-03 10:03:16 +010096 }
Skyler Grey75ea9172022-08-06 10:22:23 +010097 } catch {
98 return res.sendStatus(410);
99 }
pineafan63fc5e22022-08-04 22:04:10 +0100100 const data = structuredClone(client.verify[code]);
pineafan6702cef2022-06-13 17:52:37 +0100101 delete data.interaction;
102 return res.status(200).send(data);
pineafane625d782022-05-09 18:04:32 +0100103 }
pineafan6702cef2022-06-13 17:52:37 +0100104 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100105 });
pineafane625d782022-05-09 18:04:32 +0100106
pineafan63fc5e22022-08-04 22:04:10 +0100107 app.post("/rolemenu/:code", jsonParser, async function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +0100108 const code = req.params.code;
109 const secret = req.body.secret;
pineafanda6e5342022-07-03 10:03:16 +0100110 if (secret === client.config.verifySecret) {
TheCodedProf51296102023-01-18 22:35:02 -0500111 const guild = await client.guilds.fetch(client.roleMenu[code]!.guild);
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 if (!guild) {
113 return res.status(404);
114 }
TheCodedProf51296102023-01-18 22:35:02 -0500115 const member = await guild.members.fetch(client.roleMenu[code]!.user);
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 if (!member) {
117 return res.status(404);
118 }
pineafanda6e5342022-07-03 10:03:16 +0100119 res.sendStatus(200);
120 } else {
121 res.sendStatus(403);
122 }
123 });
124
pineafan63fc5e22022-08-04 22:04:10 +0100125 app.get("/rolemenu/:code", jsonParser, function (req, res) {
pineafanda6e5342022-07-03 10:03:16 +0100126 const code = req.params.code;
127 if (client.roleMenu[code] !== undefined) {
128 try {
TheCodedProf51296102023-01-18 22:35:02 -0500129 const interaction = client.roleMenu[code]!.interaction;
pineafanda6e5342022-07-03 10:03:16 +0100130 if (interaction) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 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 });
pineafanda6e5342022-07-03 10:03:16 +0100143 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 } catch {
145 return res.sendStatus(410);
146 }
pineafan63fc5e22022-08-04 22:04:10 +0100147 const data = structuredClone(client.roleMenu[code]);
pineafanda6e5342022-07-03 10:03:16 +0100148 delete data.interaction;
pineafan63fc5e22022-08-04 22:04:10 +0100149 console.log(data);
pineafanda6e5342022-07-03 10:03:16 +0100150 return res.status(200).send(data);
151 }
152 return res.sendStatus(404);
pineafan63fc5e22022-08-04 22:04:10 +0100153 });
pineafanda6e5342022-07-03 10:03:16 +0100154
pineafane625d782022-05-09 18:04:32 +0100155 app.listen(port);
pineafan63fc5e22022-08-04 22:04:10 +0100156};
pineafane625d782022-05-09 18:04:32 +0100157
Skyler Grey75ea9172022-08-06 10:22:23 +0100158export default runServer;