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"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 3 | import Discord, { CommandInteraction, MessageActionRow, MessageButton } 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"; |
| 7 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 8 | import { WrappedCheck } from "jshaiku"; |
| 9 | import client from "../../../utils/client.js"; |
| 10 | |
| 11 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 12 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | .setName("attachments") |
| 14 | .setDescription("Where attachments should be logged to (Premium only)") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 15 | .addChannelOption((option) => |
| 16 | option |
| 17 | .setName("channel") |
| 18 | .setDescription("The channel to log attachments in") |
| 19 | .addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText]) |
| 20 | .setRequired(false) |
| 21 | ); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 22 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 23 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | const m = (await interaction.reply({ |
| 25 | embeds: LoadingEmbed, |
| 26 | ephemeral: true, |
| 27 | fetchReply: true |
| 28 | })) as Discord.Message; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 29 | if (interaction.options.getChannel("channel")) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | let channel; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 31 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | channel = interaction.options.getChannel("channel"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 33 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | return await interaction.editReply({ |
| 35 | embeds: [ |
| 36 | new EmojiEmbed() |
| 37 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 38 | .setTitle("Attachment Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 39 | .setDescription("The channel you provided is not a valid channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 40 | .setStatus("Danger") |
| 41 | ] |
| 42 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 43 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | channel = channel as Discord.TextChannel; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 45 | if (channel.guild.id !== interaction.guild.id) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 46 | return interaction.editReply({ |
| 47 | embeds: [ |
| 48 | new EmojiEmbed() |
| 49 | .setTitle("Attachment Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 50 | .setDescription("You must choose a channel in this server") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 51 | .setStatus("Danger") |
| 52 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 53 | ] |
| 54 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 55 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 56 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 57 | .setEmoji("CHANNEL.TEXT.EDIT") |
| 58 | .setTitle("Attachment Log Channel") |
| 59 | .setDescription( |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | "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] | 61 | `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] | 62 | ) |
| 63 | .setColor("Warning") |
| 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 { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | await client.database.guilds.write(interaction.guild.id, { |
| 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: { |
| 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; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [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; |
| 120 | while (true) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | await interaction.editReply({ |
| 122 | embeds: [ |
| 123 | new EmojiEmbed() |
| 124 | .setTitle("Attachment Log Channel") |
| 125 | .setDescription( |
| 126 | channel |
| 127 | ? `Your attachment log channel is currently set to <#${channel}>` |
| 128 | : "This server does not have an attachment log channel" + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 129 | (client.database.premium.hasPremium(interaction.guild.id) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 130 | ? "" |
| 131 | : "\n\nThis server does not have premium, so this feature is disabled") |
| 132 | ) |
| 133 | .setStatus("Success") |
| 134 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 135 | ], |
| 136 | components: [ |
| 137 | new MessageActionRow().addComponents([ |
| 138 | new MessageButton() |
| 139 | .setCustomId("clear") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 140 | .setLabel(clicks ? "Click again to confirm" : "Reset channel") |
| 141 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 142 | .setStyle("DANGER") |
| 143 | .setDisabled(!channel) |
| 144 | ]) |
| 145 | ] |
| 146 | }); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 147 | let i; |
| 148 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 149 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 150 | } catch (e) { |
| 151 | break; |
| 152 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | i.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 154 | if (i.component.customId === "clear") { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 155 | clicks += 1; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 156 | if (clicks === 2) { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 157 | clicks = 0; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 158 | await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 159 | channel = undefined; |
| 160 | } |
| 161 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 162 | break; |
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: [ |
| 179 | new MessageActionRow().addComponents([ |
| 180 | new MessageButton() |
| 181 | .setCustomId("clear") |
| 182 | .setLabel("Clear") |
| 183 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 184 | .setStyle("SECONDARY") |
| 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 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 191 | const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
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")) |
| 194 | throw "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 }; |