PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../../utils/defaults.js"; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 2 | import { ChannelType } from "discord-api-types/v9"; |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 3 | import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle, ButtonInteraction } from "discord.js"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
| 5 | import confirmationMessage from "../../../utils/confirmationMessage.js"; |
| 6 | import getEmojiByName from "../../../utils/getEmojiByName.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 7 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 8 | import client from "../../../utils/client.js"; |
| 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | .setName("attachments") |
| 13 | .setDescription("Where attachments should be logged to (Premium only)") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 14 | .addChannelOption((option) => |
| 15 | option |
| 16 | .setName("channel") |
| 17 | .setDescription("The channel to log attachments in") |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 18 | .addChannelTypes(ChannelType.GuildText) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | .setRequired(false) |
| 20 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 21 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 22 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | const m = (await interaction.reply({ |
| 24 | embeds: LoadingEmbed, |
| 25 | ephemeral: true, |
| 26 | fetchReply: true |
| 27 | })) as Discord.Message; |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 28 | if (interaction.options.get("channel")?.channel) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | let channel; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 30 | try { |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 31 | channel = interaction.options.get("channel")?.channel; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 32 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 33 | return await interaction.editReply({ |
| 34 | embeds: [ |
| 35 | new EmojiEmbed() |
| 36 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 37 | .setTitle("Attachment Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 38 | .setDescription("The channel you provided is not a valid channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | .setStatus("Danger") |
| 40 | ] |
| 41 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 42 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | channel = channel as Discord.TextChannel; |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 44 | if (channel.guild.id !== interaction.guild!.id) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | return interaction.editReply({ |
| 46 | embeds: [ |
| 47 | new EmojiEmbed() |
| 48 | .setTitle("Attachment Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 49 | .setDescription("You must choose a channel in this server") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 50 | .setStatus("Danger") |
| 51 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 52 | ] |
| 53 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 54 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | const confirmation = await new confirmationMessage(interaction) |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 56 | .setEmoji("CHANNEL.TEXT.EDIT") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 57 | .setTitle("Attachment Log Channel") |
| 58 | .setDescription( |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | "This will be the channel all attachments will be sent to.\n\n" + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | `Are you sure you want to set the attachment log channel to <#${channel.id}>?` |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 61 | ) |
| 62 | .setColor("Warning") |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 63 | .setFailedMessage("No changes were made", "Success", "CHANNEL.TEXT.CREATE") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 64 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 65 | .send(true); |
| 66 | if (confirmation.cancelled) return; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 67 | if (confirmation.success) { |
| 68 | try { |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 69 | await client.database.guilds.write(interaction.guild!.id, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | "logging.attachments.channel": channel.id |
| 71 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 72 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 73 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 74 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 75 | type: "attachmentChannelUpdate", |
| 76 | displayName: "Attachment Log Channel Updated", |
| 77 | calculateType: "nucleusSettingsUpdated", |
| 78 | color: NucleusColors.yellow, |
| 79 | emoji: "CHANNEL.TEXT.EDIT", |
| 80 | timestamp: new Date().getTime() |
| 81 | }, |
| 82 | list: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 83 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 84 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | channel: entry(channel.id, renderChannel(channel)) |
| 86 | }, |
| 87 | hidden: { |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 88 | guild: interaction.guild!.id |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 89 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 90 | }; |
| 91 | log(data); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 92 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | return interaction.editReply({ |
| 94 | embeds: [ |
| 95 | new EmojiEmbed() |
| 96 | .setTitle("Attachment Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 97 | .setDescription("Something went wrong and the attachment log channel could not be set") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 98 | .setStatus("Danger") |
| 99 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 100 | ], |
| 101 | components: [] |
| 102 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 103 | } |
| 104 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | return interaction.editReply({ |
| 106 | embeds: [ |
| 107 | new EmojiEmbed() |
| 108 | .setTitle("Attachment Log Channel") |
| 109 | .setDescription("No changes were made") |
| 110 | .setStatus("Success") |
| 111 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 112 | ], |
| 113 | components: [] |
| 114 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | let clicks = 0; |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 118 | const data = await client.database.guilds.read(interaction.guild!.id); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 119 | let channel = data.logging.staff.channel; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 120 | |
| 121 | let timedOut = false; |
| 122 | while (!timedOut) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 123 | await interaction.editReply({ |
| 124 | embeds: [ |
| 125 | new EmojiEmbed() |
| 126 | .setTitle("Attachment Log Channel") |
| 127 | .setDescription( |
| 128 | channel |
| 129 | ? `Your attachment log channel is currently set to <#${channel}>` |
| 130 | : "This server does not have an attachment log channel" + |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 131 | (await client.database.premium.hasPremium(interaction.guild!.id) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 132 | ? "" |
| 133 | : "\n\nThis server does not have premium, so this feature is disabled") |
| 134 | ) |
| 135 | .setStatus("Success") |
| 136 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 137 | ], |
| 138 | components: [ |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 139 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 140 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 141 | .setCustomId("clear") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 142 | .setLabel(clicks ? "Click again to confirm" : "Reset channel") |
| 143 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 144 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 145 | .setDisabled(!channel) |
| 146 | ]) |
| 147 | ] |
| 148 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 149 | let i; |
| 150 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 151 | i = await m.awaitMessageComponent({ |
| 152 | time: 300000, |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 153 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 154 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 155 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 156 | timedOut = true; |
| 157 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 158 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 159 | await i.deferUpdate(); |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 160 | if ((i.component as unknown as ButtonInteraction).customId === "clear") { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 161 | clicks ++; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 162 | if (clicks === 2) { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 163 | clicks = 0; |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 164 | await client.database.guilds.write(interaction.guild!.id, null, ["logging.announcements.channel"]); |
| 165 | channel = null; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 166 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 167 | } |
| 168 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 169 | await interaction.editReply({ |
| 170 | embeds: [ |
| 171 | new EmojiEmbed() |
| 172 | .setTitle("Attachment Log Channel") |
| 173 | .setDescription( |
| 174 | channel |
| 175 | ? `Your attachment log channel is currently set to <#${channel}>` |
| 176 | : "This server does not have an attachment log channel" |
| 177 | ) |
| 178 | .setStatus("Success") |
| 179 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 180 | .setFooter({ text: "Message closed" }) |
| 181 | ], |
| 182 | components: [ |
TheCodedProf | c25369c | 2023-01-18 21:41:41 -0500 | [diff] [blame] | 183 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 184 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 185 | .setCustomId("clear") |
| 186 | .setLabel("Clear") |
| 187 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 188 | .setStyle(ButtonStyle.Secondary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 189 | .setDisabled(true) |
| 190 | ]) |
| 191 | ] |
| 192 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 193 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 194 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 195 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 196 | const member = interaction.member as Discord.GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 197 | if (!member.permissions.has("ManageGuild")) |
| 198 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 199 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 200 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 201 | |
| 202 | export { command }; |
| 203 | export { callback }; |
| 204 | export { check }; |