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