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"; |
| 5 | import compare from "lodash" |
| 6 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 7 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
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); |
| 22 | let data = Object.assign({}, config.autoPublish); |
| 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 | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 36 | .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) |
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); |
| 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 | |
| 96 | export const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
| 97 | const member = interaction.member as Discord.GuildMember; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 98 | const me = interaction.guild!.members.me!; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 99 | 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 | }; |