blob: a77f2f432968832a7540776a55bcf6e14fb12275 [file] [log] [blame]
pineafane23c4ec2022-07-27 21:56:27 +01001import { LoadingEmbed } from './../../utils/defaultEmbeds.js';
pineafan41d93562022-07-30 22:10:15 +01002import Discord, { CommandInteraction, Emoji, MessageActionRow, MessageButton, MessageSelectMenu, TextInputComponent } from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +01003import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
4import confirmationMessage from "../../utils/confirmationMessage.js";
5import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +00006import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
7import { WrappedCheck } from "jshaiku";
pineafanda6e5342022-07-03 10:03:16 +01008import client from "../../utils/client.js";
pineafan41d93562022-07-30 22:10:15 +01009import { modalInteractionCollector } from '../../utils/dualCollector.js';
pineafan4f164f32022-02-26 22:07:12 +000010
11const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafanda6e5342022-07-03 10:03:16 +010013 .setName("verify")
14 .setDescription("Manage the role given after typing /verify")
15 .addRoleOption(option => option.setName("role").setDescription("The role to give after verifying").setRequired(false))
pineafan4f164f32022-02-26 22:07:12 +000016
pineafan6702cef2022-06-13 17:52:37 +010017const callback = async (interaction: CommandInteraction): Promise<any> => {
18 let m;
pineafane23c4ec2022-07-27 21:56:27 +010019 m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true});
pineafan6702cef2022-06-13 17:52:37 +010020 if (interaction.options.getRole("role")) {
21 let role
22 try {
23 role = interaction.options.getRole("role")
24 } catch {
pineafan4edb7762022-06-26 19:21:04 +010025 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010026 .setEmoji("GUILD.ROLES.DELETE")
27 .setTitle("Verify Role")
28 .setDescription("The role you provided is not a valid role")
29 .setStatus("Danger")
30 ]})
31 }
32 role = role as Discord.Role
pineafane23c4ec2022-07-27 21:56:27 +010033 if (role.guild.id !== interaction.guild.id) {
pineafan4edb7762022-06-26 19:21:04 +010034 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010035 .setTitle("Verify Role")
36 .setDescription(`You must choose a role in this server`)
37 .setStatus("Danger")
38 .setEmoji("GUILD.ROLES.DELETE")
39 ]});
40 }
41 let confirmation = await new confirmationMessage(interaction)
42 .setEmoji("GUILD.ROLES.EDIT")
43 .setTitle("Verify Role")
44 .setDescription(`Are you sure you want to set the verify role to <@&${role.id}>?`)
45 .setColor("Warning")
46 .setInverted(true)
47 .send(true)
pineafan02ba0232022-07-24 22:16:15 +010048 if (confirmation.cancelled) return
pineafan6702cef2022-06-13 17:52:37 +010049 if (confirmation.success) {
50 try {
pineafan4edb7762022-06-26 19:21:04 +010051 await client.database.guilds.write(interaction.guild.id, {"verify.role": role.id, "verify.enabled": true});
pineafanda6e5342022-07-03 10:03:16 +010052 const { log, NucleusColors, entry, renderUser, renderRole } = client.logger
53 try {
54 let data = {
55 meta:{
56 type: 'verifyRoleChanged',
pineafan41d93562022-07-30 22:10:15 +010057 displayName: 'Verify Role Changed',
pineafanda6e5342022-07-03 10:03:16 +010058 calculateType: 'nucleusSettingsUpdated',
59 color: NucleusColors.green,
60 emoji: "CONTROL.BLOCKTICK",
61 timestamp: new Date().getTime()
62 },
63 list: {
64 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
65 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
66 role: entry(role.id, renderRole(role)),
67 },
68 hidden: {
69 guild: interaction.guild.id
70 }
71 }
72 log(data);
73 } catch {}
pineafan6702cef2022-06-13 17:52:37 +010074 } catch (e) {
75 console.log(e)
pineafan4edb7762022-06-26 19:21:04 +010076 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010077 .setTitle("Verify Role")
78 .setDescription(`Something went wrong while setting the verify role`)
79 .setStatus("Danger")
80 .setEmoji("GUILD.ROLES.DELETE")
81 ], components: []});
82 }
83 } else {
pineafan4edb7762022-06-26 19:21:04 +010084 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010085 .setTitle("Verify Role")
86 .setDescription(`No changes were made`)
87 .setStatus("Success")
88 .setEmoji("GUILD.ROLES.CREATE")
89 ], components: []});
90 }
91 }
92 let clicks = 0;
pineafan4edb7762022-06-26 19:21:04 +010093 let data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +010094 let role = data.verify.role;
95 while (true) {
pineafan4edb7762022-06-26 19:21:04 +010096 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010097 .setTitle("Verify Role")
98 .setDescription(role ? `Your verify role is currently set to <@&${role}>` : `You have not set a verify role`)
99 .setStatus("Success")
100 .setEmoji("GUILD.ROLES.CREATE")
pineafane23c4ec2022-07-27 21:56:27 +0100101 ], components: [new MessageActionRow().addComponents([
102 new MessageButton()
103 .setCustomId("clear")
104 .setLabel(clicks ? "Click again to confirm" : "Reset role")
105 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
106 .setStyle("DANGER")
pineafan41d93562022-07-30 22:10:15 +0100107 .setDisabled(!role),
108 new MessageButton()
109 .setCustomId("send")
110 .setLabel("Add verify button")
111 .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id"))
112 .setStyle("PRIMARY")
pineafan6702cef2022-06-13 17:52:37 +0100113 ])]});
114 let i;
115 try {
pineafanc6158ab2022-06-17 16:34:07 +0100116 i = await m.awaitMessageComponent({time: 300000});
pineafan6702cef2022-06-13 17:52:37 +0100117 } catch(e) { break }
118 i.deferUpdate()
pineafane23c4ec2022-07-27 21:56:27 +0100119 if (i.component.customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100120 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100121 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100122 clicks = 0;
pineafane23c4ec2022-07-27 21:56:27 +0100123 await client.database.guilds.write(interaction.guild.id, null, ["verify.role", "verify.enabled"])
pineafan6702cef2022-06-13 17:52:37 +0100124 role = undefined;
125 }
pineafan41d93562022-07-30 22:10:15 +0100126 } else if (i.component.customId === "send") {
127 const verifyMessages = [
128 {label: "Verify", description: "Click the button below to get verified"},
129 {label: "Get verified", description: "To get access to the rest of the server, click the button below"},
130 {label: "Ready to verify?", description: "Click the button below to verify yourself"},
131 ]
132 while (true) {
133 await interaction.editReply({embeds: [new EmojiEmbed()
134 .setTitle("Verify Button")
135 .setDescription("Select a message template to send in this channel")
136 .setFooter({text: role ? "" : "You do no have a verify role set so the button will not work."})
137 .setStatus(role ? "Success" : "Warning")
138 .setEmoji("GUILD.ROLES.CREATE")
139 ], components: [
140 new MessageActionRow().addComponents([
141 new MessageSelectMenu().setOptions(verifyMessages.map((t: {label: string, description: string, value?: string}, index) => {
142 t.value = index.toString(); return t as {value: string, label: string, description: string}
143 })).setCustomId("template").setMaxValues(1).setMinValues(1).setPlaceholder("Select a message template"),
144 ]),
145 new MessageActionRow().addComponents([
146 new MessageButton()
147 .setCustomId("back")
148 .setLabel("Back")
149 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
150 .setStyle("DANGER"),
151 new MessageButton()
152 .setCustomId("blank")
153 .setLabel("Empty")
154 .setStyle("SECONDARY"),
155 new MessageButton()
156 .setCustomId("custom")
157 .setLabel("Custom")
158 .setEmoji(getEmojiByName("TICKETS.OTHER", "id"))
159 .setStyle("PRIMARY")
160 ])
161 ]});
162 let i;
163 try {
164 i = await m.awaitMessageComponent({time: 300000});
165 } catch(e) { break }
166 if (i.component.customId === "template") {
167 i.deferUpdate()
168 await interaction.channel.send({embeds: [new EmojiEmbed()
169 .setTitle(verifyMessages[parseInt(i.values[0])].label)
170 .setDescription(verifyMessages[parseInt(i.values[0])].description)
171 .setStatus("Success")
172 .setEmoji("CONTROL.BLOCKTICK")
173 ], components: [new MessageActionRow().addComponents([new MessageButton()
174 .setLabel("Verify")
175 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
176 .setStyle("SUCCESS")
177 .setCustomId("verifybutton")
178 ])]});
179 break
180 } else if (i.component.customId === "blank") {
181 i.deferUpdate()
182 await interaction.channel.send({components: [new MessageActionRow().addComponents([new MessageButton()
183 .setLabel("Verify")
184 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
185 .setStyle("SUCCESS")
186 .setCustomId("verifybutton")
187 ])]});
188 break
189 } else if (i.component.customId === "custom") {
190 await i.showModal(new Discord.Modal().setCustomId("modal").setTitle(`Enter embed details`).addComponents(
191 new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent()
192 .setCustomId("title")
193 .setLabel("Title")
194 .setMaxLength(256)
195 .setRequired(true)
196 .setStyle("SHORT")
197 ),
198 new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent()
199 .setCustomId("description")
200 .setLabel("Description")
201 .setMaxLength(4000)
202 .setRequired(true)
203 .setStyle("PARAGRAPH")
204 )
205 ))
206 await interaction.editReply({
207 embeds: [new EmojiEmbed()
208 .setTitle("Verify Button")
209 .setDescription("Modal opened. If you can't see it, click back and try again.")
210 .setStatus("Success")
211 .setEmoji("GUILD.TICKET.OPEN")
212 ], components: [new MessageActionRow().addComponents([new MessageButton()
213 .setLabel("Back")
214 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
215 .setStyle("PRIMARY")
216 .setCustomId("back")
217 ])]
218 });
219 let out;
220 try {
221 out = await modalInteractionCollector(m, (m) => m.channel.id === interaction.channel.id, (m) => m.customId === "modify")
222 } catch (e) { break }
223 if (out.fields) {
224 let title = out.fields.getTextInputValue("title");
225 let description = out.fields.getTextInputValue("description");
226 await interaction.channel.send({embeds: [new EmojiEmbed()
227 .setTitle(title)
228 .setDescription(description)
229 .setStatus("Success")
230 .setEmoji("CONTROL.BLOCKTICK")
231 ], components: [new MessageActionRow().addComponents([new MessageButton()
232 .setLabel("Verify")
233 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
234 .setStyle("SUCCESS")
235 .setCustomId("verifybutton")
236 ])]});
237 break
238 } else { continue }
239 }
240 }
pineafan6702cef2022-06-13 17:52:37 +0100241 } else {
pineafan41d93562022-07-30 22:10:15 +0100242 i.deferUpdate()
243 break;
pineafan6702cef2022-06-13 17:52:37 +0100244 }
245 }
pineafan41d93562022-07-30 22:10:15 +0100246 await interaction.editReply({embeds: [m.embeds[0].setFooter({text: "Message closed"})], components: []});
pineafan4f164f32022-02-26 22:07:12 +0000247}
248
249const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan6702cef2022-06-13 17:52:37 +0100250 let member = (interaction.member as Discord.GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +0100251 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command"
pineafan4f164f32022-02-26 22:07:12 +0000252 return true;
253}
254
255export { command };
256export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100257export { check };