blob: afc4e7d9ac705f248c6d5c3c75cab03dae05ee39 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import {
2 ActionRowBuilder,
3 APIMessageComponentEmoji,
4 ButtonBuilder,
5 ButtonStyle,
6 ChannelSelectMenuBuilder,
7 ChannelType,
8 CommandInteraction,
9 SlashCommandSubcommandBuilder
10} from "discord.js";
TheCodedProfbaee2c12023-02-18 16:11:06 -050011import type Discord from "discord.js";
12import client from "../../utils/client.js";
13import { LoadingEmbed } from "../../utils/defaults.js";
TheCodedProfbaee2c12023-02-18 16:11:06 -050014import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
TheCodedProf003160f2023-03-04 17:09:40 -050015import getEmojiByName from "../../utils/getEmojiByName.js";
TheCodedProfea3e0e02023-03-04 17:55:42 -050016import _ from "lodash";
TheCodedProfbaee2c12023-02-18 16:11:06 -050017
18export const command = new SlashCommandSubcommandBuilder()
19 .setName("autopublish")
20 .setDescription("Automatically publish messages posted in announcement channels");
21
22export const callback = async (interaction: CommandInteraction): Promise<void> => {
23 await interaction.reply({
24 embeds: LoadingEmbed,
25 ephemeral: true,
26 fetchReply: true
27 });
28
29 let closed = false;
30 let config = await client.database.guilds.read(interaction.guild!.id);
TheCodedProfea3e0e02023-03-04 17:55:42 -050031 let data = _.cloneDeep(config.autoPublish);
TheCodedProfbaee2c12023-02-18 16:11:06 -050032 do {
Skyler Greyda16adf2023-03-05 10:22:12 +000033 const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
34 new ButtonBuilder()
35 .setCustomId("switch")
36 .setLabel(data.enabled ? "Enabled" : "Disabled")
37 .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger)
38 .setEmoji(
39 getEmojiByName("CONTROL." + (data.enabled ? "TICK" : "CROSS"), "id") as APIMessageComponentEmoji
40 ),
41 new ButtonBuilder()
42 .setCustomId("save")
43 .setLabel("Save")
44 .setStyle(ButtonStyle.Success)
45 .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji)
46 .setDisabled(_.isEqual(data, config.autoPublish))
47 );
TheCodedProfbaee2c12023-02-18 16:11:06 -050048
Skyler Greyda16adf2023-03-05 10:22:12 +000049 const channelSelect = new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents(
50 new ChannelSelectMenuBuilder()
51 .setCustomId("channel")
52 .setPlaceholder("Select a channel")
53 .setMinValues(1)
54 .setChannelTypes(ChannelType.GuildAnnouncement, ChannelType.AnnouncementThread)
55 );
TheCodedProfbaee2c12023-02-18 16:11:06 -050056
TheCodedProf003160f2023-03-04 17:09:40 -050057 const current = data.channels.map((c) => `> <#${c}>`).join("\n") || "*None set*";
TheCodedProfbaee2c12023-02-18 16:11:06 -050058
TheCodedProf003160f2023-03-04 17:09:40 -050059 const embed = new EmojiEmbed()
60 .setTitle("Auto Publish")
61 .setDescription("Currently enabled in:\n" + current)
Skyler Greyda16adf2023-03-05 10:22:12 +000062 .setStatus("Success")
63 .setEmoji("ICONS.PUBLISH");
TheCodedProf003160f2023-03-04 17:09:40 -050064
Skyler Greyda16adf2023-03-05 10:22:12 +000065 await interaction.editReply({
TheCodedProfbaee2c12023-02-18 16:11:06 -050066 embeds: [embed],
67 components: [channelSelect, buttons]
68 });
69
70 let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
71 try {
Skyler Greyda16adf2023-03-05 10:22:12 +000072 i = (await interaction.channel!.awaitMessageComponent({
pineafan96228bd2023-02-21 14:22:55 +000073 filter: (i: Discord.Interaction) => i.user.id === interaction.user.id,
TheCodedProfbaee2c12023-02-18 16:11:06 -050074 time: 300000
Skyler Greyda16adf2023-03-05 10:22:12 +000075 })) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
TheCodedProfbaee2c12023-02-18 16:11:06 -050076 } catch (e) {
77 closed = true;
TheCodedProf1807fb32023-02-20 14:33:48 -050078 continue;
TheCodedProfbaee2c12023-02-18 16:11:06 -050079 }
TheCodedProfe15de662023-03-04 17:13:36 -050080 await i.deferUpdate();
Skyler Greyda16adf2023-03-05 10:22:12 +000081 if (i.isButton()) {
82 switch (i.customId) {
TheCodedProfbaee2c12023-02-18 16:11:06 -050083 case "switch": {
84 data.enabled = !data.enabled;
85 break;
86 }
87 case "save": {
Skyler Greyda16adf2023-03-05 10:22:12 +000088 await client.database.guilds.write(interaction.guild!.id, { autoPublish: data });
TheCodedProfbaee2c12023-02-18 16:11:06 -050089 config = await client.database.guilds.read(interaction.guild!.id);
TheCodedProfea3e0e02023-03-04 17:55:42 -050090 data = _.cloneDeep(config.autoPublish);
Skyler Grey16ecb172023-03-05 07:30:32 +000091 await client.memory.forceUpdate(interaction.guild!.id);
TheCodedProfbaee2c12023-02-18 16:11:06 -050092 break;
93 }
94 }
95 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +000096 await interaction.editReply({ embeds: LoadingEmbed, components: [] });
97 for (const channel of i.values) {
98 data.channels.includes(channel)
99 ? data.channels.splice(data.channels.indexOf(channel), 1)
100 : data.channels.push(channel);
TheCodedProfbaee2c12023-02-18 16:11:06 -0500101 }
102 }
TheCodedProfbaee2c12023-02-18 16:11:06 -0500103 } while (!closed);
104
105 await interaction.deleteReply();
Skyler Greyda16adf2023-03-05 10:22:12 +0000106};
TheCodedProfbaee2c12023-02-18 16:11:06 -0500107
108export const check = (interaction: CommandInteraction, _partial: boolean = false) => {
109 const member = interaction.member as Discord.GuildMember;
TheCodedProf1807fb32023-02-20 14:33:48 -0500110 const me = interaction.guild!.members.me!;
TheCodedProfbaee2c12023-02-18 16:11:06 -0500111 if (!member.permissions.has("ManageMessages"))
112 return "You must have the *Manage Messages* permission to use this command";
113 if (_partial) return true;
114 if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission";
115 return true;
116};