blob: c645581bbb30d60c5c7e56ee8c9735682af447c8 [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;
117 while (true) {
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: [
131 new MessageActionRow().addComponents([
132 new MessageButton()
133 .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"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 .setStyle("DANGER")
137 .setDisabled(!channel)
138 ])
139 ]
140 });
pineafan6702cef2022-06-13 17:52:37 +0100141 let i;
142 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 i = await m.awaitMessageComponent({ time: 300000 });
144 } catch (e) {
145 break;
146 }
pineafan63fc5e22022-08-04 22:04:10 +0100147 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100148 if (i.component.customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100149 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100150 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100151 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100152 await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"]);
pineafan6702cef2022-06-13 17:52:37 +0100153 channel = undefined;
154 }
155 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100156 break;
pineafan6702cef2022-06-13 17:52:37 +0100157 }
158 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100159 await interaction.editReply({
160 embeds: [
161 new EmojiEmbed()
162 .setTitle("Log channel")
163 .setDescription(
164 channel
165 ? `Your log channel is currently set to <#${channel}>`
166 : "This server does not have a log channel"
167 )
168 .setStatus("Success")
169 .setEmoji("CHANNEL.TEXT.CREATE")
170 .setFooter({ text: "Message closed" })
171 ],
172 components: [
173 new MessageActionRow().addComponents([
174 new MessageButton()
175 .setCustomId("clear")
176 .setLabel("Clear")
177 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
178 .setStyle("SECONDARY")
179 .setDisabled(true)
180 ])
181 ]
182 });
pineafan63fc5e22022-08-04 22:04:10 +0100183};
pineafan6702cef2022-06-13 17:52:37 +0100184
Skyler Grey11236ba2022-08-08 21:13:33 +0100185const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 const member = interaction.member as Discord.GuildMember;
187 if (!member.permissions.has("MANAGE_GUILD"))
pineafan3a02ea32022-08-11 21:35:04 +0100188 throw new Error("You must have the *Manage Server* permission to use this command");
pineafan6702cef2022-06-13 17:52:37 +0100189 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100190};
pineafan6702cef2022-06-13 17:52:37 +0100191
192export { command };
193export { callback };
194export { check };