blob: edd1c612c7f30f8efac02a0a21e15e4ff06481a5 [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
19const callback = async (interaction: CommandInteraction): Promise<any> => {
20 let m;
pineafane23c4ec2022-07-27 21:56:27 +010021 m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true});
pineafan708692b2022-07-24 22:16:22 +010022 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010023 let channel;
pineafan708692b2022-07-24 22:16:22 +010024 try {
pineafan63fc5e22022-08-04 22:04:10 +010025 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010026 } catch {
27 return await interaction.editReply({embeds: [new EmojiEmbed()
28 .setEmoji("CHANNEL.TEXT.DELETE")
29 .setTitle("Attachment Log Channel")
30 .setDescription("The channel you provided is not a valid channel")
31 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010032 ]});
pineafan708692b2022-07-24 22:16:22 +010033 }
pineafan63fc5e22022-08-04 22:04:10 +010034 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010035 if (channel.guild.id !== interaction.guild.id) {
pineafan708692b2022-07-24 22:16:22 +010036 return interaction.editReply({embeds: [new EmojiEmbed()
37 .setTitle("Attachment Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010038 .setDescription("You must choose a channel in this server")
pineafan708692b2022-07-24 22:16:22 +010039 .setStatus("Danger")
40 .setEmoji("CHANNEL.TEXT.DELETE")
41 ]});
42 }
pineafan63fc5e22022-08-04 22:04:10 +010043 const confirmation = await new confirmationMessage(interaction)
pineafan708692b2022-07-24 22:16:22 +010044 .setEmoji("CHANNEL.TEXT.EDIT")
45 .setTitle("Attachment Log Channel")
46 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010047 "This will be the channel all attachments will be sent to.\n\n" +
pineafan708692b2022-07-24 22:16:22 +010048 `Are you sure you want to set the attachment log channel to <#${channel.id}>?`
49 )
50 .setColor("Warning")
51 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010052 .send(true);
53 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010054 if (confirmation.success) {
55 try {
pineafan63fc5e22022-08-04 22:04:10 +010056 await client.database.guilds.write(interaction.guild.id, {"logging.attachments.channel": channel.id});
57 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
58 const data = {
59 meta:{
60 type: "attachmentChannelUpdate",
61 displayName: "Attachment Log Channel Updated",
62 calculateType: "nucleusSettingsUpdated",
63 color: NucleusColors.yellow,
64 emoji: "CHANNEL.TEXT.EDIT",
65 timestamp: new Date().getTime()
66 },
67 list: {
68 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
69 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
70 channel: entry(channel.id, renderChannel(channel))
71 },
72 hidden: {
73 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010074 }
pineafan63fc5e22022-08-04 22:04:10 +010075 };
76 log(data);
pineafan708692b2022-07-24 22:16:22 +010077 } catch (e) {
78 return interaction.editReply({embeds: [new EmojiEmbed()
79 .setTitle("Attachment Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010080 .setDescription("Something went wrong and the attachment log channel could not be set")
pineafan708692b2022-07-24 22:16:22 +010081 .setStatus("Danger")
82 .setEmoji("CHANNEL.TEXT.DELETE")
83 ], components: []});
84 }
85 } else {
86 return interaction.editReply({embeds: [new EmojiEmbed()
87 .setTitle("Attachment Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010088 .setDescription("No changes were made")
pineafan708692b2022-07-24 22:16:22 +010089 .setStatus("Success")
90 .setEmoji("CHANNEL.TEXT.CREATE")
91 ], components: []});
92 }
93 }
94 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +010095 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +010096 let channel = data.logging.staff.channel;
97 while (true) {
98 await interaction.editReply({embeds: [new EmojiEmbed()
99 .setTitle("Attachment Log Channel")
100 .setDescription(
101 channel ? `Your attachment log channel is currently set to <#${channel}>` : "This server does not have an attachment log channel" +
102 (client.database.premium.hasPremium(interaction.guild.id) ? "" : "\n\nThis server does not have premium, so this feature is disabled")
103 )
104 .setStatus("Success")
105 .setEmoji("CHANNEL.TEXT.CREATE")
106 ], components: [new MessageActionRow().addComponents([new MessageButton()
107 .setCustomId("clear")
108 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
109 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
110 .setStyle("DANGER")
111 .setDisabled(!channel)
112 ])]});
113 let i;
114 try {
115 i = await m.awaitMessageComponent({time: 300000});
pineafan63fc5e22022-08-04 22:04:10 +0100116 } catch(e) { break; }
117 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100118 if (i.component.customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100119 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100120 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100121 clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100122 await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100123 channel = undefined;
124 }
125 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100126 break;
pineafan708692b2022-07-24 22:16:22 +0100127 }
128 }
129 await interaction.editReply({embeds: [new EmojiEmbed()
130 .setTitle("Attachment Log Channel")
131 .setDescription(channel ? `Your attachment log channel is currently set to <#${channel}>` : "This server does not have an attachment log channel")
132 .setStatus("Success")
133 .setEmoji("CHANNEL.TEXT.CREATE")
134 .setFooter({text: "Message closed"})
135 ], components: [new MessageActionRow().addComponents([new MessageButton()
136 .setCustomId("clear")
137 .setLabel("Clear")
138 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
139 .setStyle("SECONDARY")
140 .setDisabled(true)
141 ])]});
pineafan63fc5e22022-08-04 22:04:10 +0100142};
pineafan708692b2022-07-24 22:16:22 +0100143
144const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100145 const member = (interaction.member as Discord.GuildMember);
146 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100147 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100148};
pineafan708692b2022-07-24 22:16:22 +0100149
150export { command };
151export { callback };
152export { check };