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> => { |
| 23 | await interaction.reply({ |
| 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({ |
pineafan | 96228bd | 2023-02-21 14:22:55 +0000 | [diff] [blame] | 73 | filter: (i: Discord.Interaction) => i.user.id === interaction.user.id, |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 74 | time: 300000 |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 75 | })) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 76 | } catch (e) { |
| 77 | closed = true; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 78 | continue; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 79 | } |
TheCodedProf | e15de66 | 2023-03-04 17:13:36 -0500 | [diff] [blame] | 80 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 81 | if (i.isButton()) { |
| 82 | switch (i.customId) { |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 83 | case "switch": { |
| 84 | data.enabled = !data.enabled; |
| 85 | break; |
| 86 | } |
| 87 | case "save": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 88 | await client.database.guilds.write(interaction.guild!.id, { autoPublish: data }); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 89 | config = await client.database.guilds.read(interaction.guild!.id); |
TheCodedProf | ea3e0e0 | 2023-03-04 17:55:42 -0500 | [diff] [blame] | 90 | data = _.cloneDeep(config.autoPublish); |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame] | 91 | await client.memory.forceUpdate(interaction.guild!.id); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | } |
| 95 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 96 | await interaction.editReply({ embeds: LoadingEmbed, components: [] }); |
| 97 | for (const channel of i.values) { |
| 98 | data.channels.includes(channel) |
| 99 | ? data.channels.splice(data.channels.indexOf(channel), 1) |
| 100 | : data.channels.push(channel); |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 101 | } |
| 102 | } |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 103 | } while (!closed); |
| 104 | |
| 105 | await interaction.deleteReply(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 106 | }; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 107 | |
| 108 | export const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
| 109 | const member = interaction.member as Discord.GuildMember; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 110 | const me = interaction.guild!.members.me!; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 111 | if (!member.permissions.has("ManageMessages")) |
| 112 | return "You must have the *Manage Messages* permission to use this command"; |
| 113 | if (_partial) return true; |
| 114 | if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission"; |
| 115 | return true; |
| 116 | }; |