blob: be15869948546784b9037f6b120399634f9e527f [file] [log] [blame]
pineafane23c4ec2022-07-27 21:56:27 +01001import { LoadingEmbed } from './../../utils/defaultEmbeds.js';
pineafan708692b2022-07-24 22:16:22 +01002import { ChannelType } from 'discord-api-types';
pineafan0bc04162022-07-25 17:22:26 +01003import Discord, { AutocompleteInteraction, CommandInteraction, Message, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js";
4import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
5import confirmationMessage from "../../utils/confirmationMessage.js";
6import getEmojiByName from "../../utils/getEmojiByName.js";
7import { SelectMenuOption, SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan708692b2022-07-24 22:16:22 +01008import { WrappedCheck } from "jshaiku";
pineafan0bc04162022-07-25 17:22:26 +01009import client from "../../utils/client.js";
10import convertCurlyBracketString from '../../utils/convertCurlyBracketString.js';
11import {callback as statsChannelAddCallback} from "../../reflex/statsChannelUpdate.js";
pineafane23c4ec2022-07-27 21:56:27 +010012import singleNotify from '../../utils/singleNotify.js';
pineafan708692b2022-07-24 22:16:22 +010013
14const command = (builder: SlashCommandSubcommandBuilder) =>
15 builder
pineafan0bc04162022-07-25 17:22:26 +010016 .setName("stats")
17 .setDescription("Controls channels which update when someone joins or leaves the server")
18 .addChannelOption(option => option.setName("channel").setDescription("The channel to modify"))
19 .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 +010020
21const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafane23c4ec2022-07-27 21:56:27 +010022 singleNotify("statsChannelDeleted", interaction.guild.id, true)
pineafan708692b2022-07-24 22:16:22 +010023 let m;
pineafane23c4ec2022-07-27 21:56:27 +010024 m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true});
25 let config = await client.database.guilds.read(interaction.guild.id);
pineafan0bc04162022-07-25 17:22:26 +010026 if (interaction.options.getString("name")) {
pineafane23c4ec2022-07-27 21:56:27 +010027 let channel;
28 if (Object.keys(config.getKey("stats")).length >= 25) {
29 return await interaction.editReply({embeds: [new EmojiEmbed()
30 .setEmoji("CHANNEL.TEXT.DELETE")
31 .setTitle("Stats Channel")
32 .setDescription("You can only have 25 stats channels in a server")
33 .setStatus("Danger")
34 ]})
35 }
pineafan708692b2022-07-24 22:16:22 +010036 try {
37 channel = interaction.options.getChannel("channel")
38 } catch {
39 return await interaction.editReply({embeds: [new EmojiEmbed()
40 .setEmoji("CHANNEL.TEXT.DELETE")
41 .setTitle("Stats Channel")
42 .setDescription("The channel you provided is not a valid channel")
43 .setStatus("Danger")
44 ]})
45 }
46 channel = channel as Discord.TextChannel
pineafane23c4ec2022-07-27 21:56:27 +010047 if (channel.guild.id !== interaction.guild.id) {
pineafan708692b2022-07-24 22:16:22 +010048 return interaction.editReply({embeds: [new EmojiEmbed()
49 .setTitle("Stats Channel")
50 .setDescription(`You must choose a channel in this server`)
51 .setStatus("Danger")
52 .setEmoji("CHANNEL.TEXT.DELETE")
53 ]});
54 }
55 let newName = await convertCurlyBracketString(interaction.options.getString("name"), null, null, interaction.guild.name, interaction.guild.members)
56 if (interaction.options.getChannel("channel").type === "GUILD_TEXT") {
57 newName = newName.toLowerCase().replace(/[\s]/g, "-")
58 }
59 let confirmation = await new confirmationMessage(interaction)
60 .setEmoji("CHANNEL.TEXT.EDIT")
61 .setTitle("Stats Channel")
pineafane23c4ec2022-07-27 21:56:27 +010062 .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 +010063 .setColor("Warning")
64 .setInverted(true)
65 .send(true)
66 if (confirmation.cancelled) return
67 if (confirmation.success) {
68 try {
69 let name = interaction.options.getString("name")
70 let channel = interaction.options.getChannel("channel")
71 await client.database.guilds.write(interaction.guild.id, {[`stats.${channel.id}`]: {name: name, enabled: true}});
72 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
73 try {
74 let data = {
75 meta:{
76 type: 'statsChannelUpdate',
77 displayName: 'Stats Channel Updated',
78 calculateType: 'nucleusSettingsUpdated',
79 color: NucleusColors.yellow,
80 emoji: "CHANNEL.TEXT.EDIT",
81 timestamp: new Date().getTime()
82 },
83 list: {
84 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
85 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
86 channel: entry(channel.id, renderChannel(channel)),
87 name: entry(interaction.options.getString("name"), `\`${interaction.options.getString("name")}\``)
88 },
89 hidden: {
90 guild: interaction.guild.id
91 }
92 }
93 log(data);
94 } catch {}
95 } catch (e) {
96 console.log(e)
97 return interaction.editReply({embeds: [new EmojiEmbed()
98 .setTitle("Stats Channel")
99 .setDescription(`Something went wrong and the stats channel could not be set`)
100 .setStatus("Danger")
101 .setEmoji("CHANNEL.TEXT.DELETE")
102 ], components: []});
103 }
104 } else {
105 return interaction.editReply({embeds: [new EmojiEmbed()
106 .setTitle("Stats Channel")
107 .setDescription(`No changes were made`)
108 .setStatus("Success")
109 .setEmoji("CHANNEL.TEXT.CREATE")
110 ], components: []});
111 }
112 await statsChannelAddCallback(client, interaction.member);
pineafan0bc04162022-07-25 17:22:26 +0100113 }
114 while (true) {
pineafane23c4ec2022-07-27 21:56:27 +0100115 config = await client.database.guilds.read(interaction.guild.id);
pineafan0bc04162022-07-25 17:22:26 +0100116 let stats = config.getKey("stats")
117 let selectMenu = new MessageSelectMenu()
118 .setCustomId("remove")
119 .setMinValues(1)
120 .setMaxValues(Math.max(1, Object.keys(stats).length))
121 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan708692b2022-07-24 22:16:22 +0100122 .setTitle("Stats Channel")
pineafan0bc04162022-07-25 17:22:26 +0100123 .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 +0100124 .setStatus("Success")
125 .setEmoji("CHANNEL.TEXT.CREATE")
pineafan0bc04162022-07-25 17:22:26 +0100126 ], components: [
127 new MessageActionRow().addComponents(Object.keys(stats).length ? [
128 selectMenu.setPlaceholder("Select a stats channel to remove, stopping it updating").addOptions(Object.keys(stats).map(key => ({
129 label: interaction.guild.channels.cache.get(key).name,
130 value: key,
131 description: `${stats[key].name}`,
132 })))
133 ] : [selectMenu.setPlaceholder("The server has no stats channels").setDisabled(true).setOptions([
134 {label: "*Placeholder*", value: "placeholder", description: "No stats channels"}
135 ])])
136 ]})
137 let i;
138 try {
139 i = await m.awaitMessageComponent({ time: 300000 });
140 } catch (e) { break }
141 i.deferUpdate()
142 if (i.customId === "remove") {
143 let toRemove = i.values;
pineafane23c4ec2022-07-27 21:56:27 +0100144 await client.database.guilds.write(interaction.guild.id, null, toRemove.map(k => `stats.${k}`));
pineafan0bc04162022-07-25 17:22:26 +0100145 }
pineafan708692b2022-07-24 22:16:22 +0100146 }
pineafan41d93562022-07-30 22:10:15 +0100147 await interaction.editReply({embeds: [m.embeds[0].setFooter({text: "Message closed"})], components: []});
pineafan708692b2022-07-24 22:16:22 +0100148}
149
150const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
151 let member = (interaction.member as Discord.GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +0100152 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command"
pineafan708692b2022-07-24 22:16:22 +0100153 return true;
154}
155
156export { command };
157export { callback };
pineafan0bc04162022-07-25 17:22:26 +0100158export { check };