blob: 992491a4484ece810b43be55a0e518555f61ac6a [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LoadingEmbed } from "../../../utils/defaults.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +01002import { ChannelType } from "discord-api-types/v9";
TheCodedProf327016b2023-01-18 21:48:00 -05003import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle, ButtonInteraction, ButtonComponent } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
pineafan6702cef2022-06-13 17:52:37 +01005import confirmationMessage from "../../../utils/confirmationMessage.js";
6import getEmojiByName from "../../../utils/getEmojiByName.js";
TheCodedProf327016b2023-01-18 21:48:00 -05007import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan6702cef2022-06-13 17:52:37 +01008import client from "../../../utils/client.js";
9
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("channel")
13 .setDescription("Sets or shows the log channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010014 .addChannelOption((option) =>
15 option
16 .setName("channel")
17 .setDescription("The channel to set the log channel to")
PineaFan64486c42022-12-28 09:21:04 +000018 .addChannelTypes(ChannelType.GuildText)
Skyler Grey75ea9172022-08-06 10:22:23 +010019 );
pineafan6702cef2022-06-13 17:52:37 +010020
pineafan3a02ea32022-08-11 21:35:04 +010021const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey75ea9172022-08-06 10:22:23 +010022 const m = (await interaction.reply({
23 embeds: LoadingEmbed,
24 ephemeral: true,
25 fetchReply: true
26 })) as Discord.Message;
TheCodedProf327016b2023-01-18 21:48:00 -050027 if (interaction.options.get("channel")?.channel) {
pineafan63fc5e22022-08-04 22:04:10 +010028 let channel;
pineafan6702cef2022-06-13 17:52:37 +010029 try {
TheCodedProf327016b2023-01-18 21:48:00 -050030 channel = interaction.options.get("channel")?.channel;
pineafan6702cef2022-06-13 17:52:37 +010031 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010032 return await interaction.editReply({
33 embeds: [
34 new EmojiEmbed()
35 .setEmoji("CHANNEL.TEXT.DELETE")
36 .setTitle("Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010037 .setDescription("The channel you provided is not a valid channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010038 .setStatus("Danger")
39 ]
40 });
pineafan6702cef2022-06-13 17:52:37 +010041 }
pineafan63fc5e22022-08-04 22:04:10 +010042 channel = channel as Discord.TextChannel;
TheCodedProf327016b2023-01-18 21:48:00 -050043 if (channel.guild.id !== interaction.guild!.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010044 return interaction.editReply({
45 embeds: [
46 new EmojiEmbed()
47 .setTitle("Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010048 .setDescription("You must choose a channel in this server")
Skyler Grey75ea9172022-08-06 10:22:23 +010049 .setStatus("Danger")
50 .setEmoji("CHANNEL.TEXT.DELETE")
51 ]
52 });
pineafan6702cef2022-06-13 17:52:37 +010053 }
pineafan63fc5e22022-08-04 22:04:10 +010054 const confirmation = await new confirmationMessage(interaction)
TheCodedProf327016b2023-01-18 21:48:00 -050055 .setEmoji("CHANNEL.TEXT.EDIT")
pineafan6702cef2022-06-13 17:52:37 +010056 .setTitle("Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010057 .setDescription(`Are you sure you want to set the log channel to <#${channel.id}>?`)
pineafan6702cef2022-06-13 17:52:37 +010058 .setColor("Warning")
TheCodedProf327016b2023-01-18 21:48:00 -050059 .setFailedMessage("The log channel was not changed", "Danger", "CHANNEL.TEXT.DELETE")
pineafan6702cef2022-06-13 17:52:37 +010060 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010061 .send(true);
62 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010063 if (confirmation.success) {
64 try {
TheCodedProf327016b2023-01-18 21:48:00 -050065 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +010066 "logging.logs.channel": channel.id
67 });
Skyler Grey11236ba2022-08-08 21:13:33 +010068 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010069 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010070 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010071 type: "logChannelUpdate",
72 displayName: "Log Channel Changed",
73 calculateType: "nucleusSettingsUpdated",
74 color: NucleusColors.yellow,
75 emoji: "CHANNEL.TEXT.EDIT",
76 timestamp: new Date().getTime()
77 },
78 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +010079 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
80 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010081 channel: entry(channel.id, renderChannel(channel))
82 },
83 hidden: {
84 guild: channel.guild.id
pineafanda6e5342022-07-03 10:03:16 +010085 }
pineafan63fc5e22022-08-04 22:04:10 +010086 };
87 log(data);
pineafan6702cef2022-06-13 17:52:37 +010088 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010089 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +010090 return interaction.editReply({
91 embeds: [
92 new EmojiEmbed()
93 .setTitle("Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010094 .setDescription("Something went wrong and the log channel could not be set")
Skyler Grey75ea9172022-08-06 10:22:23 +010095 .setStatus("Danger")
96 .setEmoji("CHANNEL.TEXT.DELETE")
97 ],
98 components: []
99 });
pineafan6702cef2022-06-13 17:52:37 +0100100 }
101 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 return interaction.editReply({
103 embeds: [
104 new EmojiEmbed()
105 .setTitle("Log Channel")
106 .setDescription("No changes were made")
107 .setStatus("Success")
108 .setEmoji("CHANNEL.TEXT.CREATE")
109 ],
110 components: []
111 });
pineafan6702cef2022-06-13 17:52:37 +0100112 }
113 }
114 let clicks = 0;
TheCodedProf327016b2023-01-18 21:48:00 -0500115 const data = await client.database.guilds.read(interaction.guild!.id);
pineafan6702cef2022-06-13 17:52:37 +0100116 let channel = data.logging.logs.channel;
Skyler Greyad002172022-08-16 18:48:26 +0100117 let timedOut = false;
118 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100119 await interaction.editReply({
120 embeds: [
121 new EmojiEmbed()
122 .setTitle("Log channel")
123 .setDescription(
124 channel
125 ? `Your log channel is currently set to <#${channel}>`
126 : "This server does not have a log channel"
127 )
128 .setStatus("Success")
129 .setEmoji("CHANNEL.TEXT.CREATE")
130 ],
131 components: [
TheCodedProf327016b2023-01-18 21:48:00 -0500132 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400133 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100135 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
136 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400137 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100138 .setDisabled(!channel)
139 ])
140 ]
141 });
TheCodedProf327016b2023-01-18 21:48:00 -0500142 let i: ButtonInteraction;
pineafan6702cef2022-06-13 17:52:37 +0100143 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000144 i = await m.awaitMessageComponent({
145 time: 300000,
146 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id }
TheCodedProf327016b2023-01-18 21:48:00 -0500147 }) as ButtonInteraction;
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100149 timedOut = true;
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 }
TheCodedProf327016b2023-01-18 21:48:00 -0500151 i = i!
pineafan63fc5e22022-08-04 22:04:10 +0100152 i.deferUpdate();
TheCodedProf327016b2023-01-18 21:48:00 -0500153 if ((i.component as ButtonComponent).customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100154 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100155 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100156 clicks = 0;
TheCodedProf327016b2023-01-18 21:48:00 -0500157 await client.database.guilds.write(interaction.guild!.id, null, ["logging.logs.channel"]);
158 channel = null;
pineafan6702cef2022-06-13 17:52:37 +0100159 }
pineafan6702cef2022-06-13 17:52:37 +0100160 }
161 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100162 await interaction.editReply({
163 embeds: [
164 new EmojiEmbed()
165 .setTitle("Log channel")
166 .setDescription(
167 channel
168 ? `Your log channel is currently set to <#${channel}>`
169 : "This server does not have a log channel"
170 )
171 .setStatus("Success")
172 .setEmoji("CHANNEL.TEXT.CREATE")
173 .setFooter({ text: "Message closed" })
174 ],
175 components: [
TheCodedProf327016b2023-01-18 21:48:00 -0500176 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400177 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100178 .setCustomId("clear")
179 .setLabel("Clear")
180 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400181 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 .setDisabled(true)
183 ])
184 ]
185 });
pineafan63fc5e22022-08-04 22:04:10 +0100186};
pineafan6702cef2022-06-13 17:52:37 +0100187
PineaFan64486c42022-12-28 09:21:04 +0000188const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100189 const member = interaction.member as Discord.GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000190 if (!member.permissions.has("ManageGuild"))
191 return "You must have the *Manage Server* permission to use this command";
pineafan6702cef2022-06-13 17:52:37 +0100192 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100193};
pineafan6702cef2022-06-13 17:52:37 +0100194
195export { command };
196export { callback };
197export { check };