PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../../utils/defaults.js"; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 2 | import { ChannelType } from "discord-api-types/v9"; |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 3 | import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle, ButtonInteraction, ButtonComponent } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 5 | import confirmationMessage from "../../../utils/confirmationMessage.js"; |
| 6 | import getEmojiByName from "../../../utils/getEmojiByName.js"; |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 7 | import type { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 8 | import client from "../../../utils/client.js"; |
| 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
PineaFan | d471ccd | 2023-01-26 20:48:40 +0000 | [diff] [blame^] | 12 | .setName("general") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | .setDescription("Sets or shows the log channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 14 | .addChannelOption((option) => |
| 15 | option |
| 16 | .setName("channel") |
| 17 | .setDescription("The channel to set the log channel to") |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 18 | .addChannelTypes(ChannelType.GuildText) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | ); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 20 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 21 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 22 | const m = (await interaction.reply({ |
| 23 | embeds: LoadingEmbed, |
| 24 | ephemeral: true, |
| 25 | fetchReply: true |
| 26 | })) as Discord.Message; |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 27 | if (interaction.options.get("channel")?.channel) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | let channel; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 29 | try { |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 30 | channel = interaction.options.get("channel")?.channel; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 31 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 32 | return await interaction.editReply({ |
| 33 | embeds: [ |
| 34 | new EmojiEmbed() |
| 35 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 36 | .setTitle("Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 37 | .setDescription("The channel you provided is not a valid channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 38 | .setStatus("Danger") |
| 39 | ] |
| 40 | }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 41 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | channel = channel as Discord.TextChannel; |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 43 | if (channel.guild.id !== interaction.guild!.id) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 44 | return interaction.editReply({ |
| 45 | embeds: [ |
| 46 | new EmojiEmbed() |
| 47 | .setTitle("Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 48 | .setDescription("You must choose a channel in this server") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | .setStatus("Danger") |
| 50 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 51 | ] |
| 52 | }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 53 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | const confirmation = await new confirmationMessage(interaction) |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 55 | .setEmoji("CHANNEL.TEXT.EDIT") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 56 | .setTitle("Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 57 | .setDescription(`Are you sure you want to set the log channel to <#${channel.id}>?`) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 58 | .setColor("Warning") |
PineaFan | 5d98a4b | 2023-01-19 16:15:47 +0000 | [diff] [blame] | 59 | .setFailedMessage("No changes were made", "Success", "CHANNEL.TEXT.CREATE") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 60 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | .send(true); |
| 62 | if (confirmation.cancelled) return; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 63 | if (confirmation.success) { |
| 64 | try { |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 65 | await client.database.guilds.write(interaction.guild!.id, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 66 | "logging.logs.channel": channel.id |
| 67 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 68 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 69 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 71 | type: "logChannelUpdate", |
| 72 | displayName: "Log Channel Changed", |
| 73 | calculateType: "nucleusSettingsUpdated", |
| 74 | color: NucleusColors.yellow, |
| 75 | emoji: "CHANNEL.TEXT.EDIT", |
| 76 | timestamp: new Date().getTime() |
| 77 | }, |
| 78 | list: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 79 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 80 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 81 | channel: entry(channel.id, renderChannel(channel)) |
| 82 | }, |
| 83 | hidden: { |
| 84 | guild: channel.guild.id |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 85 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 86 | }; |
| 87 | log(data); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 88 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | console.log(e); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | return interaction.editReply({ |
| 91 | embeds: [ |
| 92 | new EmojiEmbed() |
| 93 | .setTitle("Log Channel") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 94 | .setDescription("Something went wrong and the log channel could not be set") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 95 | .setStatus("Danger") |
| 96 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 97 | ], |
| 98 | components: [] |
| 99 | }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 100 | } |
| 101 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | return interaction.editReply({ |
| 103 | embeds: [ |
| 104 | new EmojiEmbed() |
| 105 | .setTitle("Log Channel") |
| 106 | .setDescription("No changes were made") |
| 107 | .setStatus("Success") |
| 108 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 109 | ], |
| 110 | components: [] |
| 111 | }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | let clicks = 0; |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 115 | const data = await client.database.guilds.read(interaction.guild!.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 116 | let channel = data.logging.logs.channel; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 117 | let timedOut = false; |
| 118 | while (!timedOut) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 119 | await interaction.editReply({ |
| 120 | embeds: [ |
| 121 | new EmojiEmbed() |
| 122 | .setTitle("Log channel") |
| 123 | .setDescription( |
| 124 | channel |
| 125 | ? `Your log channel is currently set to <#${channel}>` |
| 126 | : "This server does not have a log channel" |
| 127 | ) |
| 128 | .setStatus("Success") |
| 129 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 130 | ], |
| 131 | components: [ |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 132 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 133 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 134 | .setCustomId("clear") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 135 | .setLabel(clicks ? "Click again to confirm" : "Reset channel") |
| 136 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 137 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 138 | .setDisabled(!channel) |
| 139 | ]) |
| 140 | ] |
| 141 | }); |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 142 | let i: ButtonInteraction; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 143 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 144 | i = await m.awaitMessageComponent({ |
| 145 | time: 300000, |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 146 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id } |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 147 | }) as ButtonInteraction; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 148 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 149 | timedOut = true; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 150 | } |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 151 | i = i! |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 152 | await i.deferUpdate(); |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 153 | if ((i.component as ButtonComponent).customId === "clear") { |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 154 | clicks ++; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 155 | if (clicks === 2) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 156 | clicks = 0; |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 157 | await client.database.guilds.write(interaction.guild!.id, null, ["logging.logs.channel"]); |
| 158 | channel = null; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 159 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 162 | await interaction.editReply({ |
| 163 | embeds: [ |
| 164 | new EmojiEmbed() |
| 165 | .setTitle("Log channel") |
| 166 | .setDescription( |
| 167 | channel |
| 168 | ? `Your log channel is currently set to <#${channel}>` |
| 169 | : "This server does not have a log channel" |
| 170 | ) |
| 171 | .setStatus("Success") |
| 172 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 173 | .setFooter({ text: "Message closed" }) |
| 174 | ], |
| 175 | components: [ |
TheCodedProf | 327016b | 2023-01-18 21:48:00 -0500 | [diff] [blame] | 176 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 177 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 178 | .setCustomId("clear") |
| 179 | .setLabel("Clear") |
| 180 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 181 | .setStyle(ButtonStyle.Secondary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 182 | .setDisabled(true) |
| 183 | ]) |
| 184 | ] |
| 185 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 186 | }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 187 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 188 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 189 | const member = interaction.member as Discord.GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 190 | if (!member.permissions.has("ManageGuild")) |
| 191 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 192 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 193 | }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 194 | |
| 195 | export { command }; |
| 196 | export { callback }; |
| 197 | export { check }; |