blob: 1d695f831dee5b1c87f8af60c4d52e38ef008ad7 [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LoadingEmbed } from "../../utils/defaults.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01002import Discord, {
3 CommandInteraction,
pineafan3a02ea32022-08-11 21:35:04 +01004 Interaction,
Skyler Grey75ea9172022-08-06 10:22:23 +01005 Message,
TheCodedProf21c08592022-09-13 14:14:43 -04006 ActionRowBuilder,
TheCodedProf21c08592022-09-13 14:14:43 -04007 ButtonBuilder,
Skyler Grey75ea9172022-08-06 10:22:23 +01008 MessageComponentInteraction,
pineafan3a02ea32022-08-11 21:35:04 +01009 ModalSubmitInteraction,
Skyler Grey75ea9172022-08-06 10:22:23 +010010 Role,
TheCodedProf59772f82023-01-18 22:17:16 -050011 ButtonStyle,
12 StringSelectMenuBuilder,
TheCodedProf59772f82023-01-18 22:17:16 -050013 TextInputBuilder,
14 EmbedBuilder,
TheCodedProf59772f82023-01-18 22:17:16 -050015 ButtonComponent
Skyler Grey75ea9172022-08-06 10:22:23 +010016} from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +010017import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
18import confirmationMessage from "../../utils/confirmationMessage.js";
19import getEmojiByName from "../../utils/getEmojiByName.js";
TheCodedProff86ba092023-01-27 17:10:07 -050020import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +010021import client from "../../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +010022import { modalInteractionCollector } from "../../utils/dualCollector.js";
pineafan4f164f32022-02-26 22:07:12 +000023
24const command = (builder: SlashCommandSubcommandBuilder) =>
25 builder
pineafan63fc5e22022-08-04 22:04:10 +010026 .setName("verify")
27 .setDescription("Manage the role given after typing /verify")
Skyler Grey75ea9172022-08-06 10:22:23 +010028 .addRoleOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010029 option.setName("role").setDescription("The role to give after verifying").setRequired(false)
Skyler Grey75ea9172022-08-06 10:22:23 +010030 );
pineafan4f164f32022-02-26 22:07:12 +000031
pineafan3a02ea32022-08-11 21:35:04 +010032const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000033 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010034 const m = (await interaction.reply({
35 embeds: LoadingEmbed,
36 ephemeral: true,
37 fetchReply: true
38 })) as Message;
TheCodedProf59772f82023-01-18 22:17:16 -050039 if (interaction.options.get("role")?.role) {
Skyler Grey1a67e182022-08-04 23:05:44 +010040 let role: Role;
pineafan6702cef2022-06-13 17:52:37 +010041 try {
TheCodedProf59772f82023-01-18 22:17:16 -050042 role = interaction.options.get("role")?.role as Role;
pineafan6702cef2022-06-13 17:52:37 +010043 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010044 return await interaction.editReply({
45 embeds: [
46 new EmojiEmbed()
47 .setEmoji("GUILD.ROLES.DELETE")
48 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010049 .setDescription("The role you provided is not a valid role")
Skyler Grey75ea9172022-08-06 10:22:23 +010050 .setStatus("Danger")
51 ]
52 });
pineafan6702cef2022-06-13 17:52:37 +010053 }
pineafan63fc5e22022-08-04 22:04:10 +010054 role = role as Discord.Role;
PineaFana00db1b2023-01-02 15:32:54 +000055 if (role.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010056 return interaction.editReply({
57 embeds: [
58 new EmojiEmbed()
59 .setTitle("Verify Role")
60 .setDescription("You must choose a role in this server")
61 .setStatus("Danger")
62 .setEmoji("GUILD.ROLES.DELETE")
63 ]
64 });
pineafan6702cef2022-06-13 17:52:37 +010065 }
pineafan63fc5e22022-08-04 22:04:10 +010066 const confirmation = await new confirmationMessage(interaction)
TheCodedProf59772f82023-01-18 22:17:16 -050067 .setEmoji("GUILD.ROLES.EDIT")
pineafan6702cef2022-06-13 17:52:37 +010068 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010069 .setDescription(`Are you sure you want to set the verify role to <@&${role.id}>?`)
pineafan6702cef2022-06-13 17:52:37 +010070 .setColor("Warning")
TheCodedProf59772f82023-01-18 22:17:16 -050071 .setFailedMessage("No changes were made", "Warning", "GUILD.ROLES.DELETE")
pineafan6702cef2022-06-13 17:52:37 +010072 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010073 .send(true);
74 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010075 if (confirmation.success) {
76 try {
PineaFana00db1b2023-01-02 15:32:54 +000077 await client.database.guilds.write(interaction.guild.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 "verify.role": role.id,
79 "verify.enabled": true
80 });
Skyler Grey11236ba2022-08-08 21:13:33 +010081 const { log, NucleusColors, entry, renderUser, renderRole } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010082 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010083 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010084 type: "verifyRoleChanged",
85 displayName: "Verify Role Changed",
86 calculateType: "nucleusSettingsUpdated",
87 color: NucleusColors.green,
88 emoji: "CONTROL.BLOCKTICK",
89 timestamp: new Date().getTime()
90 },
91 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +010092 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
93 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010094 role: entry(role.id, renderRole(role))
95 },
96 hidden: {
PineaFana00db1b2023-01-02 15:32:54 +000097 guild: interaction.guild.id
pineafanda6e5342022-07-03 10:03:16 +010098 }
pineafan63fc5e22022-08-04 22:04:10 +010099 };
100 log(data);
pineafan6702cef2022-06-13 17:52:37 +0100101 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100102 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 return interaction.editReply({
104 embeds: [
105 new EmojiEmbed()
106 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +0100107 .setDescription("Something went wrong while setting the verify role")
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 .setStatus("Danger")
109 .setEmoji("GUILD.ROLES.DELETE")
110 ],
111 components: []
112 });
pineafan6702cef2022-06-13 17:52:37 +0100113 }
114 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 return interaction.editReply({
116 embeds: [
117 new EmojiEmbed()
118 .setTitle("Verify Role")
119 .setDescription("No changes were made")
120 .setStatus("Success")
121 .setEmoji("GUILD.ROLES.CREATE")
122 ],
123 components: []
124 });
pineafan6702cef2022-06-13 17:52:37 +0100125 }
126 }
127 let clicks = 0;
PineaFana00db1b2023-01-02 15:32:54 +0000128 const data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +0100129 let role = data.verify.role;
Skyler Greyad002172022-08-16 18:48:26 +0100130
131 let timedOut = false;
132 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 await interaction.editReply({
134 embeds: [
135 new EmojiEmbed()
136 .setTitle("Verify Role")
137 .setDescription(
Skyler Grey11236ba2022-08-08 21:13:33 +0100138 role ? `Your verify role is currently set to <@&${role}>` : "You have not set a verify role"
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 )
140 .setStatus("Success")
141 .setEmoji("GUILD.ROLES.CREATE")
142 ],
143 components: [
TheCodedProf59772f82023-01-18 22:17:16 -0500144 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400145 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100147 .setLabel(clicks ? "Click again to confirm" : "Reset role")
148 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400149 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 .setDisabled(!role),
TheCodedProf21c08592022-09-13 14:14:43 -0400151 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 .setCustomId("send")
153 .setLabel("Add verify button")
154 .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400155 .setStyle(ButtonStyle.Primary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100156 ])
157 ]
158 });
Skyler Grey1a67e182022-08-04 23:05:44 +0100159 let i: MessageComponentInteraction;
pineafan6702cef2022-06-13 17:52:37 +0100160 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000161 i = await m.awaitMessageComponent({
162 time: 300000,
TheCodedProf267563a2023-01-21 17:00:57 -0500163 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id }
PineaFan0d06edc2023-01-17 22:10:31 +0000164 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100165 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100166 timedOut = true;
167 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100168 }
TheCodedProf267563a2023-01-21 17:00:57 -0500169 await i.deferUpdate();
TheCodedProf59772f82023-01-18 22:17:16 -0500170 if ((i.component as ButtonComponent).customId === "clear") {
TheCodedProf4a6d5712023-01-19 15:54:40 -0500171 clicks ++;
pineafane23c4ec2022-07-27 21:56:27 +0100172 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100173 clicks = 0;
PineaFana00db1b2023-01-02 15:32:54 +0000174 await client.database.guilds.write(interaction.guild.id, null, ["verify.role", "verify.enabled"]);
TheCodedProf59772f82023-01-18 22:17:16 -0500175 role = null;
pineafan6702cef2022-06-13 17:52:37 +0100176 }
177 } else {
TheCodedProf267563a2023-01-21 17:00:57 -0500178 await i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100179 break;
pineafan6702cef2022-06-13 17:52:37 +0100180 }
TheCodedProf1f675042023-02-16 17:01:29 -0500181 } // TODO: On save, clear SEN
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 await interaction.editReply({
TheCodedProf59772f82023-01-18 22:17:16 -0500183 embeds: [new EmbedBuilder(m.embeds[0]!.data).setFooter({ text: "Message closed" })],
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 components: []
185 });
pineafan63fc5e22022-08-04 22:04:10 +0100186};
pineafan4f164f32022-02-26 22:07:12 +0000187
TheCodedProff86ba092023-01-27 17:10:07 -0500188const check = (interaction: CommandInteraction, _partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100189 const member = interaction.member as Discord.GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000190 if (!member.permissions.has("ManageGuild"))
PineaFan0d06edc2023-01-17 22:10:31 +0000191 return "You must have the *Manage Server* permission to use this command";
pineafan4f164f32022-02-26 22:07:12 +0000192 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100193};
pineafan4f164f32022-02-26 22:07:12 +0000194
195export { command };
196export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100197export { check };