blob: 00d2411931b4a1d60ff44816d2e5fd0879e7a5e8 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
2import { ChannelType } from "discord-api-types";
Skyler Grey75ea9172022-08-06 10:22:23 +01003import Discord, {
4 CommandInteraction,
5 MessageActionRow,
6 MessageButton
7} from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01008import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
pineafan6702cef2022-06-13 17:52:37 +01009import confirmationMessage from "../../../utils/confirmationMessage.js";
10import getEmojiByName from "../../../utils/getEmojiByName.js";
11import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
12import { WrappedCheck } from "jshaiku";
13import client from "../../../utils/client.js";
14
15const command = (builder: SlashCommandSubcommandBuilder) =>
16 builder
pineafan63fc5e22022-08-04 22:04:10 +010017 .setName("channel")
18 .setDescription("Sets or shows the log channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010019 .addChannelOption((option) =>
20 option
21 .setName("channel")
22 .setDescription("The channel to set the log channel to")
23 .addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText])
24 );
pineafan6702cef2022-06-13 17:52:37 +010025
Skyler Grey75ea9172022-08-06 10:22:23 +010026const callback = async (
27 interaction: CommandInteraction
28): Promise<void | unknown> => {
29 const m = (await interaction.reply({
30 embeds: LoadingEmbed,
31 ephemeral: true,
32 fetchReply: true
33 })) as Discord.Message;
pineafan6702cef2022-06-13 17:52:37 +010034 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010035 let channel;
pineafan6702cef2022-06-13 17:52:37 +010036 try {
pineafan63fc5e22022-08-04 22:04:10 +010037 channel = interaction.options.getChannel("channel");
pineafan6702cef2022-06-13 17:52:37 +010038 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010039 return await interaction.editReply({
40 embeds: [
41 new EmojiEmbed()
42 .setEmoji("CHANNEL.TEXT.DELETE")
43 .setTitle("Log Channel")
44 .setDescription(
45 "The channel you provided is not a valid channel"
46 )
47 .setStatus("Danger")
48 ]
49 });
pineafan6702cef2022-06-13 17:52:37 +010050 }
pineafan63fc5e22022-08-04 22:04:10 +010051 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010052 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010053 return interaction.editReply({
54 embeds: [
55 new EmojiEmbed()
56 .setTitle("Log Channel")
57 .setDescription(
58 "You must choose a channel in this server"
59 )
60 .setStatus("Danger")
61 .setEmoji("CHANNEL.TEXT.DELETE")
62 ]
63 });
pineafan6702cef2022-06-13 17:52:37 +010064 }
pineafan63fc5e22022-08-04 22:04:10 +010065 const confirmation = await new confirmationMessage(interaction)
pineafan6702cef2022-06-13 17:52:37 +010066 .setEmoji("CHANNEL.TEXT.EDIT")
67 .setTitle("Log Channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010068 .setDescription(
69 `Are you sure you want to set the log channel to <#${channel.id}>?`
70 )
pineafan6702cef2022-06-13 17:52:37 +010071 .setColor("Warning")
72 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010073 .send(true);
74 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010075 if (confirmation.success) {
76 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010077 await client.database.guilds.write(interaction.guild.id, {
78 "logging.logs.channel": channel.id
79 });
80 const { log, NucleusColors, entry, renderUser, renderChannel } =
81 client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010082 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010083 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010084 type: "logChannelUpdate",
85 displayName: "Log Channel Changed",
86 calculateType: "nucleusSettingsUpdated",
87 color: NucleusColors.yellow,
88 emoji: "CHANNEL.TEXT.EDIT",
89 timestamp: new Date().getTime()
90 },
91 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010092 memberId: entry(
93 interaction.user.id,
94 `\`${interaction.user.id}\``
95 ),
96 changedBy: entry(
97 interaction.user.id,
98 renderUser(interaction.user)
99 ),
pineafan63fc5e22022-08-04 22:04:10 +0100100 channel: entry(channel.id, renderChannel(channel))
101 },
102 hidden: {
103 guild: channel.guild.id
pineafanda6e5342022-07-03 10:03:16 +0100104 }
pineafan63fc5e22022-08-04 22:04:10 +0100105 };
106 log(data);
pineafan6702cef2022-06-13 17:52:37 +0100107 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100108 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 return interaction.editReply({
110 embeds: [
111 new EmojiEmbed()
112 .setTitle("Log Channel")
113 .setDescription(
114 "Something went wrong and the log channel could not be set"
115 )
116 .setStatus("Danger")
117 .setEmoji("CHANNEL.TEXT.DELETE")
118 ],
119 components: []
120 });
pineafan6702cef2022-06-13 17:52:37 +0100121 }
122 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 return interaction.editReply({
124 embeds: [
125 new EmojiEmbed()
126 .setTitle("Log Channel")
127 .setDescription("No changes were made")
128 .setStatus("Success")
129 .setEmoji("CHANNEL.TEXT.CREATE")
130 ],
131 components: []
132 });
pineafan6702cef2022-06-13 17:52:37 +0100133 }
134 }
135 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100136 const data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +0100137 let channel = data.logging.logs.channel;
138 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 await interaction.editReply({
140 embeds: [
141 new EmojiEmbed()
142 .setTitle("Log channel")
143 .setDescription(
144 channel
145 ? `Your log channel is currently set to <#${channel}>`
146 : "This server does not have a log channel"
147 )
148 .setStatus("Success")
149 .setEmoji("CHANNEL.TEXT.CREATE")
150 ],
151 components: [
152 new MessageActionRow().addComponents([
153 new MessageButton()
154 .setCustomId("clear")
155 .setLabel(
156 clicks ? "Click again to confirm" : "Reset channel"
157 )
158 .setEmoji(
159 getEmojiByName(
160 clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS",
161 "id"
162 )
163 )
164 .setStyle("DANGER")
165 .setDisabled(!channel)
166 ])
167 ]
168 });
pineafan6702cef2022-06-13 17:52:37 +0100169 let i;
170 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100171 i = await m.awaitMessageComponent({ time: 300000 });
172 } catch (e) {
173 break;
174 }
pineafan63fc5e22022-08-04 22:04:10 +0100175 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100176 if (i.component.customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100177 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100178 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100179 clicks = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100180 await client.database.guilds.write(interaction.guild.id, null, [
181 "logging.logs.channel"
182 ]);
pineafan6702cef2022-06-13 17:52:37 +0100183 channel = undefined;
184 }
185 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100186 break;
pineafan6702cef2022-06-13 17:52:37 +0100187 }
188 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100189 await interaction.editReply({
190 embeds: [
191 new EmojiEmbed()
192 .setTitle("Log channel")
193 .setDescription(
194 channel
195 ? `Your log channel is currently set to <#${channel}>`
196 : "This server does not have a log channel"
197 )
198 .setStatus("Success")
199 .setEmoji("CHANNEL.TEXT.CREATE")
200 .setFooter({ text: "Message closed" })
201 ],
202 components: [
203 new MessageActionRow().addComponents([
204 new MessageButton()
205 .setCustomId("clear")
206 .setLabel("Clear")
207 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
208 .setStyle("SECONDARY")
209 .setDisabled(true)
210 ])
211 ]
212 });
pineafan63fc5e22022-08-04 22:04:10 +0100213};
pineafan6702cef2022-06-13 17:52:37 +0100214
Skyler Grey75ea9172022-08-06 10:22:23 +0100215const check = (
216 interaction: CommandInteraction,
217 _defaultCheck: WrappedCheck
218) => {
219 const member = interaction.member as Discord.GuildMember;
220 if (!member.permissions.has("MANAGE_GUILD"))
221 throw "You must have the *Manage Server* permission to use this command";
pineafan6702cef2022-06-13 17:52:37 +0100222 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100223};
pineafan6702cef2022-06-13 17:52:37 +0100224
225export { command };
226export { callback };
227export { check };