blob: f37bd1a4b201b605b992de2f9bb1b1e20e002c5d [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
2import { ChannelType } from "discord-api-types";
Skyler Grey75ea9172022-08-06 10:22:23 +01003import Discord, {
4 CommandInteraction,
5 MessageActionRow,
6 MessageButton
7} from "discord.js";
pineafan708692b2022-07-24 22:16:22 +01008import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
9import confirmationMessage from "../../../utils/confirmationMessage.js";
10import getEmojiByName from "../../../utils/getEmojiByName.js";
11import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
12import { WrappedCheck } from "jshaiku";
13import client from "../../../utils/client.js";
14
15const command = (builder: SlashCommandSubcommandBuilder) =>
16 builder
pineafan63fc5e22022-08-04 22:04:10 +010017 .setName("attachments")
18 .setDescription("Where attachments should be logged to (Premium only)")
Skyler Grey75ea9172022-08-06 10:22:23 +010019 .addChannelOption((option) =>
20 option
21 .setName("channel")
22 .setDescription("The channel to log attachments in")
23 .addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText])
24 .setRequired(false)
25 );
pineafan708692b2022-07-24 22:16:22 +010026
Skyler Grey75ea9172022-08-06 10:22:23 +010027const callback = async (
28 interaction: CommandInteraction
29): Promise<void | unknown> => {
30 const m = (await interaction.reply({
31 embeds: LoadingEmbed,
32 ephemeral: true,
33 fetchReply: true
34 })) as Discord.Message;
pineafan708692b2022-07-24 22:16:22 +010035 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010036 let channel;
pineafan708692b2022-07-24 22:16:22 +010037 try {
pineafan63fc5e22022-08-04 22:04:10 +010038 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010039 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010040 return await interaction.editReply({
41 embeds: [
42 new EmojiEmbed()
43 .setEmoji("CHANNEL.TEXT.DELETE")
44 .setTitle("Attachment Log Channel")
45 .setDescription(
46 "The channel you provided is not a valid channel"
47 )
48 .setStatus("Danger")
49 ]
50 });
pineafan708692b2022-07-24 22:16:22 +010051 }
pineafan63fc5e22022-08-04 22:04:10 +010052 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010053 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010054 return interaction.editReply({
55 embeds: [
56 new EmojiEmbed()
57 .setTitle("Attachment Log Channel")
58 .setDescription(
59 "You must choose a channel in this server"
60 )
61 .setStatus("Danger")
62 .setEmoji("CHANNEL.TEXT.DELETE")
63 ]
64 });
pineafan708692b2022-07-24 22:16:22 +010065 }
pineafan63fc5e22022-08-04 22:04:10 +010066 const confirmation = await new confirmationMessage(interaction)
pineafan708692b2022-07-24 22:16:22 +010067 .setEmoji("CHANNEL.TEXT.EDIT")
68 .setTitle("Attachment Log Channel")
69 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010070 "This will be the channel all attachments will be sent to.\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +010071 `Are you sure you want to set the attachment log channel to <#${channel.id}>?`
pineafan708692b2022-07-24 22:16:22 +010072 )
73 .setColor("Warning")
74 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010075 .send(true);
76 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010077 if (confirmation.success) {
78 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010079 await client.database.guilds.write(interaction.guild.id, {
80 "logging.attachments.channel": channel.id
81 });
82 const { log, NucleusColors, entry, renderUser, renderChannel } =
83 client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010084 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010085 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010086 type: "attachmentChannelUpdate",
87 displayName: "Attachment Log Channel Updated",
88 calculateType: "nucleusSettingsUpdated",
89 color: NucleusColors.yellow,
90 emoji: "CHANNEL.TEXT.EDIT",
91 timestamp: new Date().getTime()
92 },
93 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 memberId: entry(
95 interaction.user.id,
96 `\`${interaction.user.id}\``
97 ),
98 changedBy: entry(
99 interaction.user.id,
100 renderUser(interaction.user)
101 ),
pineafan63fc5e22022-08-04 22:04:10 +0100102 channel: entry(channel.id, renderChannel(channel))
103 },
104 hidden: {
105 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +0100106 }
pineafan63fc5e22022-08-04 22:04:10 +0100107 };
108 log(data);
pineafan708692b2022-07-24 22:16:22 +0100109 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 return interaction.editReply({
111 embeds: [
112 new EmojiEmbed()
113 .setTitle("Attachment Log Channel")
114 .setDescription(
115 "Something went wrong and the attachment log channel could not be set"
116 )
117 .setStatus("Danger")
118 .setEmoji("CHANNEL.TEXT.DELETE")
119 ],
120 components: []
121 });
pineafan708692b2022-07-24 22:16:22 +0100122 }
123 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 return interaction.editReply({
125 embeds: [
126 new EmojiEmbed()
127 .setTitle("Attachment Log Channel")
128 .setDescription("No changes were made")
129 .setStatus("Success")
130 .setEmoji("CHANNEL.TEXT.CREATE")
131 ],
132 components: []
133 });
pineafan708692b2022-07-24 22:16:22 +0100134 }
135 }
136 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100137 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +0100138 let channel = data.logging.staff.channel;
139 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 await interaction.editReply({
141 embeds: [
142 new EmojiEmbed()
143 .setTitle("Attachment Log Channel")
144 .setDescription(
145 channel
146 ? `Your attachment log channel is currently set to <#${channel}>`
147 : "This server does not have an attachment log channel" +
148 (client.database.premium.hasPremium(
149 interaction.guild.id
150 )
151 ? ""
152 : "\n\nThis server does not have premium, so this feature is disabled")
153 )
154 .setStatus("Success")
155 .setEmoji("CHANNEL.TEXT.CREATE")
156 ],
157 components: [
158 new MessageActionRow().addComponents([
159 new MessageButton()
160 .setCustomId("clear")
161 .setLabel(
162 clicks ? "Click again to confirm" : "Reset channel"
163 )
164 .setEmoji(
165 getEmojiByName(
166 clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS",
167 "id"
168 )
169 )
170 .setStyle("DANGER")
171 .setDisabled(!channel)
172 ])
173 ]
174 });
pineafan708692b2022-07-24 22:16:22 +0100175 let i;
176 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 i = await m.awaitMessageComponent({ time: 300000 });
178 } catch (e) {
179 break;
180 }
pineafan63fc5e22022-08-04 22:04:10 +0100181 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100182 if (i.component.customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100183 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100184 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100185 clicks = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 await client.database.guilds.write(interaction.guild.id, null, [
187 "logging.announcements.channel"
188 ]);
pineafan708692b2022-07-24 22:16:22 +0100189 channel = undefined;
190 }
191 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100192 break;
pineafan708692b2022-07-24 22:16:22 +0100193 }
194 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 await interaction.editReply({
196 embeds: [
197 new EmojiEmbed()
198 .setTitle("Attachment Log Channel")
199 .setDescription(
200 channel
201 ? `Your attachment log channel is currently set to <#${channel}>`
202 : "This server does not have an attachment log channel"
203 )
204 .setStatus("Success")
205 .setEmoji("CHANNEL.TEXT.CREATE")
206 .setFooter({ text: "Message closed" })
207 ],
208 components: [
209 new MessageActionRow().addComponents([
210 new MessageButton()
211 .setCustomId("clear")
212 .setLabel("Clear")
213 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
214 .setStyle("SECONDARY")
215 .setDisabled(true)
216 ])
217 ]
218 });
pineafan63fc5e22022-08-04 22:04:10 +0100219};
pineafan708692b2022-07-24 22:16:22 +0100220
Skyler Grey75ea9172022-08-06 10:22:23 +0100221const check = (
222 interaction: CommandInteraction,
223 _defaultCheck: WrappedCheck
224) => {
225 const member = interaction.member as Discord.GuildMember;
226 if (!member.permissions.has("MANAGE_GUILD"))
227 throw "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100228 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100229};
pineafan708692b2022-07-24 22:16:22 +0100230
231export { command };
232export { callback };
233export { check };