blob: c4b9b56f38061c076e72aa02e0b96c091305da9a [file] [log] [blame]
TheCodedProfbaee2c12023-02-18 16:11:06 -05001import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelSelectMenuBuilder, CommandInteraction, SlashCommandSubcommandBuilder } from "discord.js";
2import 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";
7
8export const command = new SlashCommandSubcommandBuilder()
9 .setName("autopublish")
10 .setDescription("Automatically publish messages posted in announcement channels");
11
12export const callback = async (interaction: CommandInteraction): Promise<void> => {
13 await interaction.reply({
14 embeds: LoadingEmbed,
15 ephemeral: true,
16 fetchReply: true
17 });
18
19 let closed = false;
20 let config = await client.database.guilds.read(interaction.guild!.id);
21 let data = Object.assign({}, config.autoPublish);
22 do {
23 const buttons = new ActionRowBuilder<ButtonBuilder>()
24 .addComponents(
25 new ButtonBuilder()
26 .setCustomId("switch")
27 .setLabel(data.enabled ? "Disabled" : "Enabled")
28 .setStyle(data.enabled ? ButtonStyle.Danger : ButtonStyle.Success)
29 .setEmoji(data.enabled ? "✅" : "❌"),
30 new ButtonBuilder()
31 .setCustomId("save")
32 .setLabel("Save")
33 .setStyle(ButtonStyle.Success)
34 .setEmoji("💾")
35 .setDisabled(compare.isEqual(data, config.autoPublish))
36 );
37
38 const channelSelect = new ActionRowBuilder<ChannelSelectMenuBuilder>()
39 .addComponents(
40 new ChannelSelectMenuBuilder()
41 .setCustomId("channel")
42 .setPlaceholder("Select a channel")
43 .setMinValues(1)
44 );
45
46 const embed = new EmojiEmbed()
47
48 await interaction.editReply({
49 embeds: [embed],
50 components: [channelSelect, buttons]
51 });
52
53 let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
54 try {
55 i = await interaction.channel!.awaitMessageComponent({
56 filter: (i) => i.user.id === interaction.user.id,
57 time: 300000
58 }) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
59 } catch (e) {
60 closed = true;
61 break;
62 }
63
64 if(i.isButton()) {
65 switch(i.customId) {
66 case "switch": {
67 data.enabled = !data.enabled;
68 break;
69 }
70 case "save": {
71 await client.database.guilds.write(interaction.guild!.id, { "autoPublish": data });
72 config = await client.database.guilds.read(interaction.guild!.id);
73 data = Object.assign({}, config.autoPublish);
74 break;
75 }
76 }
77 } else {
78 for(const channel of i.values) {
79 data.channels.includes(channel) ? data.channels.splice(data.channels.indexOf(channel), 1) : data.channels.push(channel);
80 }
81 }
82
83 } while (!closed);
84
85 await interaction.deleteReply();
86}
87
88export const check = (interaction: CommandInteraction, _partial: boolean = false) => {
89 const member = interaction.member as Discord.GuildMember;
90 const me = interaction.guild!.members.me as Discord.GuildMember;
91 if (!member.permissions.has("ManageMessages"))
92 return "You must have the *Manage Messages* permission to use this command";
93 if (_partial) return true;
94 if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission";
95 return true;
96};