TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 1 | import { ActionRowBuilder, APIMessageComponentEmoji, ButtonBuilder, ButtonStyle, ChannelSelectMenuBuilder, ChannelType, CommandInteraction, SlashCommandSubcommandBuilder } from "discord.js"; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 2 | import type Discord from "discord.js"; |
| 3 | import client from "../../utils/client.js"; |
| 4 | import { LoadingEmbed } from "../../utils/defaults.js"; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 5 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 6 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame^] | 7 | import _ from "lodash"; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 8 | |
| 9 | export const command = new SlashCommandSubcommandBuilder() |
| 10 | .setName("autopublish") |
| 11 | .setDescription("Automatically publish messages posted in announcement channels"); |
| 12 | |
| 13 | export 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); |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame^] | 22 | let data = _.cloneDeep(config.autoPublish); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 23 | do { |
| 24 | const buttons = new ActionRowBuilder<ButtonBuilder>() |
| 25 | .addComponents( |
| 26 | new ButtonBuilder() |
| 27 | .setCustomId("switch") |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 28 | .setLabel(data.enabled ? "Enabled" : "Disabled") |
| 29 | .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger) |
| 30 | .setEmoji(getEmojiByName("CONTROL." + (data.enabled ? "TICK" : "CROSS"), "id") as APIMessageComponentEmoji), |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 31 | new ButtonBuilder() |
| 32 | .setCustomId("save") |
| 33 | .setLabel("Save") |
| 34 | .setStyle(ButtonStyle.Success) |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 35 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame^] | 36 | .setDisabled(_.isEqual(data, config.autoPublish)) |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 37 | ); |
| 38 | |
| 39 | const channelSelect = new ActionRowBuilder<ChannelSelectMenuBuilder>() |
| 40 | .addComponents( |
| 41 | new ChannelSelectMenuBuilder() |
| 42 | .setCustomId("channel") |
| 43 | .setPlaceholder("Select a channel") |
| 44 | .setMinValues(1) |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 45 | .setChannelTypes(ChannelType.GuildAnnouncement, ChannelType.AnnouncementThread) |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 46 | ); |
| 47 | |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 48 | const current = data.channels.map((c) => `> <#${c}>`).join("\n") || "*None set*"; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 49 | |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 50 | 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({ |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 57 | embeds: [embed], |
| 58 | components: [channelSelect, buttons] |
| 59 | }); |
| 60 | |
| 61 | let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction; |
| 62 | try { |
| 63 | i = await interaction.channel!.awaitMessageComponent({ |
pineafan | 96228bd | 2023-02-21 14:22:55 +0000 | [diff] [blame] | 64 | filter: (i: Discord.Interaction) => i.user.id === interaction.user.id, |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 65 | time: 300000 |
| 66 | }) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction; |
| 67 | } catch (e) { |
| 68 | closed = true; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 69 | continue; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 70 | } |
TheCodedProf | e15de66 | 2023-03-04 17:13:36 -0500 | [diff] [blame] | 71 | await i.deferUpdate(); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 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); |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame^] | 81 | data = _.cloneDeep(config.autoPublish); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 82 | break; |
| 83 | } |
| 84 | } |
| 85 | } else { |
TheCodedProf | f40edba | 2023-03-04 17:17:11 -0500 | [diff] [blame] | 86 | await interaction.editReply({embeds: LoadingEmbed, components: []}) |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 87 | for(const channel of i.values) { |
| 88 | data.channels.includes(channel) ? data.channels.splice(data.channels.indexOf(channel), 1) : data.channels.push(channel); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | } while (!closed); |
| 93 | |
| 94 | await interaction.deleteReply(); |
| 95 | } |
| 96 | |
| 97 | export const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
| 98 | const member = interaction.member as Discord.GuildMember; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 99 | const me = interaction.guild!.members.me!; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 100 | if (!member.permissions.has("ManageMessages")) |
| 101 | return "You must have the *Manage Messages* permission to use this command"; |
| 102 | if (_partial) return true; |
| 103 | if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission"; |
| 104 | return true; |
| 105 | }; |