blob: 62fb248b0dbaa3c30800985ee18bd7a82c0f5401 [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LoadingEmbed } from "../../../utils/defaults.js";
Skyler Greyda16adf2023-03-05 10:22:12 +00002import Discord, {
3 CommandInteraction,
4 ActionRowBuilder,
5 ButtonBuilder,
6 ButtonStyle,
7 ChannelSelectMenuBuilder,
8 ChannelType
9} from "discord.js";
pineafan708692b2022-07-24 22:16:22 +010010import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
pineafan708692b2022-07-24 22:16:22 +010011import getEmojiByName from "../../../utils/getEmojiByName.js";
TheCodedProff86ba092023-01-27 17:10:07 -050012import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan708692b2022-07-24 22:16:22 +010013import client from "../../../utils/client.js";
TheCodedProf01cba762023-02-18 15:55:05 -050014import { getCommandMentionByName } from "../../../utils/getCommandDataByName.js";
pineafan708692b2022-07-24 22:16:22 +010015
16const command = (builder: SlashCommandSubcommandBuilder) =>
Skyler Greyda16adf2023-03-05 10:22:12 +000017 builder.setName("attachments").setDescription("Where attachments should be logged to (Premium only)");
pineafan708692b2022-07-24 22:16:22 +010018
pineafan3a02ea32022-08-11 21:35:04 +010019const callback = async (interaction: CommandInteraction): Promise<unknown> => {
TheCodedProf9c51a7e2023-02-27 17:11:13 -050020 if (interaction.guild) client.database.premium.hasPremium(interaction.guild.id).finally(() => {});
TheCodedProf80ad8542023-03-10 12:52:33 -050021 const m = await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010022 embeds: LoadingEmbed,
23 ephemeral: true,
24 fetchReply: true
TheCodedProf01cba762023-02-18 15:55:05 -050025 });
26
Skyler Greyda16adf2023-03-05 10:22:12 +000027 if (!(await client.database.premium.hasPremium(interaction.guild!.id)))
28 return interaction.editReply({
29 embeds: [
30 new EmojiEmbed()
31 .setTitle("Premium Required")
32 .setDescription(
33 `This feature is exclusive to ${getCommandMentionByName("nucleus/premium")} servers.`
34 )
35 .setStatus("Danger")
36 .setEmoji("NUCLEUS.PREMIUM")
37 ]
38 });
39
TheCodedProf01cba762023-02-18 15:55:05 -050040 let data = await client.database.guilds.read(interaction.guild!.id);
pineafanaea03fb2023-03-27 16:50:39 +010041 let channel = data.logging.attachments.channel;
TheCodedProf01cba762023-02-18 15:55:05 -050042
43 let closed = false;
44 do {
Skyler Greyda16adf2023-03-05 10:22:12 +000045 const channelMenu = new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents(
46 new ChannelSelectMenuBuilder()
47 .setCustomId("channel")
48 .setPlaceholder("Select a channel")
49 .setChannelTypes(ChannelType.GuildText)
50 );
51 const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
52 new ButtonBuilder()
53 .setCustomId("clear")
54 .setLabel("Clear")
55 .setStyle(ButtonStyle.Danger)
56 .setEmoji(getEmojiByName("CONTROL.CROSS", "id") as Discord.APIMessageComponentEmoji)
57 .setDisabled(!channel),
58 new ButtonBuilder()
59 .setCustomId("save")
60 .setLabel("Save")
61 .setStyle(ButtonStyle.Success)
62 .setEmoji(getEmojiByName("ICONS.SAVE", "id") as Discord.APIMessageComponentEmoji)
pineafanaea03fb2023-03-27 16:50:39 +010063 .setDisabled(channel === data.logging.attachments.channel)
Skyler Greyda16adf2023-03-05 10:22:12 +000064 );
TheCodedProf01cba762023-02-18 15:55:05 -050065
66 const embed = new EmojiEmbed()
67 .setTitle("Attachments")
68 .setDescription(
TheCodedProf94ff6de2023-02-22 17:47:26 -050069 `The channel to send all attachments from the server, allowing you to check them if they are deleted\n` +
Skyler Greyda16adf2023-03-05 10:22:12 +000070 `**Channel:** ${channel ? `<#${channel}>` : "*None*"}\n`
TheCodedProf01cba762023-02-18 15:55:05 -050071 )
72 .setStatus("Success")
Skyler Greyda16adf2023-03-05 10:22:12 +000073 .setEmoji("CHANNEL.TEXT.CREATE");
TheCodedProf01cba762023-02-18 15:55:05 -050074
75 await interaction.editReply({
76 embeds: [embed],
77 components: [channelMenu, buttons]
78 });
79
80 let i: Discord.ButtonInteraction | Discord.SelectMenuInteraction;
81 try {
TheCodedProf80ad8542023-03-10 12:52:33 -050082 i = (await m.awaitMessageComponent({
pineafan96228bd2023-02-21 14:22:55 +000083 filter: (i: Discord.Interaction) => i.user.id === interaction.user.id,
TheCodedProf01cba762023-02-18 15:55:05 -050084 time: 300000
85 })) as Discord.ButtonInteraction | Discord.SelectMenuInteraction;
86 } catch (e) {
87 closed = true;
TheCodedProf1807fb32023-02-20 14:33:48 -050088 continue;
TheCodedProf01cba762023-02-18 15:55:05 -050089 }
90 await i.deferUpdate();
Skyler Greyda16adf2023-03-05 10:22:12 +000091 if (i.isButton()) {
TheCodedProf01cba762023-02-18 15:55:05 -050092 switch (i.customId) {
93 case "clear": {
94 channel = null;
95 break;
96 }
97 case "save": {
98 await client.database.guilds.write(interaction.guild!.id, {
99 "logging.attachments.channel": channel
100 });
101 data = await client.database.guilds.read(interaction.guild!.id);
Skyler Grey16ecb172023-03-05 07:30:32 +0000102 await client.memory.forceUpdate(interaction.guild!.id);
TheCodedProf01cba762023-02-18 15:55:05 -0500103 break;
104 }
105 }
106 } else {
107 channel = i.values[0]!;
108 }
TheCodedProf01cba762023-02-18 15:55:05 -0500109 } while (!closed);
Skyler Greyda16adf2023-03-05 10:22:12 +0000110 await interaction.deleteReply();
pineafan63fc5e22022-08-04 22:04:10 +0100111};
pineafan708692b2022-07-24 22:16:22 +0100112
TheCodedProff86ba092023-01-27 17:10:07 -0500113const check = (interaction: CommandInteraction, _partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 const member = interaction.member as Discord.GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000115 if (!member.permissions.has("ManageGuild"))
116 return "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100117 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100118};
pineafan708692b2022-07-24 22:16:22 +0100119
120export { command };
121export { callback };
122export { check };