TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 1 | import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelSelectMenuBuilder, CommandInteraction, SlashCommandSubcommandBuilder } from "discord.js"; |
| 2 | import type Discord from "discord.js"; |
| 3 | import client from "../../utils/client.js"; |
| 4 | import { LoadingEmbed } from "../../utils/defaults.js"; |
| 5 | import compare from "lodash" |
| 6 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 7 | |
| 8 | export const command = new SlashCommandSubcommandBuilder() |
| 9 | .setName("autopublish") |
| 10 | .setDescription("Automatically publish messages posted in announcement channels"); |
| 11 | |
| 12 | export 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({ |
pineafan | 96228bd | 2023-02-21 14:22:55 +0000 | [diff] [blame] | 56 | filter: (i: Discord.Interaction) => i.user.id === interaction.user.id, |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 57 | time: 300000 |
| 58 | }) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction; |
| 59 | } catch (e) { |
| 60 | closed = true; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 61 | continue; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 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 | |
| 88 | export const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
| 89 | const member = interaction.member as Discord.GuildMember; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 90 | const me = interaction.guild!.members.me!; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 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 | }; |