blob: 4d614964ae5e36ec53af0385be356c4a948cdc19 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
pineafanc1c18792022-08-03 21:41:36 +01002import Discord, { CommandInteraction, 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
19const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan63fc5e22022-08-04 22:04:10 +010020 singleNotify("statsChannelDeleted", interaction.guild.id, true);
pineafan708692b2022-07-24 22:16:22 +010021 let m;
pineafane23c4ec2022-07-27 21:56:27 +010022 m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true});
23 let config = await client.database.guilds.read(interaction.guild.id);
pineafan0bc04162022-07-25 17:22:26 +010024 if (interaction.options.getString("name")) {
pineafane23c4ec2022-07-27 21:56:27 +010025 let channel;
26 if (Object.keys(config.getKey("stats")).length >= 25) {
27 return await interaction.editReply({embeds: [new EmojiEmbed()
28 .setEmoji("CHANNEL.TEXT.DELETE")
29 .setTitle("Stats Channel")
30 .setDescription("You can only have 25 stats channels in a server")
31 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010032 ]});
pineafane23c4ec2022-07-27 21:56:27 +010033 }
pineafan708692b2022-07-24 22:16:22 +010034 try {
pineafan63fc5e22022-08-04 22:04:10 +010035 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010036 } catch {
37 return await interaction.editReply({embeds: [new EmojiEmbed()
38 .setEmoji("CHANNEL.TEXT.DELETE")
39 .setTitle("Stats Channel")
40 .setDescription("The channel you provided is not a valid channel")
41 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010042 ]});
pineafan708692b2022-07-24 22:16:22 +010043 }
pineafan63fc5e22022-08-04 22:04:10 +010044 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010045 if (channel.guild.id !== interaction.guild.id) {
pineafan708692b2022-07-24 22:16:22 +010046 return interaction.editReply({embeds: [new EmojiEmbed()
47 .setTitle("Stats Channel")
pineafan63fc5e22022-08-04 22:04:10 +010048 .setDescription("You must choose a channel in this server")
pineafan708692b2022-07-24 22:16:22 +010049 .setStatus("Danger")
50 .setEmoji("CHANNEL.TEXT.DELETE")
51 ]});
52 }
pineafan63fc5e22022-08-04 22:04:10 +010053 let newName = await convertCurlyBracketString(interaction.options.getString("name"), null, null, interaction.guild.name, interaction.guild.members);
pineafan708692b2022-07-24 22:16:22 +010054 if (interaction.options.getChannel("channel").type === "GUILD_TEXT") {
pineafan63fc5e22022-08-04 22:04:10 +010055 newName = newName.toLowerCase().replace(/[\s]/g, "-");
pineafan708692b2022-07-24 22:16:22 +010056 }
pineafan63fc5e22022-08-04 22:04:10 +010057 const confirmation = await new confirmationMessage(interaction)
pineafan708692b2022-07-24 22:16:22 +010058 .setEmoji("CHANNEL.TEXT.EDIT")
59 .setTitle("Stats Channel")
pineafane23c4ec2022-07-27 21:56:27 +010060 .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 +010061 .setColor("Warning")
62 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010063 .send(true);
64 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010065 if (confirmation.success) {
66 try {
pineafan63fc5e22022-08-04 22:04:10 +010067 const name = interaction.options.getString("name");
68 const channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010069 await client.database.guilds.write(interaction.guild.id, {[`stats.${channel.id}`]: {name: name, enabled: true}});
pineafan63fc5e22022-08-04 22:04:10 +010070 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
71 const data = {
72 meta:{
73 type: "statsChannelUpdate",
74 displayName: "Stats Channel Updated",
75 calculateType: "nucleusSettingsUpdated",
76 color: NucleusColors.yellow,
77 emoji: "CHANNEL.TEXT.EDIT",
78 timestamp: new Date().getTime()
79 },
80 list: {
81 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
82 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
83 channel: entry(channel.id, renderChannel(channel)),
84 name: entry(interaction.options.getString("name"), `\`${interaction.options.getString("name")}\``)
85 },
86 hidden: {
87 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010088 }
pineafan63fc5e22022-08-04 22:04:10 +010089 };
90 log(data);
pineafan708692b2022-07-24 22:16:22 +010091 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010092 console.log(e);
pineafan708692b2022-07-24 22:16:22 +010093 return interaction.editReply({embeds: [new EmojiEmbed()
94 .setTitle("Stats Channel")
pineafan63fc5e22022-08-04 22:04:10 +010095 .setDescription("Something went wrong and the stats channel could not be set")
pineafan708692b2022-07-24 22:16:22 +010096 .setStatus("Danger")
97 .setEmoji("CHANNEL.TEXT.DELETE")
98 ], components: []});
99 }
100 } else {
101 return interaction.editReply({embeds: [new EmojiEmbed()
102 .setTitle("Stats Channel")
pineafan63fc5e22022-08-04 22:04:10 +0100103 .setDescription("No changes were made")
pineafan708692b2022-07-24 22:16:22 +0100104 .setStatus("Success")
105 .setEmoji("CHANNEL.TEXT.CREATE")
106 ], components: []});
107 }
108 await statsChannelAddCallback(client, interaction.member);
pineafan0bc04162022-07-25 17:22:26 +0100109 }
110 while (true) {
pineafane23c4ec2022-07-27 21:56:27 +0100111 config = await client.database.guilds.read(interaction.guild.id);
pineafan63fc5e22022-08-04 22:04:10 +0100112 const stats = config.getKey("stats");
113 const selectMenu = new MessageSelectMenu()
pineafan0bc04162022-07-25 17:22:26 +0100114 .setCustomId("remove")
115 .setMinValues(1)
pineafan63fc5e22022-08-04 22:04:10 +0100116 .setMaxValues(Math.max(1, Object.keys(stats).length));
pineafan0bc04162022-07-25 17:22:26 +0100117 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan708692b2022-07-24 22:16:22 +0100118 .setTitle("Stats Channel")
pineafan0bc04162022-07-25 17:22:26 +0100119 .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 +0100120 .setStatus("Success")
121 .setEmoji("CHANNEL.TEXT.CREATE")
pineafan0bc04162022-07-25 17:22:26 +0100122 ], components: [
123 new MessageActionRow().addComponents(Object.keys(stats).length ? [
124 selectMenu.setPlaceholder("Select a stats channel to remove, stopping it updating").addOptions(Object.keys(stats).map(key => ({
125 label: interaction.guild.channels.cache.get(key).name,
126 value: key,
pineafan63fc5e22022-08-04 22:04:10 +0100127 description: `${stats[key].name}`
pineafan0bc04162022-07-25 17:22:26 +0100128 })))
129 ] : [selectMenu.setPlaceholder("The server has no stats channels").setDisabled(true).setOptions([
130 {label: "*Placeholder*", value: "placeholder", description: "No stats channels"}
131 ])])
pineafan63fc5e22022-08-04 22:04:10 +0100132 ]});
pineafan0bc04162022-07-25 17:22:26 +0100133 let i;
134 try {
135 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +0100136 } catch (e) { break; }
137 i.deferUpdate();
pineafan0bc04162022-07-25 17:22:26 +0100138 if (i.customId === "remove") {
pineafan63fc5e22022-08-04 22:04:10 +0100139 const toRemove = i.values;
pineafane23c4ec2022-07-27 21:56:27 +0100140 await client.database.guilds.write(interaction.guild.id, null, toRemove.map(k => `stats.${k}`));
pineafan0bc04162022-07-25 17:22:26 +0100141 }
pineafan708692b2022-07-24 22:16:22 +0100142 }
pineafan41d93562022-07-30 22:10:15 +0100143 await interaction.editReply({embeds: [m.embeds[0].setFooter({text: "Message closed"})], components: []});
pineafan63fc5e22022-08-04 22:04:10 +0100144};
pineafan708692b2022-07-24 22:16:22 +0100145
146const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100147 const member = (interaction.member as Discord.GuildMember);
148 if (!member.permissions.has("MANAGE_CHANNELS")) throw "You must have the *Manage Channels* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100149 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100150};
pineafan708692b2022-07-24 22:16:22 +0100151
152export { command };
153export { callback };
pineafan0bc04162022-07-25 17:22:26 +0100154export { check };