blob: 10fba6b408b917c515fcb9651bd08e6d60132521 [file] [log] [blame]
pineafane23c4ec2022-07-27 21:56:27 +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";
8import convertCurlyBracketString from '../../utils/convertCurlyBracketString.js';
9import {callback as statsChannelAddCallback} from "../../reflex/statsChannelUpdate.js";
pineafane23c4ec2022-07-27 21:56:27 +010010import singleNotify from '../../utils/singleNotify.js';
pineafan708692b2022-07-24 22:16:22 +010011
12const command = (builder: SlashCommandSubcommandBuilder) =>
13 builder
pineafan0bc04162022-07-25 17:22:26 +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> => {
pineafane23c4ec2022-07-27 21:56:27 +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")
32 ]})
33 }
pineafan708692b2022-07-24 22:16:22 +010034 try {
35 channel = interaction.options.getChannel("channel")
36 } 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")
42 ]})
43 }
44 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")
48 .setDescription(`You must choose a channel in this server`)
49 .setStatus("Danger")
50 .setEmoji("CHANNEL.TEXT.DELETE")
51 ]});
52 }
53 let newName = await convertCurlyBracketString(interaction.options.getString("name"), null, null, interaction.guild.name, interaction.guild.members)
54 if (interaction.options.getChannel("channel").type === "GUILD_TEXT") {
55 newName = newName.toLowerCase().replace(/[\s]/g, "-")
56 }
57 let confirmation = await new confirmationMessage(interaction)
58 .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)
63 .send(true)
64 if (confirmation.cancelled) return
65 if (confirmation.success) {
66 try {
67 let name = interaction.options.getString("name")
68 let channel = interaction.options.getChannel("channel")
69 await client.database.guilds.write(interaction.guild.id, {[`stats.${channel.id}`]: {name: name, enabled: true}});
70 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
71 try {
72 let data = {
73 meta:{
74 type: 'statsChannelUpdate',
75 displayName: 'Stats Channel Updated',
76 calculateType: 'nucleusSettingsUpdated',
77 color: NucleusColors.yellow,
78 emoji: "CHANNEL.TEXT.EDIT",
79 timestamp: new Date().getTime()
80 },
81 list: {
82 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
83 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
84 channel: entry(channel.id, renderChannel(channel)),
85 name: entry(interaction.options.getString("name"), `\`${interaction.options.getString("name")}\``)
86 },
87 hidden: {
88 guild: interaction.guild.id
89 }
90 }
91 log(data);
92 } catch {}
93 } catch (e) {
94 console.log(e)
95 return interaction.editReply({embeds: [new EmojiEmbed()
96 .setTitle("Stats Channel")
97 .setDescription(`Something went wrong and the stats channel could not be set`)
98 .setStatus("Danger")
99 .setEmoji("CHANNEL.TEXT.DELETE")
100 ], components: []});
101 }
102 } else {
103 return interaction.editReply({embeds: [new EmojiEmbed()
104 .setTitle("Stats Channel")
105 .setDescription(`No changes were made`)
106 .setStatus("Success")
107 .setEmoji("CHANNEL.TEXT.CREATE")
108 ], components: []});
109 }
110 await statsChannelAddCallback(client, interaction.member);
pineafan0bc04162022-07-25 17:22:26 +0100111 }
112 while (true) {
pineafane23c4ec2022-07-27 21:56:27 +0100113 config = await client.database.guilds.read(interaction.guild.id);
pineafan0bc04162022-07-25 17:22:26 +0100114 let stats = config.getKey("stats")
115 let selectMenu = new MessageSelectMenu()
116 .setCustomId("remove")
117 .setMinValues(1)
118 .setMaxValues(Math.max(1, Object.keys(stats).length))
119 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan708692b2022-07-24 22:16:22 +0100120 .setTitle("Stats Channel")
pineafan0bc04162022-07-25 17:22:26 +0100121 .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 +0100122 .setStatus("Success")
123 .setEmoji("CHANNEL.TEXT.CREATE")
pineafan0bc04162022-07-25 17:22:26 +0100124 ], components: [
125 new MessageActionRow().addComponents(Object.keys(stats).length ? [
126 selectMenu.setPlaceholder("Select a stats channel to remove, stopping it updating").addOptions(Object.keys(stats).map(key => ({
127 label: interaction.guild.channels.cache.get(key).name,
128 value: key,
129 description: `${stats[key].name}`,
130 })))
131 ] : [selectMenu.setPlaceholder("The server has no stats channels").setDisabled(true).setOptions([
132 {label: "*Placeholder*", value: "placeholder", description: "No stats channels"}
133 ])])
134 ]})
135 let i;
136 try {
137 i = await m.awaitMessageComponent({ time: 300000 });
138 } catch (e) { break }
139 i.deferUpdate()
140 if (i.customId === "remove") {
141 let toRemove = i.values;
pineafane23c4ec2022-07-27 21:56:27 +0100142 await client.database.guilds.write(interaction.guild.id, null, toRemove.map(k => `stats.${k}`));
pineafan0bc04162022-07-25 17:22:26 +0100143 }
pineafan708692b2022-07-24 22:16:22 +0100144 }
pineafan41d93562022-07-30 22:10:15 +0100145 await interaction.editReply({embeds: [m.embeds[0].setFooter({text: "Message closed"})], components: []});
pineafan708692b2022-07-24 22:16:22 +0100146}
147
148const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
149 let member = (interaction.member as Discord.GuildMember)
pineafanc1c18792022-08-03 21:41:36 +0100150 if (!member.permissions.has("MANAGE_CHANNELS")) throw "You must have the *Manage Channels* permission to use this command"
pineafan708692b2022-07-24 22:16:22 +0100151 return true;
152}
153
154export { command };
155export { callback };
pineafan0bc04162022-07-25 17:22:26 +0100156export { check };