blob: 2709beecdd9f989f0ee7cf2bb0a457369bcf9e2a [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";
TheCodedProfc25369c2023-01-18 21:41:41 -05003import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle, ButtonInteraction } 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;
TheCodedProfc25369c2023-01-18 21:41:41 -050028 if (interaction.options.get("channel")?.channel) {
pineafan63fc5e22022-08-04 22:04:10 +010029 let channel;
pineafan708692b2022-07-24 22:16:22 +010030 try {
TheCodedProfc25369c2023-01-18 21:41:41 -050031 channel = interaction.options.get("channel")?.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;
TheCodedProfc25369c2023-01-18 21:41:41 -050044 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)
TheCodedProfc25369c2023-01-18 21:41:41 -050056 .setEmoji("CHANNEL.TEXT.EDIT")
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")
TheCodedProfc25369c2023-01-18 21:41:41 -050063 .setFailedMessage("Attachment log channel not set", "Warning", "CHANNEL.TEXT.DELETE")
pineafan708692b2022-07-24 22:16:22 +010064 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010065 .send(true);
66 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010067 if (confirmation.success) {
68 try {
TheCodedProfc25369c2023-01-18 21:41:41 -050069 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +010070 "logging.attachments.channel": channel.id
71 });
Skyler Grey11236ba2022-08-08 21:13:33 +010072 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010073 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010074 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010075 type: "attachmentChannelUpdate",
76 displayName: "Attachment Log Channel Updated",
77 calculateType: "nucleusSettingsUpdated",
78 color: NucleusColors.yellow,
79 emoji: "CHANNEL.TEXT.EDIT",
80 timestamp: new Date().getTime()
81 },
82 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +010083 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
84 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010085 channel: entry(channel.id, renderChannel(channel))
86 },
87 hidden: {
TheCodedProfc25369c2023-01-18 21:41:41 -050088 guild: interaction.guild!.id
pineafan708692b2022-07-24 22:16:22 +010089 }
pineafan63fc5e22022-08-04 22:04:10 +010090 };
91 log(data);
pineafan708692b2022-07-24 22:16:22 +010092 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010093 return interaction.editReply({
94 embeds: [
95 new EmojiEmbed()
96 .setTitle("Attachment Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010097 .setDescription("Something went wrong and the attachment log channel could not be set")
Skyler Grey75ea9172022-08-06 10:22:23 +010098 .setStatus("Danger")
99 .setEmoji("CHANNEL.TEXT.DELETE")
100 ],
101 components: []
102 });
pineafan708692b2022-07-24 22:16:22 +0100103 }
104 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 return interaction.editReply({
106 embeds: [
107 new EmojiEmbed()
108 .setTitle("Attachment Log Channel")
109 .setDescription("No changes were made")
110 .setStatus("Success")
111 .setEmoji("CHANNEL.TEXT.CREATE")
112 ],
113 components: []
114 });
pineafan708692b2022-07-24 22:16:22 +0100115 }
116 }
117 let clicks = 0;
TheCodedProfc25369c2023-01-18 21:41:41 -0500118 const data = await client.database.guilds.read(interaction.guild!.id);
pineafan708692b2022-07-24 22:16:22 +0100119 let channel = data.logging.staff.channel;
Skyler Greyad002172022-08-16 18:48:26 +0100120
121 let timedOut = false;
122 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 await interaction.editReply({
124 embeds: [
125 new EmojiEmbed()
126 .setTitle("Attachment Log Channel")
127 .setDescription(
128 channel
129 ? `Your attachment log channel is currently set to <#${channel}>`
130 : "This server does not have an attachment log channel" +
TheCodedProfc25369c2023-01-18 21:41:41 -0500131 (await client.database.premium.hasPremium(interaction.guild!.id)
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 ? ""
133 : "\n\nThis server does not have premium, so this feature is disabled")
134 )
135 .setStatus("Success")
136 .setEmoji("CHANNEL.TEXT.CREATE")
137 ],
138 components: [
TheCodedProfc25369c2023-01-18 21:41:41 -0500139 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400140 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100142 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
143 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400144 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 .setDisabled(!channel)
146 ])
147 ]
148 });
pineafan708692b2022-07-24 22:16:22 +0100149 let i;
150 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000151 i = await m.awaitMessageComponent({
152 time: 300000,
153 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id }
154 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100155 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100156 timedOut = true;
157 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100158 }
pineafan63fc5e22022-08-04 22:04:10 +0100159 i.deferUpdate();
TheCodedProfc25369c2023-01-18 21:41:41 -0500160 if ((i.component as unknown as ButtonInteraction).customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100161 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100162 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100163 clicks = 0;
TheCodedProfc25369c2023-01-18 21:41:41 -0500164 await client.database.guilds.write(interaction.guild!.id, null, ["logging.announcements.channel"]);
165 channel = null;
pineafan708692b2022-07-24 22:16:22 +0100166 }
pineafan708692b2022-07-24 22:16:22 +0100167 }
168 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100169 await interaction.editReply({
170 embeds: [
171 new EmojiEmbed()
172 .setTitle("Attachment Log Channel")
173 .setDescription(
174 channel
175 ? `Your attachment log channel is currently set to <#${channel}>`
176 : "This server does not have an attachment log channel"
177 )
178 .setStatus("Success")
179 .setEmoji("CHANNEL.TEXT.CREATE")
180 .setFooter({ text: "Message closed" })
181 ],
182 components: [
TheCodedProfc25369c2023-01-18 21:41:41 -0500183 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400184 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 .setCustomId("clear")
186 .setLabel("Clear")
187 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400188 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100189 .setDisabled(true)
190 ])
191 ]
192 });
pineafan63fc5e22022-08-04 22:04:10 +0100193};
pineafan708692b2022-07-24 22:16:22 +0100194
PineaFan64486c42022-12-28 09:21:04 +0000195const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100196 const member = interaction.member as Discord.GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000197 if (!member.permissions.has("ManageGuild"))
198 return "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100199 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100200};
pineafan708692b2022-07-24 22:16:22 +0100201
202export { command };
203export { callback };
204export { check };