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