blob: a21657f011cf2b80b0e8d00da27af0e7ebb5d43f [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";
5import compare from "lodash"
6import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
TheCodedProf003160f2023-03-04 17:09:40 -05007import getEmojiByName from "../../utils/getEmojiByName.js";
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);
22 let data = Object.assign({}, config.autoPublish);
23 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)
TheCodedProfbaee2c12023-02-18 16:11:06 -050036 .setDisabled(compare.isEqual(data, config.autoPublish))
37 );
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 }
71
72 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);
81 data = Object.assign({}, config.autoPublish);
82 break;
83 }
84 }
85 } else {
86 for(const channel of i.values) {
87 data.channels.includes(channel) ? data.channels.splice(data.channels.indexOf(channel), 1) : data.channels.push(channel);
88 }
89 }
90
91 } while (!closed);
92
93 await interaction.deleteReply();
94}
95
96export const check = (interaction: CommandInteraction, _partial: boolean = false) => {
97 const member = interaction.member as Discord.GuildMember;
TheCodedProf1807fb32023-02-20 14:33:48 -050098 const me = interaction.guild!.members.me!;
TheCodedProfbaee2c12023-02-18 16:11:06 -050099 if (!member.permissions.has("ManageMessages"))
100 return "You must have the *Manage Messages* permission to use this command";
101 if (_partial) return true;
102 if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission";
103 return true;
104};