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