blob: 5a23839f8a3f7c885bba7b4fef69a3aed942218f [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";
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";
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;
pineafan708692b2022-07-24 22:16:22 +010029 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010030 let channel;
pineafan708692b2022-07-24 22:16:22 +010031 try {
pineafan63fc5e22022-08-04 22:04:10 +010032 channel = interaction.options.getChannel("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)
pineafan62ce1922022-08-25 20:34:45 +010057 .setEmoji("CHANNEL.TEXT.EDIT", "CHANNEL.TEXT.DELETE")
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")
64 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010065 .send(true);
66 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010067 if (confirmation.success) {
68 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 await client.database.guilds.write(interaction.guild.id, {
70 "logging.staff.channel": channel.id
71 });
Skyler Grey11236ba2022-08-08 21:13:33 +010072 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010073 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010074 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010075 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 Grey11236ba2022-08-08 21:13:33 +010083 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
84 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010085 channel: entry(channel.id, renderChannel(channel))
86 },
87 hidden: {
88 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010089 }
pineafan63fc5e22022-08-04 22:04:10 +010090 };
91 log(data);
pineafan708692b2022-07-24 22:16:22 +010092 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010093 return interaction.editReply({
94 embeds: [
95 new EmojiEmbed()
96 .setTitle("Staff Notifications Channel")
Skyler Grey11236ba2022-08-08 21:13:33 +010097 .setDescription("Something went wrong and the staff notifications channel could not be set")
Skyler Grey75ea9172022-08-06 10:22:23 +010098 .setStatus("Danger")
99 .setEmoji("CHANNEL.TEXT.DELETE")
100 ],
101 components: []
102 });
pineafan708692b2022-07-24 22:16:22 +0100103 }
104 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 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 });
pineafan708692b2022-07-24 22:16:22 +0100115 }
116 }
117 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100118 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +0100119 let channel = data.logging.staff.channel;
Skyler Greyad002172022-08-16 18:48:26 +0100120 let timedOut = false;
121 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 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: [
TheCodedProf21c08592022-09-13 14:14:43 -0400135 new ActionRowBuilder().addComponents([
136 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100138 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
139 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400140 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 .setDisabled(!channel)
142 ])
143 ]
144 });
pineafan708692b2022-07-24 22:16:22 +0100145 let i;
146 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000147 i = await m.awaitMessageComponent({
148 time: 300000,
149 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id }
150 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100151 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100152 timedOut = true;
153 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100154 }
pineafan63fc5e22022-08-04 22:04:10 +0100155 i.deferUpdate();
TheCodedProf21c08592022-09-13 14:14:43 -0400156 if ((i.component as ButtonBuilder).customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100157 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100158 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100159 clicks = 0;
Skyler Grey11236ba2022-08-08 21:13:33 +0100160 await client.database.guilds.write(interaction.guild.id, null, ["logging.staff.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100161 channel = undefined;
162 }
pineafan708692b2022-07-24 22:16:22 +0100163 }
164 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100165 await interaction.editReply({
166 embeds: [
167 new EmojiEmbed()
168 .setTitle("Staff Notifications channel")
169 .setDescription(
170 channel
171 ? `Your staff notifications channel is currently set to <#${channel}>`
172 : "This server does not have a staff notifications channel"
173 )
174 .setStatus("Success")
175 .setEmoji("CHANNEL.TEXT.CREATE")
176 .setFooter({ text: "Message closed" })
177 ],
178 components: [
TheCodedProf21c08592022-09-13 14:14:43 -0400179 new ActionRowBuilder().addComponents([
180 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100181 .setCustomId("clear")
182 .setLabel("Clear")
183 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
TheCodedProf21c08592022-09-13 14:14:43 -0400184 .setStyle(ButtonStyle.Secondary)
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 .setDisabled(true)
186 ])
187 ]
188 });
pineafan63fc5e22022-08-04 22:04:10 +0100189};
pineafan708692b2022-07-24 22:16:22 +0100190
PineaFan64486c42022-12-28 09:21:04 +0000191const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 const member = interaction.member as Discord.GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000193 if (!member.permissions.has("ManageGuild"))
194 return "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100195 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100196};
pineafan708692b2022-07-24 22:16:22 +0100197
198export { command };
199export { callback };
200export { check };