blob: 932605c1b65602137890d09f4e94e1624e86c36e [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
Skyler Grey11236ba2022-08-08 21:13:33 +01002import Discord, { CommandInteraction, Message, MessageActionRow, MessageSelectMenu } from "discord.js";
pineafan0bc04162022-07-25 17:22:26 +01003import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
4import confirmationMessage from "../../utils/confirmationMessage.js";
pineafanc1c18792022-08-03 21:41:36 +01005import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan708692b2022-07-24 22:16:22 +01006import { WrappedCheck } from "jshaiku";
pineafan0bc04162022-07-25 17:22:26 +01007import client from "../../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +01008import convertCurlyBracketString from "../../utils/convertCurlyBracketString.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01009import { callback as statsChannelAddCallback } from "../../reflex/statsChannelUpdate.js";
pineafan63fc5e22022-08-04 22:04:10 +010010import singleNotify from "../../utils/singleNotify.js";
pineafan708692b2022-07-24 22:16:22 +010011
12const command = (builder: SlashCommandSubcommandBuilder) =>
13 builder
pineafan63fc5e22022-08-04 22:04:10 +010014 .setName("stats")
Skyler Grey11236ba2022-08-08 21:13:33 +010015 .setDescription("Controls channels which update when someone joins or leaves the server")
16 .addChannelOption((option) => option.setName("channel").setDescription("The channel to modify"))
Skyler Grey75ea9172022-08-06 10:22:23 +010017 .addStringOption((option) =>
18 option
19 .setName("name")
Skyler Grey11236ba2022-08-08 21:13:33 +010020 .setDescription("The new channel name | Enter any text or use the extra variables like {memberCount}")
Skyler Grey75ea9172022-08-06 10:22:23 +010021 .setAutocomplete(true)
22 );
pineafan708692b2022-07-24 22:16:22 +010023
pineafan3a02ea32022-08-11 21:35:04 +010024const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010025 singleNotify("statsChannelDeleted", interaction.guild.id, true);
Skyler Grey75ea9172022-08-06 10:22:23 +010026 const m = (await interaction.reply({
27 embeds: LoadingEmbed,
28 ephemeral: true,
29 fetchReply: true
30 })) as Message;
pineafane23c4ec2022-07-27 21:56:27 +010031 let config = await client.database.guilds.read(interaction.guild.id);
pineafan0bc04162022-07-25 17:22:26 +010032 if (interaction.options.getString("name")) {
pineafane23c4ec2022-07-27 21:56:27 +010033 let channel;
34 if (Object.keys(config.getKey("stats")).length >= 25) {
Skyler Grey75ea9172022-08-06 10:22:23 +010035 return await interaction.editReply({
36 embeds: [
37 new EmojiEmbed()
38 .setEmoji("CHANNEL.TEXT.DELETE")
39 .setTitle("Stats Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010040 .setDescription("You can only have 25 stats channels in a server")
Skyler Grey75ea9172022-08-06 10:22:23 +010041 .setStatus("Danger")
42 ]
43 });
pineafane23c4ec2022-07-27 21:56:27 +010044 }
pineafan708692b2022-07-24 22:16:22 +010045 try {
pineafan63fc5e22022-08-04 22:04:10 +010046 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010047 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010048 return await interaction.editReply({
49 embeds: [
50 new EmojiEmbed()
51 .setEmoji("CHANNEL.TEXT.DELETE")
52 .setTitle("Stats Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010053 .setDescription("The channel you provided is not a valid channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010054 .setStatus("Danger")
55 ]
56 });
pineafan708692b2022-07-24 22:16:22 +010057 }
pineafan63fc5e22022-08-04 22:04:10 +010058 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010059 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010060 return interaction.editReply({
61 embeds: [
62 new EmojiEmbed()
63 .setTitle("Stats Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010064 .setDescription("You must choose a channel in this server")
Skyler Grey75ea9172022-08-06 10:22:23 +010065 .setStatus("Danger")
66 .setEmoji("CHANNEL.TEXT.DELETE")
67 ]
68 });
pineafan708692b2022-07-24 22:16:22 +010069 }
Skyler Grey75ea9172022-08-06 10:22:23 +010070 let newName = await convertCurlyBracketString(
71 interaction.options.getString("name"),
72 null,
73 null,
74 interaction.guild.name,
75 interaction.guild.members
76 );
pineafan708692b2022-07-24 22:16:22 +010077 if (interaction.options.getChannel("channel").type === "GUILD_TEXT") {
pineafan63fc5e22022-08-04 22:04:10 +010078 newName = newName.toLowerCase().replace(/[\s]/g, "-");
pineafan708692b2022-07-24 22:16:22 +010079 }
pineafan63fc5e22022-08-04 22:04:10 +010080 const confirmation = await new confirmationMessage(interaction)
pineafan62ce1922022-08-25 20:34:45 +010081 .setEmoji("CHANNEL.TEXT.EDIT", "CHANNEL.TEXT.DELETE")
pineafan708692b2022-07-24 22:16:22 +010082 .setTitle("Stats Channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010083 .setDescription(
Skyler Grey11236ba2022-08-08 21:13:33 +010084 `Are you sure you want to set <#${channel.id}> to a stats channel?\n\n*Preview: ${newName.replace(
Skyler Grey75ea9172022-08-06 10:22:23 +010085 /^ +| $/g,
86 ""
87 )}*`
88 )
pineafan708692b2022-07-24 22:16:22 +010089 .setColor("Warning")
90 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010091 .send(true);
92 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010093 if (confirmation.success) {
94 try {
pineafan63fc5e22022-08-04 22:04:10 +010095 const name = interaction.options.getString("name");
96 const channel = interaction.options.getChannel("channel");
Skyler Grey75ea9172022-08-06 10:22:23 +010097 await client.database.guilds.write(interaction.guild.id, {
98 [`stats.${channel.id}`]: { name: name, enabled: true }
99 });
Skyler Grey11236ba2022-08-08 21:13:33 +0100100 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +0100101 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100103 type: "statsChannelUpdate",
104 displayName: "Stats Channel Updated",
105 calculateType: "nucleusSettingsUpdated",
106 color: NucleusColors.yellow,
107 emoji: "CHANNEL.TEXT.EDIT",
108 timestamp: new Date().getTime()
109 },
110 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100111 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
112 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +0100113 channel: entry(channel.id, renderChannel(channel)),
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 name: entry(
115 interaction.options.getString("name"),
116 `\`${interaction.options.getString("name")}\``
117 )
pineafan63fc5e22022-08-04 22:04:10 +0100118 },
119 hidden: {
120 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +0100121 }
pineafan63fc5e22022-08-04 22:04:10 +0100122 };
123 log(data);
pineafan708692b2022-07-24 22:16:22 +0100124 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100125 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +0100126 return interaction.editReply({
127 embeds: [
128 new EmojiEmbed()
129 .setTitle("Stats Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +0100130 .setDescription("Something went wrong and the stats channel could not be set")
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 .setStatus("Danger")
132 .setEmoji("CHANNEL.TEXT.DELETE")
133 ],
134 components: []
135 });
pineafan708692b2022-07-24 22:16:22 +0100136 }
137 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100138 return interaction.editReply({
139 embeds: [
140 new EmojiEmbed()
141 .setTitle("Stats Channel")
142 .setDescription("No changes were made")
143 .setStatus("Success")
144 .setEmoji("CHANNEL.TEXT.CREATE")
145 ],
146 components: []
147 });
pineafan708692b2022-07-24 22:16:22 +0100148 }
149 await statsChannelAddCallback(client, interaction.member);
pineafan0bc04162022-07-25 17:22:26 +0100150 }
Skyler Greyad002172022-08-16 18:48:26 +0100151 let timedOut = false;
152 while (!timedOut) {
pineafane23c4ec2022-07-27 21:56:27 +0100153 config = await client.database.guilds.read(interaction.guild.id);
pineafan63fc5e22022-08-04 22:04:10 +0100154 const stats = config.getKey("stats");
155 const selectMenu = new MessageSelectMenu()
pineafan0bc04162022-07-25 17:22:26 +0100156 .setCustomId("remove")
157 .setMinValues(1)
pineafan63fc5e22022-08-04 22:04:10 +0100158 .setMaxValues(Math.max(1, Object.keys(stats).length));
Skyler Grey75ea9172022-08-06 10:22:23 +0100159 await interaction.editReply({
160 embeds: [
161 new EmojiEmbed()
162 .setTitle("Stats Channel")
163 .setDescription(
164 "The following channels update when someone joins or leaves the server. You can select a channel to remove it from the list."
165 )
166 .setStatus("Success")
167 .setEmoji("CHANNEL.TEXT.CREATE")
168 ],
169 components: [
170 new MessageActionRow().addComponents(
171 Object.keys(stats).length
172 ? [
173 selectMenu
Skyler Grey11236ba2022-08-08 21:13:33 +0100174 .setPlaceholder("Select a stats channel to remove, stopping it updating")
Skyler Grey75ea9172022-08-06 10:22:23 +0100175 .addOptions(
176 Object.keys(stats).map((key) => ({
Skyler Grey11236ba2022-08-08 21:13:33 +0100177 label: interaction.guild.channels.cache.get(key).name,
Skyler Grey75ea9172022-08-06 10:22:23 +0100178 value: key,
179 description: `${stats[key].name}`
180 }))
181 )
182 ]
183 : [
184 selectMenu
Skyler Grey11236ba2022-08-08 21:13:33 +0100185 .setPlaceholder("The server has no stats channels")
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 .setDisabled(true)
187 .setOptions([
188 {
189 label: "*Placeholder*",
190 value: "placeholder",
191 description: "No stats channels"
192 }
193 ])
194 ]
195 )
196 ]
197 });
pineafan0bc04162022-07-25 17:22:26 +0100198 let i;
199 try {
200 i = await m.awaitMessageComponent({ time: 300000 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100201 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100202 timedOut = true;
203 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100204 }
pineafan63fc5e22022-08-04 22:04:10 +0100205 i.deferUpdate();
pineafan0bc04162022-07-25 17:22:26 +0100206 if (i.customId === "remove") {
pineafan63fc5e22022-08-04 22:04:10 +0100207 const toRemove = i.values;
Skyler Grey75ea9172022-08-06 10:22:23 +0100208 await client.database.guilds.write(
209 interaction.guild.id,
210 null,
211 toRemove.map((k) => `stats.${k}`)
212 );
pineafan0bc04162022-07-25 17:22:26 +0100213 }
pineafan708692b2022-07-24 22:16:22 +0100214 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100215 await interaction.editReply({
Skyler Greyad002172022-08-16 18:48:26 +0100216 embeds: [m.embeds[0]!.setFooter({ text: "Message timed out" })],
Skyler Grey75ea9172022-08-06 10:22:23 +0100217 components: []
218 });
pineafan63fc5e22022-08-04 22:04:10 +0100219};
pineafan708692b2022-07-24 22:16:22 +0100220
Skyler Grey11236ba2022-08-08 21:13:33 +0100221const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100222 const member = interaction.member as Discord.GuildMember;
223 if (!member.permissions.has("MANAGE_CHANNELS"))
pineafan3a02ea32022-08-11 21:35:04 +0100224 throw new Error("You must have the *Manage Channels* permission to use this command");
pineafan708692b2022-07-24 22:16:22 +0100225 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100226};
pineafan708692b2022-07-24 22:16:22 +0100227
228export { command };
229export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100230export { check };