blob: 5a1affd9a44d6c59b338e67ece1d0db03899dc97 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
pineafanbd02b4a2022-08-05 22:01:38 +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";
pineafan0bc04162022-07-25 17:22:26 +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")
15 .setDescription("Controls channels which update when someone joins or leaves the server")
16 .addChannelOption(option => option.setName("channel").setDescription("The channel to modify"))
17 .addStringOption(option => option.setName("name").setDescription("The new channel name | Enter any text or use the extra variables like {memberCount}").setAutocomplete(true));
pineafan708692b2022-07-24 22:16:22 +010018
pineafanbd02b4a2022-08-05 22:01:38 +010019const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010020 singleNotify("statsChannelDeleted", interaction.guild.id, true);
pineafanbd02b4a2022-08-05 22:01:38 +010021 const m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}) as Message;
pineafane23c4ec2022-07-27 21:56:27 +010022 let config = await client.database.guilds.read(interaction.guild.id);
pineafan0bc04162022-07-25 17:22:26 +010023 if (interaction.options.getString("name")) {
pineafane23c4ec2022-07-27 21:56:27 +010024 let channel;
25 if (Object.keys(config.getKey("stats")).length >= 25) {
26 return await interaction.editReply({embeds: [new EmojiEmbed()
27 .setEmoji("CHANNEL.TEXT.DELETE")
28 .setTitle("Stats Channel")
29 .setDescription("You can only have 25 stats channels in a server")
30 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010031 ]});
pineafane23c4ec2022-07-27 21:56:27 +010032 }
pineafan708692b2022-07-24 22:16:22 +010033 try {
pineafan63fc5e22022-08-04 22:04:10 +010034 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010035 } catch {
36 return await interaction.editReply({embeds: [new EmojiEmbed()
37 .setEmoji("CHANNEL.TEXT.DELETE")
38 .setTitle("Stats Channel")
39 .setDescription("The channel you provided is not a valid channel")
40 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010041 ]});
pineafan708692b2022-07-24 22:16:22 +010042 }
pineafan63fc5e22022-08-04 22:04:10 +010043 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010044 if (channel.guild.id !== interaction.guild.id) {
pineafan708692b2022-07-24 22:16:22 +010045 return interaction.editReply({embeds: [new EmojiEmbed()
46 .setTitle("Stats Channel")
pineafan63fc5e22022-08-04 22:04:10 +010047 .setDescription("You must choose a channel in this server")
pineafan708692b2022-07-24 22:16:22 +010048 .setStatus("Danger")
49 .setEmoji("CHANNEL.TEXT.DELETE")
50 ]});
51 }
pineafan63fc5e22022-08-04 22:04:10 +010052 let newName = await convertCurlyBracketString(interaction.options.getString("name"), null, null, interaction.guild.name, interaction.guild.members);
pineafan708692b2022-07-24 22:16:22 +010053 if (interaction.options.getChannel("channel").type === "GUILD_TEXT") {
pineafan63fc5e22022-08-04 22:04:10 +010054 newName = newName.toLowerCase().replace(/[\s]/g, "-");
pineafan708692b2022-07-24 22:16:22 +010055 }
pineafan63fc5e22022-08-04 22:04:10 +010056 const confirmation = await new confirmationMessage(interaction)
pineafan708692b2022-07-24 22:16:22 +010057 .setEmoji("CHANNEL.TEXT.EDIT")
58 .setTitle("Stats Channel")
pineafane23c4ec2022-07-27 21:56:27 +010059 .setDescription(`Are you sure you want to set <#${channel.id}> to a stats channel?\n\n*Preview: ${newName.replace(/^ +| $/g, "")}*`)
pineafan708692b2022-07-24 22:16:22 +010060 .setColor("Warning")
61 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010062 .send(true);
63 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010064 if (confirmation.success) {
65 try {
pineafan63fc5e22022-08-04 22:04:10 +010066 const name = interaction.options.getString("name");
67 const channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010068 await client.database.guilds.write(interaction.guild.id, {[`stats.${channel.id}`]: {name: name, enabled: true}});
pineafan63fc5e22022-08-04 22:04:10 +010069 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
70 const data = {
71 meta:{
72 type: "statsChannelUpdate",
73 displayName: "Stats Channel Updated",
74 calculateType: "nucleusSettingsUpdated",
75 color: NucleusColors.yellow,
76 emoji: "CHANNEL.TEXT.EDIT",
77 timestamp: new Date().getTime()
78 },
79 list: {
80 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
81 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
82 channel: entry(channel.id, renderChannel(channel)),
83 name: entry(interaction.options.getString("name"), `\`${interaction.options.getString("name")}\``)
84 },
85 hidden: {
86 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010087 }
pineafan63fc5e22022-08-04 22:04:10 +010088 };
89 log(data);
pineafan708692b2022-07-24 22:16:22 +010090 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010091 console.log(e);
pineafan708692b2022-07-24 22:16:22 +010092 return interaction.editReply({embeds: [new EmojiEmbed()
93 .setTitle("Stats Channel")
pineafan63fc5e22022-08-04 22:04:10 +010094 .setDescription("Something went wrong and the stats channel could not be set")
pineafan708692b2022-07-24 22:16:22 +010095 .setStatus("Danger")
96 .setEmoji("CHANNEL.TEXT.DELETE")
97 ], components: []});
98 }
99 } else {
100 return interaction.editReply({embeds: [new EmojiEmbed()
101 .setTitle("Stats Channel")
pineafan63fc5e22022-08-04 22:04:10 +0100102 .setDescription("No changes were made")
pineafan708692b2022-07-24 22:16:22 +0100103 .setStatus("Success")
104 .setEmoji("CHANNEL.TEXT.CREATE")
105 ], components: []});
106 }
107 await statsChannelAddCallback(client, interaction.member);
pineafan0bc04162022-07-25 17:22:26 +0100108 }
109 while (true) {
pineafane23c4ec2022-07-27 21:56:27 +0100110 config = await client.database.guilds.read(interaction.guild.id);
pineafan63fc5e22022-08-04 22:04:10 +0100111 const stats = config.getKey("stats");
112 const selectMenu = new MessageSelectMenu()
pineafan0bc04162022-07-25 17:22:26 +0100113 .setCustomId("remove")
114 .setMinValues(1)
pineafan63fc5e22022-08-04 22:04:10 +0100115 .setMaxValues(Math.max(1, Object.keys(stats).length));
pineafan0bc04162022-07-25 17:22:26 +0100116 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan708692b2022-07-24 22:16:22 +0100117 .setTitle("Stats Channel")
pineafan0bc04162022-07-25 17:22:26 +0100118 .setDescription("The following channels update when someone joins or leaves the server. You can select a channel to remove it from the list.")
pineafan708692b2022-07-24 22:16:22 +0100119 .setStatus("Success")
120 .setEmoji("CHANNEL.TEXT.CREATE")
pineafan0bc04162022-07-25 17:22:26 +0100121 ], components: [
122 new MessageActionRow().addComponents(Object.keys(stats).length ? [
123 selectMenu.setPlaceholder("Select a stats channel to remove, stopping it updating").addOptions(Object.keys(stats).map(key => ({
124 label: interaction.guild.channels.cache.get(key).name,
125 value: key,
pineafan63fc5e22022-08-04 22:04:10 +0100126 description: `${stats[key].name}`
pineafan0bc04162022-07-25 17:22:26 +0100127 })))
128 ] : [selectMenu.setPlaceholder("The server has no stats channels").setDisabled(true).setOptions([
129 {label: "*Placeholder*", value: "placeholder", description: "No stats channels"}
130 ])])
pineafan63fc5e22022-08-04 22:04:10 +0100131 ]});
pineafan0bc04162022-07-25 17:22:26 +0100132 let i;
133 try {
134 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +0100135 } catch (e) { break; }
136 i.deferUpdate();
pineafan0bc04162022-07-25 17:22:26 +0100137 if (i.customId === "remove") {
pineafan63fc5e22022-08-04 22:04:10 +0100138 const toRemove = i.values;
pineafane23c4ec2022-07-27 21:56:27 +0100139 await client.database.guilds.write(interaction.guild.id, null, toRemove.map(k => `stats.${k}`));
pineafan0bc04162022-07-25 17:22:26 +0100140 }
pineafan708692b2022-07-24 22:16:22 +0100141 }
pineafanbd02b4a2022-08-05 22:01:38 +0100142 await interaction.editReply({embeds: [(m.embeds[0] as Discord.MessageEmbed).setFooter({text: "Message closed"})], components: []});
pineafan63fc5e22022-08-04 22:04:10 +0100143};
pineafan708692b2022-07-24 22:16:22 +0100144
pineafanbd02b4a2022-08-05 22:01:38 +0100145const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100146 const member = (interaction.member as Discord.GuildMember);
147 if (!member.permissions.has("MANAGE_CHANNELS")) throw "You must have the *Manage Channels* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100148 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100149};
pineafan708692b2022-07-24 22:16:22 +0100150
151export { command };
152export { callback };
pineafan0bc04162022-07-25 17:22:26 +0100153export { check };