blob: 27d01bde7845519b7405025f706ceb47ec8e870c [file] [log] [blame]
TheCodedProf003160f2023-03-04 17:09:40 -05001import { ActionRowBuilder, APIMessageComponentEmoji, ButtonBuilder, ButtonStyle, ChannelSelectMenuBuilder, ChannelType, CommandInteraction, SlashCommandSubcommandBuilder } from "discord.js";
TheCodedProfbaee2c12023-02-18 16:11:06 -05002import type Discord from "discord.js";
3import client from "../../utils/client.js";
4import { LoadingEmbed } from "../../utils/defaults.js";
TheCodedProfbaee2c12023-02-18 16:11:06 -05005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
TheCodedProf003160f2023-03-04 17:09:40 -05006import getEmojiByName from "../../utils/getEmojiByName.js";
TheCodedProfea3e0e02023-03-04 17:55:42 -05007import _ from "lodash";
TheCodedProfbaee2c12023-02-18 16:11:06 -05008
9export const command = new SlashCommandSubcommandBuilder()
10 .setName("autopublish")
11 .setDescription("Automatically publish messages posted in announcement channels");
12
13export const callback = async (interaction: CommandInteraction): Promise<void> => {
14 await interaction.reply({
15 embeds: LoadingEmbed,
16 ephemeral: true,
17 fetchReply: true
18 });
19
20 let closed = false;
21 let config = await client.database.guilds.read(interaction.guild!.id);
TheCodedProfea3e0e02023-03-04 17:55:42 -050022 let data = _.cloneDeep(config.autoPublish);
TheCodedProfbaee2c12023-02-18 16:11:06 -050023 do {
24 const buttons = new ActionRowBuilder<ButtonBuilder>()
25 .addComponents(
26 new ButtonBuilder()
27 .setCustomId("switch")
TheCodedProf003160f2023-03-04 17:09:40 -050028 .setLabel(data.enabled ? "Enabled" : "Disabled")
29 .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger)
30 .setEmoji(getEmojiByName("CONTROL." + (data.enabled ? "TICK" : "CROSS"), "id") as APIMessageComponentEmoji),
TheCodedProfbaee2c12023-02-18 16:11:06 -050031 new ButtonBuilder()
32 .setCustomId("save")
33 .setLabel("Save")
34 .setStyle(ButtonStyle.Success)
TheCodedProf003160f2023-03-04 17:09:40 -050035 .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji)
TheCodedProfea3e0e02023-03-04 17:55:42 -050036 .setDisabled(_.isEqual(data, config.autoPublish))
TheCodedProfbaee2c12023-02-18 16:11:06 -050037 );
38
39 const channelSelect = new ActionRowBuilder<ChannelSelectMenuBuilder>()
40 .addComponents(
41 new ChannelSelectMenuBuilder()
42 .setCustomId("channel")
43 .setPlaceholder("Select a channel")
44 .setMinValues(1)
TheCodedProf003160f2023-03-04 17:09:40 -050045 .setChannelTypes(ChannelType.GuildAnnouncement, ChannelType.AnnouncementThread)
TheCodedProfbaee2c12023-02-18 16:11:06 -050046 );
47
TheCodedProf003160f2023-03-04 17:09:40 -050048 const current = data.channels.map((c) => `> <#${c}>`).join("\n") || "*None set*";
TheCodedProfbaee2c12023-02-18 16:11:06 -050049
TheCodedProf003160f2023-03-04 17:09:40 -050050 const embed = new EmojiEmbed()
51 .setTitle("Auto Publish")
52 .setDescription("Currently enabled in:\n" + current)
53 .setStatus('Success')
54 .setEmoji("ICONS.PUBLISH")
55
56 await interaction.editReply({
TheCodedProfbaee2c12023-02-18 16:11:06 -050057 embeds: [embed],
58 components: [channelSelect, buttons]
59 });
60
61 let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
62 try {
63 i = await interaction.channel!.awaitMessageComponent({
pineafan96228bd2023-02-21 14:22:55 +000064 filter: (i: Discord.Interaction) => i.user.id === interaction.user.id,
TheCodedProfbaee2c12023-02-18 16:11:06 -050065 time: 300000
66 }) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
67 } catch (e) {
68 closed = true;
TheCodedProf1807fb32023-02-20 14:33:48 -050069 continue;
TheCodedProfbaee2c12023-02-18 16:11:06 -050070 }
TheCodedProfe15de662023-03-04 17:13:36 -050071 await i.deferUpdate();
TheCodedProfbaee2c12023-02-18 16:11:06 -050072 if(i.isButton()) {
73 switch(i.customId) {
74 case "switch": {
75 data.enabled = !data.enabled;
76 break;
77 }
78 case "save": {
79 await client.database.guilds.write(interaction.guild!.id, { "autoPublish": data });
80 config = await client.database.guilds.read(interaction.guild!.id);
TheCodedProfea3e0e02023-03-04 17:55:42 -050081 data = _.cloneDeep(config.autoPublish);
Skyler Grey16ecb172023-03-05 07:30:32 +000082 await client.memory.forceUpdate(interaction.guild!.id);
TheCodedProfbaee2c12023-02-18 16:11:06 -050083 break;
84 }
85 }
86 } else {
TheCodedProff40edba2023-03-04 17:17:11 -050087 await interaction.editReply({embeds: LoadingEmbed, components: []})
TheCodedProfbaee2c12023-02-18 16:11:06 -050088 for(const channel of i.values) {
89 data.channels.includes(channel) ? data.channels.splice(data.channels.indexOf(channel), 1) : data.channels.push(channel);
90 }
91 }
92
93 } while (!closed);
94
95 await interaction.deleteReply();
96}
97
98export const check = (interaction: CommandInteraction, _partial: boolean = false) => {
99 const member = interaction.member as Discord.GuildMember;
TheCodedProf1807fb32023-02-20 14:33:48 -0500100 const me = interaction.guild!.members.me!;
TheCodedProfbaee2c12023-02-18 16:11:06 -0500101 if (!member.permissions.has("ManageMessages"))
102 return "You must have the *Manage Messages* permission to use this command";
103 if (_partial) return true;
104 if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission";
105 return true;
106};