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