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