blob: 7d4fef302d63dc3a522204a2fd806f583676a35a [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";
TheCodedProf21c08592022-09-13 14:14:43 -04003import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
pineafan708692b2022-07-24 22:16:22 +01004import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
5import confirmationMessage from "../../../utils/confirmationMessage.js";
6import getEmojiByName from "../../../utils/getEmojiByName.js";
PineaFan64486c42022-12-28 09:21:04 +00007import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan708692b2022-07-24 22:16:22 +01008import client from "../../../utils/client.js";
9
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("attachments")
13 .setDescription("Where attachments should be logged to (Premium only)")
Skyler Grey75ea9172022-08-06 10:22:23 +010014 .addChannelOption((option) =>
15 option
16 .setName("channel")
17 .setDescription("The channel to log attachments in")
PineaFan64486c42022-12-28 09:21:04 +000018 .addChannelTypes(ChannelType.GuildText)
Skyler Grey75ea9172022-08-06 10:22:23 +010019 .setRequired(false)
20 );
pineafan708692b2022-07-24 22:16:22 +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;
pineafan708692b2022-07-24 22:16:22 +010028 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010029 let channel;
pineafan708692b2022-07-24 22:16:22 +010030 try {
pineafan63fc5e22022-08-04 22:04:10 +010031 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +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("Attachment 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 });
pineafan708692b2022-07-24 22:16:22 +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("Attachment 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 });
pineafan708692b2022-07-24 22:16:22 +010054 }
pineafan63fc5e22022-08-04 22:04:10 +010055 const confirmation = await new confirmationMessage(interaction)
pineafan62ce1922022-08-25 20:34:45 +010056 .setEmoji("CHANNEL.TEXT.EDIT", "CHANNEL.TEXT.DELETE")
pineafan708692b2022-07-24 22:16:22 +010057 .setTitle("Attachment Log Channel")
58 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010059 "This will be the channel all attachments will be sent to.\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +010060 `Are you sure you want to set the attachment log channel to <#${channel.id}>?`
pineafan708692b2022-07-24 22:16:22 +010061 )
62 .setColor("Warning")
63 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010064 .send(true);
65 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010066 if (confirmation.success) {
67 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010068 await client.database.guilds.write(interaction.guild.id, {
69 "logging.attachments.channel": channel.id
70 });
Skyler Grey11236ba2022-08-08 21:13:33 +010071 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010072 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010073 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010074 type: "attachmentChannelUpdate",
75 displayName: "Attachment Log Channel Updated",
76 calculateType: "nucleusSettingsUpdated",
77 color: NucleusColors.yellow,
78 emoji: "CHANNEL.TEXT.EDIT",
79 timestamp: new Date().getTime()
80 },
81 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +010082 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
83 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010084 channel: entry(channel.id, renderChannel(channel))
85 },
86 hidden: {
87 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010088 }
pineafan63fc5e22022-08-04 22:04:10 +010089 };
90 log(data);
pineafan708692b2022-07-24 22:16:22 +010091 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010092 return interaction.editReply({
93 embeds: [
94 new EmojiEmbed()
95 .setTitle("Attachment Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010096 .setDescription("Something went wrong and the attachment log channel could not be set")
Skyler Grey75ea9172022-08-06 10:22:23 +010097 .setStatus("Danger")
98 .setEmoji("CHANNEL.TEXT.DELETE")
99 ],
100 components: []
101 });
pineafan708692b2022-07-24 22:16:22 +0100102 }
103 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 return interaction.editReply({
105 embeds: [
106 new EmojiEmbed()
107 .setTitle("Attachment Log Channel")
108 .setDescription("No changes were made")
109 .setStatus("Success")
110 .setEmoji("CHANNEL.TEXT.CREATE")
111 ],
112 components: []
113 });
pineafan708692b2022-07-24 22:16:22 +0100114 }
115 }
116 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100117 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +0100118 let channel = data.logging.staff.channel;
Skyler Greyad002172022-08-16 18:48:26 +0100119
120 let timedOut = false;
121 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 await interaction.editReply({
123 embeds: [
124 new EmojiEmbed()
125 .setTitle("Attachment Log Channel")
126 .setDescription(
127 channel
128 ? `Your attachment log channel is currently set to <#${channel}>`
129 : "This server does not have an attachment log channel" +
Skyler Grey11236ba2022-08-08 21:13:33 +0100130 (client.database.premium.hasPremium(interaction.guild.id)
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 ? ""
132 : "\n\nThis server does not have premium, so this feature is disabled")
133 )
134 .setStatus("Success")
135 .setEmoji("CHANNEL.TEXT.CREATE")
136 ],
137 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400138 new ActionRowBuilder().addComponents([
139 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100141 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
142 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400143 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 .setDisabled(!channel)
145 ])
146 ]
147 });
pineafan708692b2022-07-24 22:16:22 +0100148 let i;
149 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 i = await m.awaitMessageComponent({ time: 300000 });
151 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100152 timedOut = true;
153 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100154 }
pineafan63fc5e22022-08-04 22:04:10 +0100155 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100156 if (i.component.customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100157 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100158 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100159 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100160 await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100161 channel = undefined;
162 }
pineafan708692b2022-07-24 22:16:22 +0100163 }
164 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100165 await interaction.editReply({
166 embeds: [
167 new EmojiEmbed()
168 .setTitle("Attachment Log Channel")
169 .setDescription(
170 channel
171 ? `Your attachment log channel is currently set to <#${channel}>`
172 : "This server does not have an attachment log channel"
173 )
174 .setStatus("Success")
175 .setEmoji("CHANNEL.TEXT.CREATE")
176 .setFooter({ text: "Message closed" })
177 ],
178 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400179 new ActionRowBuilder().addComponents([
180 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100181 .setCustomId("clear")
182 .setLabel("Clear")
183 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400184 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 .setDisabled(true)
186 ])
187 ]
188 });
pineafan63fc5e22022-08-04 22:04:10 +0100189};
pineafan708692b2022-07-24 22:16:22 +0100190
PineaFan64486c42022-12-28 09:21:04 +0000191const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 const member = interaction.member as Discord.GuildMember;
193 if (!member.permissions.has("MANAGE_GUILD"))
pineafan3a02ea32022-08-11 21:35:04 +0100194 throw new Error("You must have the *Manage Server* permission to use this command");
pineafan708692b2022-07-24 22:16:22 +0100195 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100196};
pineafan708692b2022-07-24 22:16:22 +0100197
198export { command };
199export { callback };
200export { check };