blob: 44b8d691fb9008b959ef34bf0b2e413bf80d0a7b [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +01002import { ChannelType } from "discord-api-types/v9";
TheCodedProf21c08592022-09-13 14:14:43 -04003import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
pineafan708692b2022-07-24 22:16:22 +01004import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
5import confirmationMessage from "../../../utils/confirmationMessage.js";
6import getEmojiByName from "../../../utils/getEmojiByName.js";
pineafan63fc5e22022-08-04 22:04:10 +01007import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
Skyler Grey75ea9172022-08-06 10:22:23 +01008// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01009import type { WrappedCheck } from "jshaiku";
pineafan708692b2022-07-24 22:16:22 +010010import client from "../../../utils/client.js";
11
12const command = (builder: SlashCommandSubcommandBuilder) =>
13 builder
pineafan63fc5e22022-08-04 22:04:10 +010014 .setName("staff")
15 .setDescription("Settings for the staff notifications channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010016 .addChannelOption((option) =>
17 option
18 .setName("channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010019 .setDescription("The channel to set the staff notifications channel to")
Skyler Grey75ea9172022-08-06 10:22:23 +010020 .addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText])
21 .setRequired(false)
22 );
pineafan708692b2022-07-24 22:16:22 +010023
pineafan3a02ea32022-08-11 21:35:04 +010024const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010025 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010026 const m = (await interaction.reply({
27 embeds: LoadingEmbed,
28 ephemeral: true,
29 fetchReply: true
30 })) as Discord.Message;
pineafan708692b2022-07-24 22:16:22 +010031 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010032 let channel;
pineafan708692b2022-07-24 22:16:22 +010033 try {
pineafan63fc5e22022-08-04 22:04:10 +010034 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010035 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010036 return await interaction.editReply({
37 embeds: [
38 new EmojiEmbed()
39 .setEmoji("CHANNEL.TEXT.DELETE")
40 .setTitle("Staff Notifications Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010041 .setDescription("The channel you provided is not a valid channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010042 .setStatus("Danger")
43 ]
44 });
pineafan708692b2022-07-24 22:16:22 +010045 }
pineafan63fc5e22022-08-04 22:04:10 +010046 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010047 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010048 return interaction.editReply({
49 embeds: [
50 new EmojiEmbed()
51 .setTitle("Staff Notifications Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010052 .setDescription("You must choose a channel in this server")
Skyler Grey75ea9172022-08-06 10:22:23 +010053 .setStatus("Danger")
54 .setEmoji("CHANNEL.TEXT.DELETE")
55 ]
56 });
pineafan708692b2022-07-24 22:16:22 +010057 }
pineafan63fc5e22022-08-04 22:04:10 +010058 const confirmation = await new confirmationMessage(interaction)
pineafan62ce1922022-08-25 20:34:45 +010059 .setEmoji("CHANNEL.TEXT.EDIT", "CHANNEL.TEXT.DELETE")
pineafan708692b2022-07-24 22:16:22 +010060 .setTitle("Staff Notifications Channel")
61 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010062 "This will be the channel all notifications, updates, user reports etc. will be sent to.\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +010063 `Are you sure you want to set the staff notifications channel to <#${channel.id}>?`
pineafan708692b2022-07-24 22:16:22 +010064 )
65 .setColor("Warning")
66 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010067 .send(true);
68 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010069 if (confirmation.success) {
70 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010071 await client.database.guilds.write(interaction.guild.id, {
72 "logging.staff.channel": channel.id
73 });
Skyler Grey11236ba2022-08-08 21:13:33 +010074 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010075 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010076 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010077 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 Grey11236ba2022-08-08 21:13:33 +010085 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
86 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010087 channel: entry(channel.id, renderChannel(channel))
88 },
89 hidden: {
90 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010091 }
pineafan63fc5e22022-08-04 22:04:10 +010092 };
93 log(data);
pineafan708692b2022-07-24 22:16:22 +010094 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010095 return interaction.editReply({
96 embeds: [
97 new EmojiEmbed()
98 .setTitle("Staff Notifications Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010099 .setDescription("Something went wrong and the staff notifications channel could not be set")
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 .setStatus("Danger")
101 .setEmoji("CHANNEL.TEXT.DELETE")
102 ],
103 components: []
104 });
pineafan708692b2022-07-24 22:16:22 +0100105 }
106 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 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 });
pineafan708692b2022-07-24 22:16:22 +0100117 }
118 }
119 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100120 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +0100121 let channel = data.logging.staff.channel;
Skyler Greyad002172022-08-16 18:48:26 +0100122 let timedOut = false;
123 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 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: [
TheCodedProf21c08592022-09-13 14:14:43 -0400137 new ActionRowBuilder().addComponents([
138 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100140 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
141 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400142 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 .setDisabled(!channel)
144 ])
145 ]
146 });
pineafan708692b2022-07-24 22:16:22 +0100147 let i;
148 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 i = await m.awaitMessageComponent({ time: 300000 });
150 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100151 timedOut = true;
152 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100153 }
pineafan63fc5e22022-08-04 22:04:10 +0100154 i.deferUpdate();
TheCodedProf21c08592022-09-13 14:14:43 -0400155 if ((i.component as ButtonBuilder).customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100156 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100157 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100158 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100159 await client.database.guilds.write(interaction.guild.id, null, ["logging.staff.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100160 channel = undefined;
161 }
pineafan708692b2022-07-24 22:16:22 +0100162 }
163 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100164 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: [
TheCodedProf21c08592022-09-13 14:14:43 -0400178 new ActionRowBuilder().addComponents([
179 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100180 .setCustomId("clear")
181 .setLabel("Clear")
182 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400183 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 .setDisabled(true)
185 ])
186 ]
187 });
pineafan63fc5e22022-08-04 22:04:10 +0100188};
pineafan708692b2022-07-24 22:16:22 +0100189
Skyler Grey11236ba2022-08-08 21:13:33 +0100190const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100191 const member = interaction.member as Discord.GuildMember;
192 if (!member.permissions.has("MANAGE_GUILD"))
pineafan3a02ea32022-08-11 21:35:04 +0100193 throw new Error("You must have the *Manage Server* permission to use this command");
pineafan708692b2022-07-24 22:16:22 +0100194 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100195};
pineafan708692b2022-07-24 22:16:22 +0100196
197export { command };
198export { callback };
199export { check };