blob: 08bf6f85c58aa70a974b7adec72c5bc0aada1874 [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
Skyler Grey11236ba2022-08-08 21:13:33 +010024const callback = async (interaction: CommandInteraction): Promise<void | 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)
pineafan708692b2022-07-24 22:16:22 +010081 .setEmoji("CHANNEL.TEXT.EDIT")
82 .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 }
151 while (true) {
pineafane23c4ec2022-07-27 21:56:27 +0100152 config = await client.database.guilds.read(interaction.guild.id);
pineafan63fc5e22022-08-04 22:04:10 +0100153 const stats = config.getKey("stats");
154 const selectMenu = new MessageSelectMenu()
pineafan0bc04162022-07-25 17:22:26 +0100155 .setCustomId("remove")
156 .setMinValues(1)
pineafan63fc5e22022-08-04 22:04:10 +0100157 .setMaxValues(Math.max(1, Object.keys(stats).length));
Skyler Grey75ea9172022-08-06 10:22:23 +0100158 await interaction.editReply({
159 embeds: [
160 new EmojiEmbed()
161 .setTitle("Stats Channel")
162 .setDescription(
163 "The following channels update when someone joins or leaves the server. You can select a channel to remove it from the list."
164 )
165 .setStatus("Success")
166 .setEmoji("CHANNEL.TEXT.CREATE")
167 ],
168 components: [
169 new MessageActionRow().addComponents(
170 Object.keys(stats).length
171 ? [
172 selectMenu
Skyler Grey11236ba2022-08-08 21:13:33 +0100173 .setPlaceholder("Select a stats channel to remove, stopping it updating")
Skyler Grey75ea9172022-08-06 10:22:23 +0100174 .addOptions(
175 Object.keys(stats).map((key) => ({
Skyler Grey11236ba2022-08-08 21:13:33 +0100176 label: interaction.guild.channels.cache.get(key).name,
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 value: key,
178 description: `${stats[key].name}`
179 }))
180 )
181 ]
182 : [
183 selectMenu
Skyler Grey11236ba2022-08-08 21:13:33 +0100184 .setPlaceholder("The server has no stats channels")
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 .setDisabled(true)
186 .setOptions([
187 {
188 label: "*Placeholder*",
189 value: "placeholder",
190 description: "No stats channels"
191 }
192 ])
193 ]
194 )
195 ]
196 });
pineafan0bc04162022-07-25 17:22:26 +0100197 let i;
198 try {
199 i = await m.awaitMessageComponent({ time: 300000 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100200 } catch (e) {
201 break;
202 }
pineafan63fc5e22022-08-04 22:04:10 +0100203 i.deferUpdate();
pineafan0bc04162022-07-25 17:22:26 +0100204 if (i.customId === "remove") {
pineafan63fc5e22022-08-04 22:04:10 +0100205 const toRemove = i.values;
Skyler Grey75ea9172022-08-06 10:22:23 +0100206 await client.database.guilds.write(
207 interaction.guild.id,
208 null,
209 toRemove.map((k) => `stats.${k}`)
210 );
pineafan0bc04162022-07-25 17:22:26 +0100211 }
pineafan708692b2022-07-24 22:16:22 +0100212 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100213 await interaction.editReply({
214 embeds: [m.embeds[0]!.setFooter({ text: "Message closed" })],
215 components: []
216 });
pineafan63fc5e22022-08-04 22:04:10 +0100217};
pineafan708692b2022-07-24 22:16:22 +0100218
Skyler Grey11236ba2022-08-08 21:13:33 +0100219const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100220 const member = interaction.member as Discord.GuildMember;
221 if (!member.permissions.has("MANAGE_CHANNELS"))
222 throw "You must have the *Manage Channels* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100223 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100224};
pineafan708692b2022-07-24 22:16:22 +0100225
226export { command };
227export { callback };
Skyler Grey75ea9172022-08-06 10:22:23 +0100228export { check };