blob: 326246a3748344853cd6d48bb3b407961af35965 [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";
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 {
PineaFan0d06edc2023-01-17 22:10:31 +0000150 i = await m.awaitMessageComponent({
151 time: 300000,
152 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id }
153 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100154 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100155 timedOut = true;
156 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100157 }
pineafan63fc5e22022-08-04 22:04:10 +0100158 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100159 if (i.component.customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100160 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100161 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100162 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100163 await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100164 channel = undefined;
165 }
pineafan708692b2022-07-24 22:16:22 +0100166 }
167 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100168 await interaction.editReply({
169 embeds: [
170 new EmojiEmbed()
171 .setTitle("Attachment Log Channel")
172 .setDescription(
173 channel
174 ? `Your attachment log channel is currently set to <#${channel}>`
175 : "This server does not have an attachment log channel"
176 )
177 .setStatus("Success")
178 .setEmoji("CHANNEL.TEXT.CREATE")
179 .setFooter({ text: "Message closed" })
180 ],
181 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400182 new ActionRowBuilder().addComponents([
183 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 .setCustomId("clear")
185 .setLabel("Clear")
186 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400187 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100188 .setDisabled(true)
189 ])
190 ]
191 });
pineafan63fc5e22022-08-04 22:04:10 +0100192};
pineafan708692b2022-07-24 22:16:22 +0100193
PineaFan64486c42022-12-28 09:21:04 +0000194const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 const member = interaction.member as Discord.GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000196 if (!member.permissions.has("ManageGuild"))
197 return "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100198 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100199};
pineafan708692b2022-07-24 22:16:22 +0100200
201export { command };
202export { callback };
203export { check };