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