blob: 10c301120205829dfbf539ba347f76ef02e526d4 [file] [log] [blame]
pineafan708692b2022-07-24 22:16:22 +01001import { ChannelType } from 'discord-api-types';
2import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
3import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
4import confirmationMessage from "../../../utils/confirmationMessage.js";
5import getEmojiByName from "../../../utils/getEmojiByName.js";
6import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
7import { WrappedCheck } from "jshaiku";
8import client from "../../../utils/client.js";
9import convertCurlyBracketString from '../../../utils/convertCurlyBracketString.js';
10import {callback as statsChannelAddCallback} from "../../../reflex/statsChannelUpdate.js";
11
12const command = (builder: SlashCommandSubcommandBuilder) =>
13 builder
14 .setName("set")
15 .setDescription("Adds or edits a channel which will update when members join or leave")
16 .addChannelOption(option => option.setName("channel").setDescription("The channel to modify").addChannelTypes([
17 ChannelType.GuildNews, ChannelType.GuildText
18 ]).setRequired(true))
19 .addStringOption(option => option.setName("name").setDescription("The channel name").setRequired(true).setAutocomplete(true))
20
21const callback = async (interaction: CommandInteraction): Promise<any> => {
22 console.log(interaction.options.getString("name"))
23 let m;
24 m = await interaction.reply({embeds: [new EmojiEmbed()
25 .setTitle("Loading")
26 .setStatus("Danger")
27 .setEmoji("NUCLEUS.LOADING")
28 ], ephemeral: true, fetchReply: true});
29 if (interaction.options.getChannel("channel")) {
30 let config = client.database.guilds.read(interaction.guild.id);
31 let channel
32 try {
33 channel = interaction.options.getChannel("channel")
34 } catch {
35 return await interaction.editReply({embeds: [new EmojiEmbed()
36 .setEmoji("CHANNEL.TEXT.DELETE")
37 .setTitle("Stats Channel")
38 .setDescription("The channel you provided is not a valid channel")
39 .setStatus("Danger")
40 ]})
41 }
42 channel = channel as Discord.TextChannel
43 if (channel.guild.id != interaction.guild.id) {
44 return interaction.editReply({embeds: [new EmojiEmbed()
45 .setTitle("Stats Channel")
46 .setDescription(`You must choose a channel in this server`)
47 .setStatus("Danger")
48 .setEmoji("CHANNEL.TEXT.DELETE")
49 ]});
50 }
51 let newName = await convertCurlyBracketString(interaction.options.getString("name"), null, null, interaction.guild.name, interaction.guild.members)
52 if (interaction.options.getChannel("channel").type === "GUILD_TEXT") {
53 newName = newName.toLowerCase().replace(/[\s]/g, "-")
54 }
55 let confirmation = await new confirmationMessage(interaction)
56 .setEmoji("CHANNEL.TEXT.EDIT")
57 .setTitle("Stats Channel")
58 .setDescription(`Are you sure you want to set <#${channel.id}> to a stats channel?\n\n*Preview: ${newName}*`)
59 .setColor("Warning")
60 .setInverted(true)
61 .send(true)
62 if (confirmation.cancelled) return
63 if (confirmation.success) {
64 try {
65 let name = interaction.options.getString("name")
66 let channel = interaction.options.getChannel("channel")
67 await client.database.guilds.write(interaction.guild.id, {[`stats.${channel.id}`]: {name: name, enabled: true}});
68 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
69 try {
70 let 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
87 }
88 }
89 log(data);
90 } catch {}
91 } catch (e) {
92 console.log(e)
93 return interaction.editReply({embeds: [new EmojiEmbed()
94 .setTitle("Stats Channel")
95 .setDescription(`Something went wrong and the stats channel could not be set`)
96 .setStatus("Danger")
97 .setEmoji("CHANNEL.TEXT.DELETE")
98 ], components: []});
99 }
100 } else {
101 return interaction.editReply({embeds: [new EmojiEmbed()
102 .setTitle("Stats Channel")
103 .setDescription(`No changes were made`)
104 .setStatus("Success")
105 .setEmoji("CHANNEL.TEXT.CREATE")
106 ], components: []});
107 }
108 await statsChannelAddCallback(client, interaction.member);
109 return interaction.editReply({embeds: [new EmojiEmbed()
110 .setTitle("Stats Channel")
111 .setDescription(`The stats channel has been set to <#${channel.id}>`)
112 .setStatus("Success")
113 .setEmoji("CHANNEL.TEXT.CREATE")
114 ], components: []});
115 }
116}
117
118const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
119 let member = (interaction.member as Discord.GuildMember)
120 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command"
121 return true;
122}
123
124export { command };
125export { callback };
126export { check };