blob: bba64411e8ac94f002e0a5c54bec454287b58b9e [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LoadingEmbed } from "../../../utils/defaults.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +01002import { ChannelType } from "discord-api-types/v9";
TheCodedProf327016b2023-01-18 21:48:00 -05003import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle, ButtonComponent } 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";
pineafan708692b2022-07-24 22:16:22 +01008import client from "../../../utils/client.js";
9
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("staff")
13 .setDescription("Settings for the staff notifications channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010014 .addChannelOption((option) =>
15 option
16 .setName("channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010017 .setDescription("The channel to set the staff notifications channel to")
PineaFan64486c42022-12-28 09:21:04 +000018 .addChannelTypes(ChannelType.GuildText)
Skyler Grey75ea9172022-08-06 10:22:23 +010019 .setRequired(false)
20 );
pineafan708692b2022-07-24 22:16:22 +010021
pineafan3a02ea32022-08-11 21:35:04 +010022const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010023 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010024 const m = (await interaction.reply({
25 embeds: LoadingEmbed,
26 ephemeral: true,
27 fetchReply: true
28 })) as Discord.Message;
TheCodedProf327016b2023-01-18 21:48:00 -050029 if (interaction.options.get("channel")?.channel) {
pineafan63fc5e22022-08-04 22:04:10 +010030 let channel;
pineafan708692b2022-07-24 22:16:22 +010031 try {
TheCodedProf327016b2023-01-18 21:48:00 -050032 channel = interaction.options.get("channel")?.channel;
pineafan708692b2022-07-24 22:16:22 +010033 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010034 return await interaction.editReply({
35 embeds: [
36 new EmojiEmbed()
37 .setEmoji("CHANNEL.TEXT.DELETE")
38 .setTitle("Staff Notifications Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010039 .setDescription("The channel you provided is not a valid channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010040 .setStatus("Danger")
41 ]
42 });
pineafan708692b2022-07-24 22:16:22 +010043 }
pineafan63fc5e22022-08-04 22:04:10 +010044 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010045 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010046 return interaction.editReply({
47 embeds: [
48 new EmojiEmbed()
49 .setTitle("Staff Notifications Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010050 .setDescription("You must choose a channel in this server")
Skyler Grey75ea9172022-08-06 10:22:23 +010051 .setStatus("Danger")
52 .setEmoji("CHANNEL.TEXT.DELETE")
53 ]
54 });
pineafan708692b2022-07-24 22:16:22 +010055 }
pineafan63fc5e22022-08-04 22:04:10 +010056 const confirmation = await new confirmationMessage(interaction)
TheCodedProf327016b2023-01-18 21:48:00 -050057 .setEmoji("CHANNEL.TEXT.EDIT")
pineafan708692b2022-07-24 22:16:22 +010058 .setTitle("Staff Notifications Channel")
59 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010060 "This will be the channel all notifications, updates, user reports etc. will be sent to.\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +010061 `Are you sure you want to set the staff notifications channel to <#${channel.id}>?`
pineafan708692b2022-07-24 22:16:22 +010062 )
63 .setColor("Warning")
PineaFan5d98a4b2023-01-19 16:15:47 +000064 .setFailedMessage("No changes were made", "Success", "CHANNEL.TEXT.CREATE")
pineafan708692b2022-07-24 22:16:22 +010065 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010066 .send(true);
67 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010068 if (confirmation.success) {
69 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010070 await client.database.guilds.write(interaction.guild.id, {
71 "logging.staff.channel": channel.id
72 });
Skyler Grey11236ba2022-08-08 21:13:33 +010073 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010074 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010075 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010076 type: "staffChannelUpdate",
77 displayName: "Staff Notifications Channel Updated",
78 calculateType: "nucleusSettingsUpdated",
79 color: NucleusColors.yellow,
80 emoji: "CHANNEL.TEXT.EDIT",
81 timestamp: new Date().getTime()
82 },
83 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +010084 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
85 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010086 channel: entry(channel.id, renderChannel(channel))
87 },
88 hidden: {
89 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010090 }
pineafan63fc5e22022-08-04 22:04:10 +010091 };
92 log(data);
pineafan708692b2022-07-24 22:16:22 +010093 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 return interaction.editReply({
95 embeds: [
96 new EmojiEmbed()
97 .setTitle("Staff Notifications Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010098 .setDescription("Something went wrong and the staff notifications channel could not be set")
Skyler Grey75ea9172022-08-06 10:22:23 +010099 .setStatus("Danger")
100 .setEmoji("CHANNEL.TEXT.DELETE")
101 ],
102 components: []
103 });
pineafan708692b2022-07-24 22:16:22 +0100104 }
105 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 return interaction.editReply({
107 embeds: [
108 new EmojiEmbed()
109 .setTitle("Staff Notifications Channel")
110 .setDescription("No changes were made")
111 .setStatus("Success")
112 .setEmoji("CHANNEL.TEXT.CREATE")
113 ],
114 components: []
115 });
pineafan708692b2022-07-24 22:16:22 +0100116 }
117 }
118 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100119 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +0100120 let channel = data.logging.staff.channel;
Skyler Greyad002172022-08-16 18:48:26 +0100121 let timedOut = false;
122 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 await interaction.editReply({
124 embeds: [
125 new EmojiEmbed()
126 .setTitle("Staff Notifications channel")
127 .setDescription(
128 channel
129 ? `Your staff notifications channel is currently set to <#${channel}>`
130 : "This server does not have a staff notifications channel"
131 )
132 .setStatus("Success")
133 .setEmoji("CHANNEL.TEXT.CREATE")
134 ],
135 components: [
TheCodedProf327016b2023-01-18 21:48:00 -0500136 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400137 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100138 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100139 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
140 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400141 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 .setDisabled(!channel)
143 ])
144 ]
145 });
pineafan708692b2022-07-24 22:16:22 +0100146 let i;
147 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000148 i = await m.awaitMessageComponent({
149 time: 300000,
TheCodedProf267563a2023-01-21 17:00:57 -0500150 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id }
PineaFan0d06edc2023-01-17 22:10:31 +0000151 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100153 timedOut = true;
154 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100155 }
TheCodedProf267563a2023-01-21 17:00:57 -0500156 await i.deferUpdate();
TheCodedProf327016b2023-01-18 21:48:00 -0500157 if ((i.component as ButtonComponent).customId === "clear") {
TheCodedProf4a6d5712023-01-19 15:54:40 -0500158 clicks ++;
pineafane23c4ec2022-07-27 21:56:27 +0100159 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100160 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100161 await client.database.guilds.write(interaction.guild.id, null, ["logging.staff.channel"]);
TheCodedProf327016b2023-01-18 21:48:00 -0500162 channel = null;
pineafan708692b2022-07-24 22:16:22 +0100163 }
pineafan708692b2022-07-24 22:16:22 +0100164 }
165 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100166 await interaction.editReply({
167 embeds: [
168 new EmojiEmbed()
169 .setTitle("Staff Notifications channel")
170 .setDescription(
171 channel
172 ? `Your staff notifications channel is currently set to <#${channel}>`
173 : "This server does not have a staff notifications channel"
174 )
175 .setStatus("Success")
176 .setEmoji("CHANNEL.TEXT.CREATE")
177 .setFooter({ text: "Message closed" })
178 ],
179 components: [
TheCodedProf327016b2023-01-18 21:48:00 -0500180 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400181 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 .setCustomId("clear")
183 .setLabel("Clear")
184 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400185 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 .setDisabled(true)
187 ])
188 ]
189 });
pineafan63fc5e22022-08-04 22:04:10 +0100190};
pineafan708692b2022-07-24 22:16:22 +0100191
PineaFan64486c42022-12-28 09:21:04 +0000192const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100193 const member = interaction.member as Discord.GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000194 if (!member.permissions.has("ManageGuild"))
195 return "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100196 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100197};
pineafan708692b2022-07-24 22:16:22 +0100198
199export { command };
200export { callback };
201export { check };