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