Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { |
| 2 | ActionRowBuilder, |
| 3 | APIMessageComponentEmoji, |
| 4 | ButtonBuilder, |
| 5 | ButtonStyle, |
| 6 | ChannelSelectMenuBuilder, |
| 7 | ChannelType, |
| 8 | CommandInteraction, |
| 9 | SlashCommandSubcommandBuilder |
| 10 | } from "discord.js"; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 11 | import type Discord from "discord.js"; |
| 12 | import client from "../../utils/client.js"; |
| 13 | import { LoadingEmbed } from "../../utils/defaults.js"; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 14 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 15 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame] | 16 | import _ from "lodash"; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 17 | |
| 18 | export const command = new SlashCommandSubcommandBuilder() |
| 19 | .setName("autopublish") |
| 20 | .setDescription("Automatically publish messages posted in announcement channels"); |
| 21 | |
| 22 | export const callback = async (interaction: CommandInteraction): Promise<void> => { |
TheCodedProf | 80ad854 | 2023-03-10 12:52:33 -0500 | [diff] [blame] | 23 | const m = await interaction.reply({ |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 24 | embeds: LoadingEmbed, |
| 25 | ephemeral: true, |
| 26 | fetchReply: true |
| 27 | }); |
| 28 | |
| 29 | let closed = false; |
| 30 | let config = await client.database.guilds.read(interaction.guild!.id); |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame] | 31 | let data = _.cloneDeep(config.autoPublish); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 32 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 33 | const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 34 | new ButtonBuilder() |
| 35 | .setCustomId("switch") |
| 36 | .setLabel(data.enabled ? "Enabled" : "Disabled") |
| 37 | .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger) |
| 38 | .setEmoji( |
| 39 | getEmojiByName("CONTROL." + (data.enabled ? "TICK" : "CROSS"), "id") as APIMessageComponentEmoji |
| 40 | ), |
| 41 | new ButtonBuilder() |
| 42 | .setCustomId("save") |
| 43 | .setLabel("Save") |
| 44 | .setStyle(ButtonStyle.Success) |
| 45 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
| 46 | .setDisabled(_.isEqual(data, config.autoPublish)) |
| 47 | ); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 48 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 49 | const channelSelect = new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents( |
| 50 | new ChannelSelectMenuBuilder() |
| 51 | .setCustomId("channel") |
| 52 | .setPlaceholder("Select a channel") |
| 53 | .setMinValues(1) |
| 54 | .setChannelTypes(ChannelType.GuildAnnouncement, ChannelType.AnnouncementThread) |
| 55 | ); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 56 | |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 57 | const current = data.channels.map((c) => `> <#${c}>`).join("\n") || "*None set*"; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 58 | |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 59 | const embed = new EmojiEmbed() |
| 60 | .setTitle("Auto Publish") |
| 61 | .setDescription("Currently enabled in:\n" + current) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 62 | .setStatus("Success") |
| 63 | .setEmoji("ICONS.PUBLISH"); |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 64 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 65 | await interaction.editReply({ |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 66 | embeds: [embed], |
| 67 | components: [channelSelect, buttons] |
| 68 | }); |
| 69 | |
| 70 | let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction; |
| 71 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 72 | i = (await interaction.channel!.awaitMessageComponent({ |
Skyler Grey | 21f5229 | 2023-03-10 17:58:30 +0000 | [diff] [blame^] | 73 | filter: (i: Discord.Interaction) => |
| 74 | i.user.id === interaction.user.id && i.isMessageComponent() && i.message.id === m.id, |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 75 | time: 300000 |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 76 | })) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 77 | } catch (e) { |
| 78 | closed = true; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 79 | continue; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 80 | } |
TheCodedProf | e15de66 | 2023-03-04 17:13:36 -0500 | [diff] [blame] | 81 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 82 | if (i.isButton()) { |
| 83 | switch (i.customId) { |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 84 | case "switch": { |
| 85 | data.enabled = !data.enabled; |
| 86 | break; |
| 87 | } |
| 88 | case "save": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 89 | await client.database.guilds.write(interaction.guild!.id, { autoPublish: data }); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 90 | config = await client.database.guilds.read(interaction.guild!.id); |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame] | 91 | data = _.cloneDeep(config.autoPublish); |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame] | 92 | await client.memory.forceUpdate(interaction.guild!.id); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 97 | await interaction.editReply({ embeds: LoadingEmbed, components: [] }); |
| 98 | for (const channel of i.values) { |
| 99 | data.channels.includes(channel) |
| 100 | ? data.channels.splice(data.channels.indexOf(channel), 1) |
| 101 | : data.channels.push(channel); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 102 | } |
| 103 | } |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 104 | } while (!closed); |
| 105 | |
| 106 | await interaction.deleteReply(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 107 | }; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 108 | |
| 109 | export const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
| 110 | const member = interaction.member as Discord.GuildMember; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 111 | const me = interaction.guild!.members.me!; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 112 | if (!member.permissions.has("ManageMessages")) |
| 113 | return "You must have the *Manage Messages* permission to use this command"; |
| 114 | if (_partial) return true; |
| 115 | if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission"; |
| 116 | return true; |
| 117 | }; |