blob: 206d28227d7436764acedd03b362839b1433392b [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +01002import { ChannelType } from "discord-api-types/v9";
Skyler Grey11236ba2022-08-08 21:13:33 +01003import Discord, { CommandInteraction, MessageActionRow, MessageButton } 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";
8import { WrappedCheck } from "jshaiku";
9import client from "../../../utils/client.js";
10
11const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafan63fc5e22022-08-04 22:04:10 +010013 .setName("channel")
14 .setDescription("Sets or shows the log channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010015 .addChannelOption((option) =>
16 option
17 .setName("channel")
18 .setDescription("The channel to set the log channel to")
19 .addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText])
20 );
pineafan6702cef2022-06-13 17:52:37 +010021
pineafan3a02ea32022-08-11 21:35:04 +010022const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey75ea9172022-08-06 10:22:23 +010023 const m = (await interaction.reply({
24 embeds: LoadingEmbed,
25 ephemeral: true,
26 fetchReply: true
27 })) as Discord.Message;
pineafan6702cef2022-06-13 17:52:37 +010028 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010029 let channel;
pineafan6702cef2022-06-13 17:52:37 +010030 try {
pineafan63fc5e22022-08-04 22:04:10 +010031 channel = interaction.options.getChannel("channel");
pineafan6702cef2022-06-13 17:52:37 +010032 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010033 return await interaction.editReply({
34 embeds: [
35 new EmojiEmbed()
36 .setEmoji("CHANNEL.TEXT.DELETE")
37 .setTitle("Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010038 .setDescription("The channel you provided is not a valid channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010039 .setStatus("Danger")
40 ]
41 });
pineafan6702cef2022-06-13 17:52:37 +010042 }
pineafan63fc5e22022-08-04 22:04:10 +010043 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010044 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010045 return interaction.editReply({
46 embeds: [
47 new EmojiEmbed()
48 .setTitle("Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010049 .setDescription("You must choose a channel in this server")
Skyler Grey75ea9172022-08-06 10:22:23 +010050 .setStatus("Danger")
51 .setEmoji("CHANNEL.TEXT.DELETE")
52 ]
53 });
pineafan6702cef2022-06-13 17:52:37 +010054 }
pineafan63fc5e22022-08-04 22:04:10 +010055 const confirmation = await new confirmationMessage(interaction)
pineafan6702cef2022-06-13 17:52:37 +010056 .setEmoji("CHANNEL.TEXT.EDIT")
57 .setTitle("Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010058 .setDescription(`Are you sure you want to set the log channel to <#${channel.id}>?`)
pineafan6702cef2022-06-13 17:52:37 +010059 .setColor("Warning")
60 .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 {
Skyler Grey75ea9172022-08-06 10:22:23 +010065 await client.database.guilds.write(interaction.guild.id, {
66 "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;
pineafan63fc5e22022-08-04 22:04:10 +0100115 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: [
132 new MessageActionRow().addComponents([
133 new MessageButton()
134 .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"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 .setStyle("DANGER")
138 .setDisabled(!channel)
139 ])
140 ]
141 });
pineafan6702cef2022-06-13 17:52:37 +0100142 let i;
143 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 i = await m.awaitMessageComponent({ time: 300000 });
145 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100146 timedOut = true;
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 }
pineafan63fc5e22022-08-04 22:04:10 +0100148 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100149 if (i.component.customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100150 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100151 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100152 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100153 await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"]);
pineafan6702cef2022-06-13 17:52:37 +0100154 channel = undefined;
155 }
pineafan6702cef2022-06-13 17:52:37 +0100156 }
157 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100158 await interaction.editReply({
159 embeds: [
160 new EmojiEmbed()
161 .setTitle("Log channel")
162 .setDescription(
163 channel
164 ? `Your log channel is currently set to <#${channel}>`
165 : "This server does not have a log channel"
166 )
167 .setStatus("Success")
168 .setEmoji("CHANNEL.TEXT.CREATE")
169 .setFooter({ text: "Message closed" })
170 ],
171 components: [
172 new MessageActionRow().addComponents([
173 new MessageButton()
174 .setCustomId("clear")
175 .setLabel("Clear")
176 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
177 .setStyle("SECONDARY")
178 .setDisabled(true)
179 ])
180 ]
181 });
pineafan63fc5e22022-08-04 22:04:10 +0100182};
pineafan6702cef2022-06-13 17:52:37 +0100183
Skyler Grey11236ba2022-08-08 21:13:33 +0100184const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 const member = interaction.member as Discord.GuildMember;
186 if (!member.permissions.has("MANAGE_GUILD"))
pineafan3a02ea32022-08-11 21:35:04 +0100187 throw new Error("You must have the *Manage Server* permission to use this command");
pineafan6702cef2022-06-13 17:52:37 +0100188 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100189};
pineafan6702cef2022-06-13 17:52:37 +0100190
191export { command };
192export { callback };
193export { check };