pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js"; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 2 | import { ChannelType } from "discord-api-types/v9"; |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 3 | import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle } 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"; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame^] | 7 | import type { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
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; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 28 | if (interaction.options.getChannel("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 { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | channel = interaction.options.getChannel("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; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [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) |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 56 | .setEmoji("CHANNEL.TEXT.EDIT", "CHANNEL.TEXT.DELETE") |
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") |
| 63 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 64 | .send(true); |
| 65 | if (confirmation.cancelled) return; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 66 | if (confirmation.success) { |
| 67 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 68 | await client.database.guilds.write(interaction.guild.id, { |
| 69 | "logging.attachments.channel": channel.id |
| 70 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 71 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | type: "attachmentChannelUpdate", |
| 75 | displayName: "Attachment Log Channel Updated", |
| 76 | calculateType: "nucleusSettingsUpdated", |
| 77 | color: NucleusColors.yellow, |
| 78 | emoji: "CHANNEL.TEXT.EDIT", |
| 79 | timestamp: new Date().getTime() |
| 80 | }, |
| 81 | list: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 82 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 83 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 84 | channel: entry(channel.id, renderChannel(channel)) |
| 85 | }, |
| 86 | hidden: { |
| 87 | guild: interaction.guild.id |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 88 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | }; |
| 90 | log(data); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 91 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 92 | return interaction.editReply({ |
| 93 | embeds: [ |
| 94 | new EmojiEmbed() |
| 95 | .setTitle("Attachment Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 96 | .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] | 97 | .setStatus("Danger") |
| 98 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 99 | ], |
| 100 | components: [] |
| 101 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 102 | } |
| 103 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 104 | return interaction.editReply({ |
| 105 | embeds: [ |
| 106 | new EmojiEmbed() |
| 107 | .setTitle("Attachment Log Channel") |
| 108 | .setDescription("No changes were made") |
| 109 | .setStatus("Success") |
| 110 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 111 | ], |
| 112 | components: [] |
| 113 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | let clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 117 | const data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 118 | let channel = data.logging.staff.channel; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 119 | |
| 120 | let timedOut = false; |
| 121 | while (!timedOut) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 122 | await interaction.editReply({ |
| 123 | embeds: [ |
| 124 | new EmojiEmbed() |
| 125 | .setTitle("Attachment Log Channel") |
| 126 | .setDescription( |
| 127 | channel |
| 128 | ? `Your attachment log channel is currently set to <#${channel}>` |
| 129 | : "This server does not have an attachment log channel" + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 130 | (client.database.premium.hasPremium(interaction.guild.id) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 131 | ? "" |
| 132 | : "\n\nThis server does not have premium, so this feature is disabled") |
| 133 | ) |
| 134 | .setStatus("Success") |
| 135 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 136 | ], |
| 137 | components: [ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 138 | new ActionRowBuilder().addComponents([ |
| 139 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 140 | .setCustomId("clear") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 141 | .setLabel(clicks ? "Click again to confirm" : "Reset channel") |
| 142 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 143 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | .setDisabled(!channel) |
| 145 | ]) |
| 146 | ] |
| 147 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 148 | let i; |
| 149 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 150 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 151 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 152 | timedOut = true; |
| 153 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 154 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 155 | i.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 156 | if (i.component.customId === "clear") { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 157 | clicks += 1; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 158 | if (clicks === 2) { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 159 | clicks = 0; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 160 | await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 161 | channel = undefined; |
| 162 | } |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 165 | await interaction.editReply({ |
| 166 | embeds: [ |
| 167 | new EmojiEmbed() |
| 168 | .setTitle("Attachment Log Channel") |
| 169 | .setDescription( |
| 170 | channel |
| 171 | ? `Your attachment log channel is currently set to <#${channel}>` |
| 172 | : "This server does not have an attachment log channel" |
| 173 | ) |
| 174 | .setStatus("Success") |
| 175 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 176 | .setFooter({ text: "Message closed" }) |
| 177 | ], |
| 178 | components: [ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 179 | new ActionRowBuilder().addComponents([ |
| 180 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 181 | .setCustomId("clear") |
| 182 | .setLabel("Clear") |
| 183 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 184 | .setStyle(ButtonStyle.Secondary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 185 | .setDisabled(true) |
| 186 | ]) |
| 187 | ] |
| 188 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 189 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 190 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame^] | 191 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 192 | const member = interaction.member as Discord.GuildMember; |
| 193 | if (!member.permissions.has("MANAGE_GUILD")) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 194 | throw new Error("You must have the *Manage Server* permission to use this command"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 195 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 196 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 197 | |
| 198 | export { command }; |
| 199 | export { callback }; |
| 200 | export { check }; |