pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js"; |
| 2 | import { ChannelType } from "discord-api-types"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 3 | import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; |
| 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)") |
| 15 | .addChannelOption(option => option.setName("channel").setDescription("The channel to log attachments in").addChannelTypes([ |
| 16 | ChannelType.GuildNews, ChannelType.GuildText |
| 17 | ]).setRequired(false)); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 18 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 19 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
| 20 | const m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}) as Discord.Message; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 21 | if (interaction.options.getChannel("channel")) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 22 | let channel; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 23 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | channel = interaction.options.getChannel("channel"); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 25 | } catch { |
| 26 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 27 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 28 | .setTitle("Attachment Log Channel") |
| 29 | .setDescription("The channel you provided is not a valid channel") |
| 30 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | ]}); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 32 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | channel = channel as Discord.TextChannel; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 34 | if (channel.guild.id !== interaction.guild.id) { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 35 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 36 | .setTitle("Attachment Log Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | .setDescription("You must choose a channel in this server") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 38 | .setStatus("Danger") |
| 39 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 40 | ]}); |
| 41 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 43 | .setEmoji("CHANNEL.TEXT.EDIT") |
| 44 | .setTitle("Attachment Log Channel") |
| 45 | .setDescription( |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | "This will be the channel all attachments will be sent to.\n\n" + |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 47 | `Are you sure you want to set the attachment log channel to <#${channel.id}>?` |
| 48 | ) |
| 49 | .setColor("Warning") |
| 50 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | .send(true); |
| 52 | if (confirmation.cancelled) return; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 53 | if (confirmation.success) { |
| 54 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | await client.database.guilds.write(interaction.guild.id, {"logging.attachments.channel": channel.id}); |
| 56 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
| 57 | const data = { |
| 58 | meta:{ |
| 59 | type: "attachmentChannelUpdate", |
| 60 | displayName: "Attachment Log Channel Updated", |
| 61 | calculateType: "nucleusSettingsUpdated", |
| 62 | color: NucleusColors.yellow, |
| 63 | emoji: "CHANNEL.TEXT.EDIT", |
| 64 | timestamp: new Date().getTime() |
| 65 | }, |
| 66 | list: { |
| 67 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 68 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 69 | channel: entry(channel.id, renderChannel(channel)) |
| 70 | }, |
| 71 | hidden: { |
| 72 | guild: interaction.guild.id |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 73 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | }; |
| 75 | log(data); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 76 | } catch (e) { |
| 77 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 78 | .setTitle("Attachment Log Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | .setDescription("Something went wrong and the attachment log channel could not be set") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 80 | .setStatus("Danger") |
| 81 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 82 | ], components: []}); |
| 83 | } |
| 84 | } else { |
| 85 | return interaction.editReply({embeds: [new EmojiEmbed() |
| 86 | .setTitle("Attachment Log Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | .setDescription("No changes were made") |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 88 | .setStatus("Success") |
| 89 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 90 | ], components: []}); |
| 91 | } |
| 92 | } |
| 93 | let clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | const data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 95 | let channel = data.logging.staff.channel; |
| 96 | while (true) { |
| 97 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 98 | .setTitle("Attachment Log Channel") |
| 99 | .setDescription( |
| 100 | channel ? `Your attachment log channel is currently set to <#${channel}>` : "This server does not have an attachment log channel" + |
| 101 | (client.database.premium.hasPremium(interaction.guild.id) ? "" : "\n\nThis server does not have premium, so this feature is disabled") |
| 102 | ) |
| 103 | .setStatus("Success") |
| 104 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 105 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 106 | .setCustomId("clear") |
| 107 | .setLabel(clicks ? "Click again to confirm" : "Reset channel") |
| 108 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
| 109 | .setStyle("DANGER") |
| 110 | .setDisabled(!channel) |
| 111 | ])]}); |
| 112 | let i; |
| 113 | try { |
| 114 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 115 | } catch(e) { break; } |
| 116 | i.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 117 | if (i.component.customId === "clear") { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 118 | clicks += 1; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 119 | if (clicks === 2) { |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 120 | clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 121 | await client.database.guilds.write(interaction.guild.id, null, ["logging.announcements.channel"]); |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 122 | channel = undefined; |
| 123 | } |
| 124 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 125 | break; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 129 | .setTitle("Attachment Log Channel") |
| 130 | .setDescription(channel ? `Your attachment log channel is currently set to <#${channel}>` : "This server does not have an attachment log channel") |
| 131 | .setStatus("Success") |
| 132 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 133 | .setFooter({text: "Message closed"}) |
| 134 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 135 | .setCustomId("clear") |
| 136 | .setLabel("Clear") |
| 137 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 138 | .setStyle("SECONDARY") |
| 139 | .setDisabled(true) |
| 140 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 141 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 142 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 143 | const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 144 | const member = (interaction.member as Discord.GuildMember); |
| 145 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command"; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 146 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 147 | }; |
pineafan | 708692b | 2022-07-24 22:16:22 +0100 | [diff] [blame] | 148 | |
| 149 | export { command }; |
| 150 | export { callback }; |
| 151 | export { check }; |