blob: 38f66fb86fd24241273e8b752cd652fa999d5874 [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";
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("attachments")
14 .setDescription("Where attachments should be logged to (Premium only)")
Skyler Grey75ea9172022-08-06 10:22:23 +010015 .addChannelOption((option) =>
16 option
17 .setName("channel")
18 .setDescription("The channel to log attachments in")
19 .addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText])
20 .setRequired(false)
21 );
pineafan708692b2022-07-24 22:16:22 +010022
pineafan3a02ea32022-08-11 21:35:04 +010023const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey75ea9172022-08-06 10:22:23 +010024 const m = (await interaction.reply({
25 embeds: LoadingEmbed,
26 ephemeral: true,
27 fetchReply: true
28 })) as Discord.Message;
pineafan708692b2022-07-24 22:16:22 +010029 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010030 let channel;
pineafan708692b2022-07-24 22:16:22 +010031 try {
pineafan63fc5e22022-08-04 22:04:10 +010032 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010033 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010034 return await interaction.editReply({
35 embeds: [
36 new EmojiEmbed()
37 .setEmoji("CHANNEL.TEXT.DELETE")
38 .setTitle("Attachment Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010039 .setDescription("The channel you provided is not a valid channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010040 .setStatus("Danger")
41 ]
42 });
pineafan708692b2022-07-24 22:16:22 +010043 }
pineafan63fc5e22022-08-04 22:04:10 +010044 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010045 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010046 return interaction.editReply({
47 embeds: [
48 new EmojiEmbed()
49 .setTitle("Attachment Log Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010050 .setDescription("You must choose a channel in this server")
Skyler Grey75ea9172022-08-06 10:22:23 +010051 .setStatus("Danger")
52 .setEmoji("CHANNEL.TEXT.DELETE")
53 ]
54 });
pineafan708692b2022-07-24 22:16:22 +010055 }
pineafan63fc5e22022-08-04 22:04:10 +010056 const confirmation = await new confirmationMessage(interaction)
pineafan62ce1922022-08-25 20:34:45 +010057 .setEmoji("CHANNEL.TEXT.EDIT", "CHANNEL.TEXT.DELETE")
pineafan708692b2022-07-24 22:16:22 +010058 .setTitle("Attachment Log Channel")
59 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010060 "This will be the channel all attachments will be sent to.\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +010061 `Are you sure you want to set the attachment log channel to <#${channel.id}>?`
pineafan708692b2022-07-24 22:16:22 +010062 )
63 .setColor("Warning")
64 .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 {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 await client.database.guilds.write(interaction.guild.id, {
70 "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: {
88 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;
pineafan63fc5e22022-08-04 22:04:10 +0100118 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" +
Skyler Grey11236ba2022-08-08 21:13:33 +0100131 (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: [
TheCodedProf21c08592022-09-13 14:14:43 -0400139 new ActionRowBuilder().addComponents([
140 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 {
Skyler Grey75ea9172022-08-06 10:22:23 +0100151 i = await m.awaitMessageComponent({ time: 300000 });
152 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100153 timedOut = true;
154 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100155 }
pineafan63fc5e22022-08-04 22:04:10 +0100156 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100157 if (i.component.customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100158 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100159 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100160 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100161 await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100162 channel = undefined;
163 }
pineafan708692b2022-07-24 22:16:22 +0100164 }
165 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100166 await interaction.editReply({
167 embeds: [
168 new EmojiEmbed()
169 .setTitle("Attachment Log Channel")
170 .setDescription(
171 channel
172 ? `Your attachment log channel is currently set to <#${channel}>`
173 : "This server does not have an attachment log channel"
174 )
175 .setStatus("Success")
176 .setEmoji("CHANNEL.TEXT.CREATE")
177 .setFooter({ text: "Message closed" })
178 ],
179 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400180 new ActionRowBuilder().addComponents([
181 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 .setCustomId("clear")
183 .setLabel("Clear")
184 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400185 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 .setDisabled(true)
187 ])
188 ]
189 });
pineafan63fc5e22022-08-04 22:04:10 +0100190};
pineafan708692b2022-07-24 22:16:22 +0100191
Skyler Grey11236ba2022-08-08 21:13:33 +0100192const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100193 const member = interaction.member as Discord.GuildMember;
194 if (!member.permissions.has("MANAGE_GUILD"))
pineafan3a02ea32022-08-11 21:35:04 +0100195 throw new Error("You must have the *Manage Server* permission to use this command");
pineafan708692b2022-07-24 22:16:22 +0100196 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100197};
pineafan708692b2022-07-24 22:16:22 +0100198
199export { command };
200export { callback };
201export { check };