blob: 421cfb86f8b182191021cd4bc1fe7381da3356d0 [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> => {
TheCodedProf80ad8542023-03-10 12:52:33 -050023 const m = await interaction.reply({
TheCodedProfbaee2c12023-02-18 16:11:06 -050024 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({
Skyler Grey21f52292023-03-10 17:58:30 +000073 filter: (i: Discord.Interaction) =>
74 i.user.id === interaction.user.id && i.isMessageComponent() && i.message.id === m.id,
TheCodedProfbaee2c12023-02-18 16:11:06 -050075 time: 300000
Skyler Greyda16adf2023-03-05 10:22:12 +000076 })) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
TheCodedProfbaee2c12023-02-18 16:11:06 -050077 } catch (e) {
78 closed = true;
TheCodedProf1807fb32023-02-20 14:33:48 -050079 continue;
TheCodedProfbaee2c12023-02-18 16:11:06 -050080 }
TheCodedProfe15de662023-03-04 17:13:36 -050081 await i.deferUpdate();
Skyler Greyda16adf2023-03-05 10:22:12 +000082 if (i.isButton()) {
83 switch (i.customId) {
TheCodedProfbaee2c12023-02-18 16:11:06 -050084 case "switch": {
85 data.enabled = !data.enabled;
86 break;
87 }
88 case "save": {
Skyler Greyda16adf2023-03-05 10:22:12 +000089 await client.database.guilds.write(interaction.guild!.id, { autoPublish: data });
TheCodedProfbaee2c12023-02-18 16:11:06 -050090 config = await client.database.guilds.read(interaction.guild!.id);
TheCodedProfea3e0e02023-03-04 17:55:42 -050091 data = _.cloneDeep(config.autoPublish);
Skyler Grey16ecb172023-03-05 07:30:32 +000092 await client.memory.forceUpdate(interaction.guild!.id);
TheCodedProfbaee2c12023-02-18 16:11:06 -050093 break;
94 }
95 }
96 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +000097 await interaction.editReply({ embeds: LoadingEmbed, components: [] });
98 for (const channel of i.values) {
99 data.channels.includes(channel)
100 ? data.channels.splice(data.channels.indexOf(channel), 1)
101 : data.channels.push(channel);
TheCodedProfbaee2c12023-02-18 16:11:06 -0500102 }
103 }
TheCodedProfbaee2c12023-02-18 16:11:06 -0500104 } while (!closed);
105
106 await interaction.deleteReply();
Skyler Greyda16adf2023-03-05 10:22:12 +0000107};
TheCodedProfbaee2c12023-02-18 16:11:06 -0500108
109export const check = (interaction: CommandInteraction, _partial: boolean = false) => {
110 const member = interaction.member as Discord.GuildMember;
TheCodedProf1807fb32023-02-20 14:33:48 -0500111 const me = interaction.guild!.members.me!;
TheCodedProfbaee2c12023-02-18 16:11:06 -0500112 if (!member.permissions.has("ManageMessages"))
113 return "You must have the *Manage Messages* permission to use this command";
114 if (_partial) return true;
115 if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission";
116 return true;
117};