blob: 91298414e0ceb6f63404ec461a1d5f9ae889c782 [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";
TheCodedProf21c08592022-09-13 14:14:43 -04003import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle } 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";
7import { 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;
pineafan6702cef2022-06-13 17:52:37 +010027 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010028 let channel;
pineafan6702cef2022-06-13 17:52:37 +010029 try {
pineafan63fc5e22022-08-04 22:04:10 +010030 channel = interaction.options.getChannel("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;
pineafane23c4ec2022-07-27 21:56:27 +010043 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)
pineafan62ce1922022-08-25 20:34:45 +010055 .setEmoji("CHANNEL.TEXT.EDIT", "CHANNEL.TEXT.DELETE")
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")
59 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010060 .send(true);
61 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010062 if (confirmation.success) {
63 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010064 await client.database.guilds.write(interaction.guild.id, {
65 "logging.logs.channel": channel.id
66 });
Skyler Grey11236ba2022-08-08 21:13:33 +010067 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010068 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010070 type: "logChannelUpdate",
71 displayName: "Log Channel Changed",
72 calculateType: "nucleusSettingsUpdated",
73 color: NucleusColors.yellow,
74 emoji: "CHANNEL.TEXT.EDIT",
75 timestamp: new Date().getTime()
76 },
77 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +010078 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
79 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010080 channel: entry(channel.id, renderChannel(channel))
81 },
82 hidden: {
83 guild: channel.guild.id
pineafanda6e5342022-07-03 10:03:16 +010084 }
pineafan63fc5e22022-08-04 22:04:10 +010085 };
86 log(data);
pineafan6702cef2022-06-13 17:52:37 +010087 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010088 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +010089 return interaction.editReply({
90 embeds: [
91 new EmojiEmbed()
92 .setTitle("Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010093 .setDescription("Something went wrong and the log channel could not be set")
Skyler Grey75ea9172022-08-06 10:22:23 +010094 .setStatus("Danger")
95 .setEmoji("CHANNEL.TEXT.DELETE")
96 ],
97 components: []
98 });
pineafan6702cef2022-06-13 17:52:37 +010099 }
100 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 return interaction.editReply({
102 embeds: [
103 new EmojiEmbed()
104 .setTitle("Log Channel")
105 .setDescription("No changes were made")
106 .setStatus("Success")
107 .setEmoji("CHANNEL.TEXT.CREATE")
108 ],
109 components: []
110 });
pineafan6702cef2022-06-13 17:52:37 +0100111 }
112 }
113 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100114 const data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +0100115 let channel = data.logging.logs.channel;
Skyler Greyad002172022-08-16 18:48:26 +0100116 let timedOut = false;
117 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 await interaction.editReply({
119 embeds: [
120 new EmojiEmbed()
121 .setTitle("Log channel")
122 .setDescription(
123 channel
124 ? `Your log channel is currently set to <#${channel}>`
125 : "This server does not have a log channel"
126 )
127 .setStatus("Success")
128 .setEmoji("CHANNEL.TEXT.CREATE")
129 ],
130 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400131 new ActionRowBuilder().addComponents([
132 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100134 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
135 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400136 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 .setDisabled(!channel)
138 ])
139 ]
140 });
pineafan6702cef2022-06-13 17:52:37 +0100141 let i;
142 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000143 i = await m.awaitMessageComponent({
144 time: 300000,
145 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id }
146 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100148 timedOut = true;
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 }
pineafan63fc5e22022-08-04 22:04:10 +0100150 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100151 if (i.component.customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100152 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100153 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100154 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100155 await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"]);
pineafan6702cef2022-06-13 17:52:37 +0100156 channel = undefined;
157 }
pineafan6702cef2022-06-13 17:52:37 +0100158 }
159 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 await interaction.editReply({
161 embeds: [
162 new EmojiEmbed()
163 .setTitle("Log channel")
164 .setDescription(
165 channel
166 ? `Your log channel is currently set to <#${channel}>`
167 : "This server does not have a log channel"
168 )
169 .setStatus("Success")
170 .setEmoji("CHANNEL.TEXT.CREATE")
171 .setFooter({ text: "Message closed" })
172 ],
173 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400174 new ActionRowBuilder().addComponents([
175 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 .setCustomId("clear")
177 .setLabel("Clear")
178 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400179 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100180 .setDisabled(true)
181 ])
182 ]
183 });
pineafan63fc5e22022-08-04 22:04:10 +0100184};
pineafan6702cef2022-06-13 17:52:37 +0100185
PineaFan64486c42022-12-28 09:21:04 +0000186const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100187 const member = interaction.member as Discord.GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000188 if (!member.permissions.has("ManageGuild"))
189 return "You must have the *Manage Server* permission to use this command";
pineafan6702cef2022-06-13 17:52:37 +0100190 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100191};
pineafan6702cef2022-06-13 17:52:37 +0100192
193export { command };
194export { callback };
195export { check };