pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 1 | import { HaikuClient } from 'jshaiku'; |
| 2 | import express from 'express'; |
| 3 | import bodyParser from 'body-parser'; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 5 | import structuredClone from '@ungap/structured-clone'; |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 6 | import client from '../utils/client.js'; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 7 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 8 | |
| 9 | const jsonParser = bodyParser.json(); |
| 10 | const app = express(); |
| 11 | const port = 10000; |
| 12 | |
| 13 | const runServer = (client: HaikuClient) => { |
| 14 | app.get('/', (req, res) => { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 15 | res.status(200).send(client.ws.ping); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 16 | }); |
| 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) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 31 | interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 32 | .setTitle("Verify") |
| 33 | .setDescription(`Verification complete`) |
| 34 | .setStatus("Success") |
| 35 | .setEmoji("MEMBER.JOIN") |
| 36 | ], components: []}); |
| 37 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 38 | delete client.verify[code]; |
| 39 | const { log, NucleusColors, entry, renderUser } = client.logger |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 40 | 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: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 51 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | 412beec | 2022-06-29 21:46:26 +0100 | [diff] [blame] | 52 | member: entry(member.id, renderUser(member)) |
| 53 | }, |
| 54 | hidden: { |
| 55 | guild: guild.id |
| 56 | } |
| 57 | } |
| 58 | log(data); |
| 59 | } catch {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 60 | res.sendStatus(200); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 61 | } else { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 62 | res.sendStatus(403); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 63 | } |
| 64 | }); |
| 65 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 66 | app.get('/verify/:code', jsonParser, function (req, res) { |
| 67 | const code = req.params.code; |
| 68 | if (client.verify[code]) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 69 | 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 {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 80 | let data = structuredClone(client.verify[code]) |
| 81 | delete data.interaction; |
| 82 | return res.status(200).send(data); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 83 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 84 | return res.sendStatus(404); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 85 | }) |
| 86 | |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 87 | 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) { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 92 | let guild = await client.guilds.fetch(client.roleMenu[code].guild); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 93 | 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 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 124 | app.listen(port); |
| 125 | } |
| 126 | |
| 127 | export default runServer; |