pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../utils/defaultEmbeds.js"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 2 | import Discord, { CommandInteraction, GuildMember, Interaction, MessageComponentInteraction, Permissions, Role } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 4 | import fetch from "node-fetch"; |
| 5 | import { TestString, NSFWCheck } from "./scanners.js"; |
| 6 | import createPageIndicator from "../utils/createPageIndicator.js"; |
| 7 | import client from "../utils/client.js"; |
| 8 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 9 | export interface VerifySchema { |
| 10 | uID: string; |
| 11 | gID: string; |
| 12 | rID: string; |
| 13 | rName: string; |
| 14 | uName: string; |
| 15 | gName: string; |
| 16 | gIcon: string; |
| 17 | interaction: Interaction; |
| 18 | } |
| 19 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 20 | function step(i: number) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 21 | return "\n\n" + createPageIndicator(5, i); |
| 22 | } |
| 23 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 24 | export default async function (interaction: CommandInteraction | MessageComponentInteraction) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | const verify = client.verify; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | await interaction.reply({ |
| 27 | embeds: LoadingEmbed, |
| 28 | ephemeral: true, |
| 29 | fetchReply: true |
| 30 | }); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 31 | const config = await client.database.guilds.read(interaction.guild!.id); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 32 | if (!config.verify.enabled || !config.verify.role) |
| 33 | return interaction.editReply({ |
| 34 | embeds: [ |
| 35 | new EmojiEmbed() |
| 36 | .setTitle("Verify") |
| 37 | .setDescription("Verify is not enabled on this server") |
| 38 | .setFooter({ |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 39 | text: (interaction.member!.permissions as Permissions).has("MANAGE_GUILD") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 40 | ? "You can enable it by running /settings verify" |
| 41 | : "" |
| 42 | }) |
| 43 | .setStatus("Danger") |
| 44 | .setEmoji("CONTROL.BLOCKCROSS") |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 45 | ] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 46 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 47 | if ((interaction.member as GuildMember).roles.cache.has(config.verify.role)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | return await interaction.editReply({ |
| 49 | embeds: [ |
| 50 | new EmojiEmbed() |
| 51 | .setTitle("Verify") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 52 | .setDescription(`You already have the <@&${config.verify.role}> role` + step(0)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 53 | .setStatus("Danger") |
| 54 | .setEmoji("CONTROL.BLOCKCROSS") |
| 55 | ] |
| 56 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 57 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 58 | await interaction.editReply({ |
| 59 | embeds: [ |
| 60 | new EmojiEmbed() |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 61 | .setTitle("Verify") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | .setDescription("Checking our servers are up" + step(0)) |
| 63 | .setStatus("Warning") |
| 64 | .setEmoji("NUCLEUS.LOADING") |
| 65 | ] |
| 66 | }); |
| 67 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 68 | const status = await fetch(client.config.baseUrl).then((res) => res.status); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | if (status !== 200) { |
| 70 | return await interaction.editReply({ |
| 71 | embeds: [ |
| 72 | new EmojiEmbed() |
| 73 | .setTitle("Verify") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 74 | .setDescription("Our servers appear to be down, please try again later" + step(0)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 75 | .setStatus("Danger") |
| 76 | .setEmoji("CONTROL.BLOCKCROSS") |
| 77 | ] |
| 78 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 79 | } |
| 80 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 81 | return await interaction.editReply({ |
| 82 | embeds: [ |
| 83 | new EmojiEmbed() |
| 84 | .setTitle("Verify") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 85 | .setDescription("Our servers appear to be down, please try again later" + step(0)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 86 | .setStatus("Danger") |
| 87 | .setEmoji("CONTROL.BLOCKCROSS") |
| 88 | ], |
| 89 | components: [ |
| 90 | new Discord.MessageActionRow().addComponents([ |
| 91 | new Discord.MessageButton() |
| 92 | .setLabel("Check webpage") |
| 93 | .setStyle("LINK") |
| 94 | .setURL(client.config.baseUrl), |
| 95 | new Discord.MessageButton() |
| 96 | .setLabel("Support") |
| 97 | .setStyle("LINK") |
| 98 | .setURL("https://discord.gg/bPaNnxe") |
| 99 | ]) |
| 100 | ] |
| 101 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 102 | } |
| 103 | if (config.filters.images.NSFW) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 104 | await interaction.editReply({ |
| 105 | embeds: [ |
| 106 | new EmojiEmbed() |
| 107 | .setTitle("Verify") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 108 | .setDescription("Checking your avatar is safe for work" + step(1)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | .setStatus("Warning") |
| 110 | .setEmoji("NUCLEUS.LOADING") |
| 111 | ] |
| 112 | }); |
| 113 | if ( |
| 114 | await NSFWCheck( |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 115 | (interaction.member as GuildMember).user.displayAvatarURL({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 116 | format: "png" |
| 117 | }) |
| 118 | ) |
| 119 | ) { |
| 120 | return await interaction.editReply({ |
| 121 | embeds: [ |
| 122 | new EmojiEmbed() |
| 123 | .setTitle("Verify") |
| 124 | .setDescription( |
| 125 | "Your avatar was detected as NSFW, which we do not allow in this server.\nPlease contact one of our staff members if you believe this is a mistake" + |
| 126 | step(1) |
| 127 | ) |
| 128 | .setStatus("Danger") |
| 129 | .setEmoji("CONTROL.BLOCKCROSS") |
| 130 | ] |
| 131 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | if (config.filters.wordFilter) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 135 | await interaction.editReply({ |
| 136 | embeds: [ |
| 137 | new EmojiEmbed() |
| 138 | .setTitle("Verify") |
| 139 | .setDescription("Checking your name is allowed" + step(2)) |
| 140 | .setStatus("Warning") |
| 141 | .setEmoji("NUCLEUS.LOADING") |
| 142 | ] |
| 143 | }); |
| 144 | if ( |
| 145 | TestString( |
| 146 | (interaction.member as Discord.GuildMember).displayName, |
| 147 | config.filters.wordFilter.words.loose, |
| 148 | config.filters.wordFilter.words.strict |
| 149 | ) !== null |
| 150 | ) { |
| 151 | return await interaction.editReply({ |
| 152 | embeds: [ |
| 153 | new EmojiEmbed() |
| 154 | .setTitle("Verify") |
| 155 | .setDescription( |
| 156 | "Your name contained a word we do not allow in this server.\nPlease contact one of our staff members if you believe this is a mistake" + |
| 157 | step(2) |
| 158 | ) |
| 159 | .setStatus("Danger") |
| 160 | .setEmoji("CONTROL.BLOCKCROSS") |
| 161 | ] |
| 162 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 165 | await interaction.editReply({ |
| 166 | embeds: [ |
| 167 | new EmojiEmbed() |
| 168 | .setTitle("Verify") |
| 169 | .setDescription("One moment..." + step(3)) |
| 170 | .setStatus("Warning") |
| 171 | .setEmoji("NUCLEUS.LOADING") |
| 172 | ] |
| 173 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 174 | let code = ""; |
| 175 | let length = 5; |
| 176 | let itt = 0; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 177 | const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 178 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 179 | itt += 1; |
| 180 | code = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 181 | for (let i = 0; i < length; i++) { |
| 182 | code += chars.charAt(Math.floor(Math.random() * chars.length)); |
| 183 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 184 | if (code in verify) continue; |
| 185 | if (itt > 1000) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 186 | itt = 0; |
| 187 | length += 1; |
| 188 | continue; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 189 | } |
| 190 | break; |
| 191 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 192 | const role: Role | null = await interaction.guild!.roles.fetch(config.verify.role); |
| 193 | if (!role) { |
| 194 | await interaction.editReply({ |
| 195 | embeds: [ |
| 196 | new EmojiEmbed() |
| 197 | .setTitle("Verify") |
| 198 | .setDescription( |
| 199 | "The server's verify role was deleted, or could not be found. Please contact a staff member to fix this issue." + |
| 200 | step(4) |
| 201 | ) |
| 202 | .setStatus("Danger") |
| 203 | .setEmoji("CONTROL.BLOCKCROSS") |
| 204 | ] |
| 205 | }); |
| 206 | return; // TODO: SEN |
| 207 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 208 | verify[code] = { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 209 | uID: interaction.member!.user.id, |
| 210 | gID: interaction.guild!.id, |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 211 | rID: config.verify.role, |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 212 | rName: role.name, |
| 213 | uName: interaction.member!.user.username, |
| 214 | gName: interaction.guild!.name, |
| 215 | gIcon: interaction.guild!.iconURL({ format: "png" }), |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 216 | interaction: interaction |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 217 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 218 | await interaction.editReply({ |
| 219 | embeds: [ |
| 220 | new EmojiEmbed() |
| 221 | .setTitle("Verify") |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 222 | .setDescription("Looking good!\nClick the button below to get verified." + step(4)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 223 | .setStatus("Success") |
| 224 | .setEmoji("MEMBER.JOIN") |
| 225 | ], |
| 226 | components: [ |
| 227 | new Discord.MessageActionRow().addComponents([ |
| 228 | new Discord.MessageButton() |
| 229 | .setLabel("Verify") |
| 230 | .setStyle("LINK") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 231 | .setURL(`${client.config.baseUrl}nucleus/verify?code=${code}`) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 232 | ]) |
| 233 | ] |
| 234 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 235 | } |