blob: 614ecb9ea07e606bbc114429331fe3d06ad10cd0 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01002import Discord, {
3 CommandInteraction,
4 Message,
5 MessageActionRow,
6 MessageActionRowComponent,
7 MessageButton,
8 MessageComponentInteraction,
9 MessageEmbed,
10 MessageSelectMenu,
11 Role,
12 SelectMenuInteraction,
13 TextInputComponent
14} from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +010015import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
16import confirmationMessage from "../../utils/confirmationMessage.js";
17import getEmojiByName from "../../utils/getEmojiByName.js";
pineafanbd02b4a2022-08-05 22:01:38 +010018import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafanda6e5342022-07-03 10:03:16 +010019import client from "../../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +010020import { modalInteractionCollector } from "../../utils/dualCollector.js";
pineafan4f164f32022-02-26 22:07:12 +000021
22const command = (builder: SlashCommandSubcommandBuilder) =>
23 builder
pineafan63fc5e22022-08-04 22:04:10 +010024 .setName("verify")
25 .setDescription("Manage the role given after typing /verify")
Skyler Grey75ea9172022-08-06 10:22:23 +010026 .addRoleOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010027 option.setName("role").setDescription("The role to give after verifying").setRequired(false)
Skyler Grey75ea9172022-08-06 10:22:23 +010028 );
pineafan4f164f32022-02-26 22:07:12 +000029
Skyler Grey11236ba2022-08-08 21:13:33 +010030const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
Skyler Grey75ea9172022-08-06 10:22:23 +010031 const m = (await interaction.reply({
32 embeds: LoadingEmbed,
33 ephemeral: true,
34 fetchReply: true
35 })) as Message;
pineafan6702cef2022-06-13 17:52:37 +010036 if (interaction.options.getRole("role")) {
Skyler Grey1a67e182022-08-04 23:05:44 +010037 let role: Role;
pineafan6702cef2022-06-13 17:52:37 +010038 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010039 role = interaction.options.getRole("role") as Role;
pineafan6702cef2022-06-13 17:52:37 +010040 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010041 return await interaction.editReply({
42 embeds: [
43 new EmojiEmbed()
44 .setEmoji("GUILD.ROLES.DELETE")
45 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010046 .setDescription("The role you provided is not a valid role")
Skyler Grey75ea9172022-08-06 10:22:23 +010047 .setStatus("Danger")
48 ]
49 });
pineafan6702cef2022-06-13 17:52:37 +010050 }
pineafan63fc5e22022-08-04 22:04:10 +010051 role = role as Discord.Role;
pineafane23c4ec2022-07-27 21:56:27 +010052 if (role.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010053 return interaction.editReply({
54 embeds: [
55 new EmojiEmbed()
56 .setTitle("Verify Role")
57 .setDescription("You must choose a role in this server")
58 .setStatus("Danger")
59 .setEmoji("GUILD.ROLES.DELETE")
60 ]
61 });
pineafan6702cef2022-06-13 17:52:37 +010062 }
pineafan63fc5e22022-08-04 22:04:10 +010063 const confirmation = await new confirmationMessage(interaction)
pineafan6702cef2022-06-13 17:52:37 +010064 .setEmoji("GUILD.ROLES.EDIT")
65 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010066 .setDescription(`Are you sure you want to set the verify role to <@&${role.id}>?`)
pineafan6702cef2022-06-13 17:52:37 +010067 .setColor("Warning")
68 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010069 .send(true);
70 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010071 if (confirmation.success) {
72 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010073 await client.database.guilds.write(interaction.guild.id, {
74 "verify.role": role.id,
75 "verify.enabled": true
76 });
Skyler Grey11236ba2022-08-08 21:13:33 +010077 const { log, NucleusColors, entry, renderUser, renderRole } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010078 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010079 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010080 type: "verifyRoleChanged",
81 displayName: "Verify Role Changed",
82 calculateType: "nucleusSettingsUpdated",
83 color: NucleusColors.green,
84 emoji: "CONTROL.BLOCKTICK",
85 timestamp: new Date().getTime()
86 },
87 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +010088 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
89 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010090 role: entry(role.id, renderRole(role))
91 },
92 hidden: {
93 guild: interaction.guild.id
pineafanda6e5342022-07-03 10:03:16 +010094 }
pineafan63fc5e22022-08-04 22:04:10 +010095 };
96 log(data);
pineafan6702cef2022-06-13 17:52:37 +010097 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010098 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +010099 return interaction.editReply({
100 embeds: [
101 new EmojiEmbed()
102 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +0100103 .setDescription("Something went wrong while setting the verify role")
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 .setStatus("Danger")
105 .setEmoji("GUILD.ROLES.DELETE")
106 ],
107 components: []
108 });
pineafan6702cef2022-06-13 17:52:37 +0100109 }
110 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 return interaction.editReply({
112 embeds: [
113 new EmojiEmbed()
114 .setTitle("Verify Role")
115 .setDescription("No changes were made")
116 .setStatus("Success")
117 .setEmoji("GUILD.ROLES.CREATE")
118 ],
119 components: []
120 });
pineafan6702cef2022-06-13 17:52:37 +0100121 }
122 }
123 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100124 const data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +0100125 let role = data.verify.role;
126 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100127 await interaction.editReply({
128 embeds: [
129 new EmojiEmbed()
130 .setTitle("Verify Role")
131 .setDescription(
Skyler Grey11236ba2022-08-08 21:13:33 +0100132 role ? `Your verify role is currently set to <@&${role}>` : "You have not set a verify role"
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 )
134 .setStatus("Success")
135 .setEmoji("GUILD.ROLES.CREATE")
136 ],
137 components: [
138 new MessageActionRow().addComponents([
139 new MessageButton()
140 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100141 .setLabel(clicks ? "Click again to confirm" : "Reset role")
142 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 .setStyle("DANGER")
144 .setDisabled(!role),
145 new MessageButton()
146 .setCustomId("send")
147 .setLabel("Add verify button")
148 .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id"))
149 .setStyle("PRIMARY")
150 ])
151 ]
152 });
Skyler Grey1a67e182022-08-04 23:05:44 +0100153 let i: MessageComponentInteraction;
pineafan6702cef2022-06-13 17:52:37 +0100154 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100155 i = await m.awaitMessageComponent({ time: 300000 });
156 } catch (e) {
157 break;
158 }
pineafan63fc5e22022-08-04 22:04:10 +0100159 i.deferUpdate();
Skyler Grey1a67e182022-08-04 23:05:44 +0100160 if ((i.component as MessageActionRowComponent).customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100161 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100162 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100163 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100164 await client.database.guilds.write(interaction.guild.id, null, ["verify.role", "verify.enabled"]);
pineafan6702cef2022-06-13 17:52:37 +0100165 role = undefined;
166 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100167 } else if ((i.component as MessageActionRowComponent).customId === "send") {
pineafan41d93562022-07-30 22:10:15 +0100168 const verifyMessages = [
Skyler Grey75ea9172022-08-06 10:22:23 +0100169 {
170 label: "Verify",
171 description: "Click the button below to get verified"
172 },
173 {
174 label: "Get verified",
Skyler Grey11236ba2022-08-08 21:13:33 +0100175 description: "To get access to the rest of the server, click the button below"
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 },
177 {
178 label: "Ready to verify?",
179 description: "Click the button below to verify yourself"
180 }
pineafan63fc5e22022-08-04 22:04:10 +0100181 ];
pineafan41d93562022-07-30 22:10:15 +0100182 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100183 await interaction.editReply({
184 embeds: [
185 new EmojiEmbed()
186 .setTitle("Verify Button")
Skyler Grey11236ba2022-08-08 21:13:33 +0100187 .setDescription("Select a message template to send in this channel")
Skyler Grey75ea9172022-08-06 10:22:23 +0100188 .setFooter({
Skyler Grey11236ba2022-08-08 21:13:33 +0100189 text: role ? "" : "You do no have a verify role set so the button will not work."
Skyler Grey75ea9172022-08-06 10:22:23 +0100190 })
191 .setStatus(role ? "Success" : "Warning")
192 .setEmoji("GUILD.ROLES.CREATE")
193 ],
194 components: [
195 new MessageActionRow().addComponents([
196 new MessageSelectMenu()
197 .setOptions(
198 verifyMessages.map(
199 (
200 t: {
201 label: string;
202 description: string;
203 value?: string;
204 },
205 index
206 ) => {
207 t.value = index.toString();
208 return t as {
209 value: string;
210 label: string;
211 description: string;
212 };
213 }
214 )
215 )
216 .setCustomId("template")
217 .setMaxValues(1)
218 .setMinValues(1)
219 .setPlaceholder("Select a message template")
220 ]),
221 new MessageActionRow().addComponents([
222 new MessageButton()
223 .setCustomId("back")
224 .setLabel("Back")
225 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
226 .setStyle("DANGER"),
Skyler Grey11236ba2022-08-08 21:13:33 +0100227 new MessageButton().setCustomId("blank").setLabel("Empty").setStyle("SECONDARY"),
Skyler Grey75ea9172022-08-06 10:22:23 +0100228 new MessageButton()
229 .setCustomId("custom")
230 .setLabel("Custom")
231 .setEmoji(getEmojiByName("TICKETS.OTHER", "id"))
232 .setStyle("PRIMARY")
233 ])
234 ]
235 });
Skyler Grey1a67e182022-08-04 23:05:44 +0100236 let i: MessageComponentInteraction;
pineafan41d93562022-07-30 22:10:15 +0100237 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100238 i = await m.awaitMessageComponent({ time: 300000 });
239 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100240 break;
Skyler Grey75ea9172022-08-06 10:22:23 +0100241 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100242 if ((i.component as MessageActionRowComponent).customId === "template") {
pineafan63fc5e22022-08-04 22:04:10 +0100243 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100244 await interaction.channel.send({
245 embeds: [
246 new EmojiEmbed()
Skyler Grey11236ba2022-08-08 21:13:33 +0100247 .setTitle(verifyMessages[parseInt((i as SelectMenuInteraction).values[0])].label)
Skyler Grey75ea9172022-08-06 10:22:23 +0100248 .setDescription(
Skyler Grey11236ba2022-08-08 21:13:33 +0100249 verifyMessages[parseInt((i as SelectMenuInteraction).values[0])].description
Skyler Grey75ea9172022-08-06 10:22:23 +0100250 )
251 .setStatus("Success")
252 .setEmoji("CONTROL.BLOCKTICK")
253 ],
254 components: [
255 new MessageActionRow().addComponents([
256 new MessageButton()
257 .setLabel("Verify")
Skyler Grey11236ba2022-08-08 21:13:33 +0100258 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100259 .setStyle("SUCCESS")
260 .setCustomId("verifybutton")
261 ])
262 ]
263 });
pineafan63fc5e22022-08-04 22:04:10 +0100264 break;
Skyler Grey11236ba2022-08-08 21:13:33 +0100265 } else if ((i.component as MessageActionRowComponent).customId === "blank") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100266 i.deferUpdate();
267 await interaction.channel.send({
268 components: [
269 new MessageActionRow().addComponents([
270 new MessageButton()
271 .setLabel("Verify")
Skyler Grey11236ba2022-08-08 21:13:33 +0100272 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100273 .setStyle("SUCCESS")
274 .setCustomId("verifybutton")
275 ])
276 ]
277 });
278 break;
Skyler Grey11236ba2022-08-08 21:13:33 +0100279 } else if ((i.component as MessageActionRowComponent).customId === "custom") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100280 await i.showModal(
281 new Discord.Modal()
282 .setCustomId("modal")
283 .setTitle("Enter embed details")
284 .addComponents(
285 new MessageActionRow<TextInputComponent>().addComponents(
286 new TextInputComponent()
287 .setCustomId("title")
288 .setLabel("Title")
289 .setMaxLength(256)
290 .setRequired(true)
291 .setStyle("SHORT")
292 ),
293 new MessageActionRow<TextInputComponent>().addComponents(
294 new TextInputComponent()
295 .setCustomId("description")
296 .setLabel("Description")
297 .setMaxLength(4000)
298 .setRequired(true)
299 .setStyle("PARAGRAPH")
300 )
301 )
302 );
pineafan41d93562022-07-30 22:10:15 +0100303 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100304 embeds: [
305 new EmojiEmbed()
306 .setTitle("Verify Button")
Skyler Grey11236ba2022-08-08 21:13:33 +0100307 .setDescription("Modal opened. If you can't see it, click back and try again.")
Skyler Grey75ea9172022-08-06 10:22:23 +0100308 .setStatus("Success")
309 .setEmoji("GUILD.TICKET.OPEN")
310 ],
311 components: [
312 new MessageActionRow().addComponents([
313 new MessageButton()
314 .setLabel("Back")
Skyler Grey11236ba2022-08-08 21:13:33 +0100315 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100316 .setStyle("PRIMARY")
317 .setCustomId("back")
318 ])
319 ]
pineafan41d93562022-07-30 22:10:15 +0100320 });
321 let out;
322 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100323 out = await modalInteractionCollector(
324 m,
325 (m) => m.channel.id === interaction.channel.id,
326 (m) => m.customId === "modify"
327 );
328 } catch (e) {
329 break;
330 }
pineafan41d93562022-07-30 22:10:15 +0100331 if (out.fields) {
pineafan63fc5e22022-08-04 22:04:10 +0100332 const title = out.fields.getTextInputValue("title");
Skyler Grey11236ba2022-08-08 21:13:33 +0100333 const description = out.fields.getTextInputValue("description");
Skyler Grey75ea9172022-08-06 10:22:23 +0100334 await interaction.channel.send({
335 embeds: [
336 new EmojiEmbed()
337 .setTitle(title)
338 .setDescription(description)
339 .setStatus("Success")
340 .setEmoji("CONTROL.BLOCKTICK")
341 ],
342 components: [
343 new MessageActionRow().addComponents([
344 new MessageButton()
345 .setLabel("Verify")
Skyler Grey11236ba2022-08-08 21:13:33 +0100346 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100347 .setStyle("SUCCESS")
348 .setCustomId("verifybutton")
349 ])
350 ]
351 });
pineafan63fc5e22022-08-04 22:04:10 +0100352 break;
Skyler Grey75ea9172022-08-06 10:22:23 +0100353 } else {
354 continue;
355 }
pineafan41d93562022-07-30 22:10:15 +0100356 }
357 }
pineafan6702cef2022-06-13 17:52:37 +0100358 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100359 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100360 break;
pineafan6702cef2022-06-13 17:52:37 +0100361 }
362 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100363 await interaction.editReply({
364 embeds: [m.embeds[0]!.setFooter({ text: "Message closed" })],
365 components: []
366 });
pineafan63fc5e22022-08-04 22:04:10 +0100367};
pineafan4f164f32022-02-26 22:07:12 +0000368
Skyler Grey1a67e182022-08-04 23:05:44 +0100369const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100370 const member = interaction.member as Discord.GuildMember;
371 if (!member.permissions.has("MANAGE_GUILD"))
372 throw "You must have the *Manage Server* permission to use this command";
pineafan4f164f32022-02-26 22:07:12 +0000373 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100374};
pineafan4f164f32022-02-26 22:07:12 +0000375
376export { command };
377export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100378export { check };