blob: cb965673b08d46e4ea89c3c1162ef0d8aae30d2a [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
2import { ChannelType } from "discord-api-types";
pineafan708692b2022-07-24 22:16:22 +01003import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
4import 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)")
15 .addChannelOption(option => option.setName("channel").setDescription("The channel to log attachments in").addChannelTypes([
16 ChannelType.GuildNews, ChannelType.GuildText
17 ]).setRequired(false));
pineafan708692b2022-07-24 22:16:22 +010018
pineafanbd02b4a2022-08-05 22:01:38 +010019const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
20 const m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}) as Discord.Message;
pineafan708692b2022-07-24 22:16:22 +010021 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010022 let channel;
pineafan708692b2022-07-24 22:16:22 +010023 try {
pineafan63fc5e22022-08-04 22:04:10 +010024 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010025 } catch {
26 return await interaction.editReply({embeds: [new EmojiEmbed()
27 .setEmoji("CHANNEL.TEXT.DELETE")
28 .setTitle("Attachment Log Channel")
29 .setDescription("The channel you provided is not a valid channel")
30 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010031 ]});
pineafan708692b2022-07-24 22:16:22 +010032 }
pineafan63fc5e22022-08-04 22:04:10 +010033 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010034 if (channel.guild.id !== interaction.guild.id) {
pineafan708692b2022-07-24 22:16:22 +010035 return interaction.editReply({embeds: [new EmojiEmbed()
36 .setTitle("Attachment Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010037 .setDescription("You must choose a channel in this server")
pineafan708692b2022-07-24 22:16:22 +010038 .setStatus("Danger")
39 .setEmoji("CHANNEL.TEXT.DELETE")
40 ]});
41 }
pineafan63fc5e22022-08-04 22:04:10 +010042 const confirmation = await new confirmationMessage(interaction)
pineafan708692b2022-07-24 22:16:22 +010043 .setEmoji("CHANNEL.TEXT.EDIT")
44 .setTitle("Attachment Log Channel")
45 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010046 "This will be the channel all attachments will be sent to.\n\n" +
pineafan708692b2022-07-24 22:16:22 +010047 `Are you sure you want to set the attachment log channel to <#${channel.id}>?`
48 )
49 .setColor("Warning")
50 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010051 .send(true);
52 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010053 if (confirmation.success) {
54 try {
pineafan63fc5e22022-08-04 22:04:10 +010055 await client.database.guilds.write(interaction.guild.id, {"logging.attachments.channel": channel.id});
56 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
57 const data = {
58 meta:{
59 type: "attachmentChannelUpdate",
60 displayName: "Attachment Log Channel Updated",
61 calculateType: "nucleusSettingsUpdated",
62 color: NucleusColors.yellow,
63 emoji: "CHANNEL.TEXT.EDIT",
64 timestamp: new Date().getTime()
65 },
66 list: {
67 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
68 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
69 channel: entry(channel.id, renderChannel(channel))
70 },
71 hidden: {
72 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010073 }
pineafan63fc5e22022-08-04 22:04:10 +010074 };
75 log(data);
pineafan708692b2022-07-24 22:16:22 +010076 } catch (e) {
77 return interaction.editReply({embeds: [new EmojiEmbed()
78 .setTitle("Attachment Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010079 .setDescription("Something went wrong and the attachment log channel could not be set")
pineafan708692b2022-07-24 22:16:22 +010080 .setStatus("Danger")
81 .setEmoji("CHANNEL.TEXT.DELETE")
82 ], components: []});
83 }
84 } else {
85 return interaction.editReply({embeds: [new EmojiEmbed()
86 .setTitle("Attachment Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010087 .setDescription("No changes were made")
pineafan708692b2022-07-24 22:16:22 +010088 .setStatus("Success")
89 .setEmoji("CHANNEL.TEXT.CREATE")
90 ], components: []});
91 }
92 }
93 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +010094 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +010095 let channel = data.logging.staff.channel;
96 while (true) {
97 await interaction.editReply({embeds: [new EmojiEmbed()
98 .setTitle("Attachment Log Channel")
99 .setDescription(
100 channel ? `Your attachment log channel is currently set to <#${channel}>` : "This server does not have an attachment log channel" +
101 (client.database.premium.hasPremium(interaction.guild.id) ? "" : "\n\nThis server does not have premium, so this feature is disabled")
102 )
103 .setStatus("Success")
104 .setEmoji("CHANNEL.TEXT.CREATE")
105 ], components: [new MessageActionRow().addComponents([new MessageButton()
106 .setCustomId("clear")
107 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
108 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
109 .setStyle("DANGER")
110 .setDisabled(!channel)
111 ])]});
112 let i;
113 try {
114 i = await m.awaitMessageComponent({time: 300000});
pineafan63fc5e22022-08-04 22:04:10 +0100115 } catch(e) { break; }
116 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100117 if (i.component.customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100118 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100119 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100120 clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100121 await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100122 channel = undefined;
123 }
124 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100125 break;
pineafan708692b2022-07-24 22:16:22 +0100126 }
127 }
128 await interaction.editReply({embeds: [new EmojiEmbed()
129 .setTitle("Attachment Log Channel")
130 .setDescription(channel ? `Your attachment log channel is currently set to <#${channel}>` : "This server does not have an attachment log channel")
131 .setStatus("Success")
132 .setEmoji("CHANNEL.TEXT.CREATE")
133 .setFooter({text: "Message closed"})
134 ], components: [new MessageActionRow().addComponents([new MessageButton()
135 .setCustomId("clear")
136 .setLabel("Clear")
137 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
138 .setStyle("SECONDARY")
139 .setDisabled(true)
140 ])]});
pineafan63fc5e22022-08-04 22:04:10 +0100141};
pineafan708692b2022-07-24 22:16:22 +0100142
pineafanbd02b4a2022-08-05 22:01:38 +0100143const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100144 const member = (interaction.member as Discord.GuildMember);
145 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100146 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100147};
pineafan708692b2022-07-24 22:16:22 +0100148
149export { command };
150export { callback };
151export { check };