blob: 8e72651e0e01b169a6ddde8b635a5e02aec638c2 [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";
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
Skyler Grey11236ba2022-08-08 21:13:33 +010023const callback = async (interaction: CommandInteraction): Promise<void | 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)
pineafan708692b2022-07-24 22:16:22 +010057 .setEmoji("CHANNEL.TEXT.EDIT")
58 .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;
120 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 await interaction.editReply({
122 embeds: [
123 new EmojiEmbed()
124 .setTitle("Attachment Log Channel")
125 .setDescription(
126 channel
127 ? `Your attachment log channel is currently set to <#${channel}>`
128 : "This server does not have an attachment log channel" +
Skyler Grey11236ba2022-08-08 21:13:33 +0100129 (client.database.premium.hasPremium(interaction.guild.id)
Skyler Grey75ea9172022-08-06 10:22:23 +0100130 ? ""
131 : "\n\nThis server does not have premium, so this feature is disabled")
132 )
133 .setStatus("Success")
134 .setEmoji("CHANNEL.TEXT.CREATE")
135 ],
136 components: [
137 new MessageActionRow().addComponents([
138 new MessageButton()
139 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100140 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
141 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 .setStyle("DANGER")
143 .setDisabled(!channel)
144 ])
145 ]
146 });
pineafan708692b2022-07-24 22:16:22 +0100147 let i;
148 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 i = await m.awaitMessageComponent({ time: 300000 });
150 } catch (e) {
151 break;
152 }
pineafan63fc5e22022-08-04 22:04:10 +0100153 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100154 if (i.component.customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100155 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100156 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100157 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100158 await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100159 channel = undefined;
160 }
161 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100162 break;
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: [
179 new MessageActionRow().addComponents([
180 new MessageButton()
181 .setCustomId("clear")
182 .setLabel("Clear")
183 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
184 .setStyle("SECONDARY")
185 .setDisabled(true)
186 ])
187 ]
188 });
pineafan63fc5e22022-08-04 22:04:10 +0100189};
pineafan708692b2022-07-24 22:16:22 +0100190
Skyler Grey11236ba2022-08-08 21:13:33 +0100191const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 const member = interaction.member as Discord.GuildMember;
193 if (!member.permissions.has("MANAGE_GUILD"))
194 throw "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 };