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