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