blob: 2f0c81998239b733244815de3dd8523731ffa87e [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
2import { ChannelType } from "discord-api-types";
pineafan708692b2022-07-24 22:16:22 +01003import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
4import 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";
8// eslint-disable-next-line @typescript-eslint/ban-ts-comment
9// @ts-ignore
10import type { WrappedCheck } from "jshaiku";
pineafan708692b2022-07-24 22:16:22 +010011import client from "../../../utils/client.js";
12
13const command = (builder: SlashCommandSubcommandBuilder) =>
14 builder
pineafan63fc5e22022-08-04 22:04:10 +010015 .setName("staff")
16 .setDescription("Settings for the staff notifications channel")
17 .addChannelOption(option => option.setName("channel").setDescription("The channel to set the staff notifications channel to").addChannelTypes([
18 ChannelType.GuildNews, ChannelType.GuildText
19 ]).setRequired(false));
pineafan708692b2022-07-24 22:16:22 +010020
pineafan63fc5e22022-08-04 22:04:10 +010021const callback = async (interaction: CommandInteraction): Promise<unknown | void> => {
22 if (!interaction.guild) return;
23 const m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}) as Discord.Message<boolean>;
pineafan708692b2022-07-24 22:16:22 +010024 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010025 let channel;
pineafan708692b2022-07-24 22:16:22 +010026 try {
pineafan63fc5e22022-08-04 22:04:10 +010027 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010028 } catch {
29 return await interaction.editReply({embeds: [new EmojiEmbed()
30 .setEmoji("CHANNEL.TEXT.DELETE")
31 .setTitle("Staff Notifications Channel")
32 .setDescription("The channel you provided is not a valid channel")
33 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010034 ]});
pineafan708692b2022-07-24 22:16:22 +010035 }
pineafan63fc5e22022-08-04 22:04:10 +010036 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010037 if (channel.guild.id !== interaction.guild.id) {
pineafan708692b2022-07-24 22:16:22 +010038 return interaction.editReply({embeds: [new EmojiEmbed()
39 .setTitle("Staff Notifications Channel")
pineafan63fc5e22022-08-04 22:04:10 +010040 .setDescription("You must choose a channel in this server")
pineafan708692b2022-07-24 22:16:22 +010041 .setStatus("Danger")
42 .setEmoji("CHANNEL.TEXT.DELETE")
43 ]});
44 }
pineafan63fc5e22022-08-04 22:04:10 +010045 const confirmation = await new confirmationMessage(interaction)
pineafan708692b2022-07-24 22:16:22 +010046 .setEmoji("CHANNEL.TEXT.EDIT")
47 .setTitle("Staff Notifications Channel")
48 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010049 "This will be the channel all notifications, updates, user reports etc. will be sent to.\n\n" +
pineafan708692b2022-07-24 22:16:22 +010050 `Are you sure you want to set the staff notifications channel to <#${channel.id}>?`
51 )
52 .setColor("Warning")
53 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010054 .send(true);
55 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010056 if (confirmation.success) {
57 try {
pineafan63fc5e22022-08-04 22:04:10 +010058 await client.database.guilds.write(interaction.guild.id, {"logging.staff.channel": channel.id});
59 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
60 const data = {
61 meta:{
62 type: "staffChannelUpdate",
63 displayName: "Staff Notifications Channel Updated",
64 calculateType: "nucleusSettingsUpdated",
65 color: NucleusColors.yellow,
66 emoji: "CHANNEL.TEXT.EDIT",
67 timestamp: new Date().getTime()
68 },
69 list: {
70 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
71 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
72 channel: entry(channel.id, renderChannel(channel))
73 },
74 hidden: {
75 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +010076 }
pineafan63fc5e22022-08-04 22:04:10 +010077 };
78 log(data);
pineafan708692b2022-07-24 22:16:22 +010079 } catch (e) {
80 return interaction.editReply({embeds: [new EmojiEmbed()
81 .setTitle("Staff Notifications Channel")
pineafan63fc5e22022-08-04 22:04:10 +010082 .setDescription("Something went wrong and the staff notifications channel could not be set")
pineafan708692b2022-07-24 22:16:22 +010083 .setStatus("Danger")
84 .setEmoji("CHANNEL.TEXT.DELETE")
85 ], components: []});
86 }
87 } else {
88 return interaction.editReply({embeds: [new EmojiEmbed()
89 .setTitle("Staff Notifications Channel")
pineafan63fc5e22022-08-04 22:04:10 +010090 .setDescription("No changes were made")
pineafan708692b2022-07-24 22:16:22 +010091 .setStatus("Success")
92 .setEmoji("CHANNEL.TEXT.CREATE")
93 ], components: []});
94 }
95 }
96 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +010097 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +010098 let channel = data.logging.staff.channel;
99 while (true) {
100 await interaction.editReply({embeds: [new EmojiEmbed()
101 .setTitle("Staff Notifications channel")
102 .setDescription(channel ? `Your staff notifications channel is currently set to <#${channel}>` : "This server does not have a staff notifications channel")
103 .setStatus("Success")
104 .setEmoji("CHANNEL.TEXT.CREATE")
105 ], components: [new MessageActionRow().addComponents([new MessageButton()
106 .setCustomId("clear")
107 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
108 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
109 .setStyle("DANGER")
110 .setDisabled(!channel)
111 ])]});
112 let i;
113 try {
114 i = await m.awaitMessageComponent({time: 300000});
pineafan63fc5e22022-08-04 22:04:10 +0100115 } catch(e) { break; }
116 i.deferUpdate();
117 if ((i.component as MessageButton).customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100118 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100119 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100120 clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100121 await client.database.guilds.write(interaction.guild.id, null, ["logging.staff.channel"]);
pineafan708692b2022-07-24 22:16:22 +0100122 channel = undefined;
123 }
124 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100125 break;
pineafan708692b2022-07-24 22:16:22 +0100126 }
127 }
128 await interaction.editReply({embeds: [new EmojiEmbed()
129 .setTitle("Staff Notifications channel")
130 .setDescription(channel ? `Your staff notifications channel is currently set to <#${channel}>` : "This server does not have a staff notifications channel")
131 .setStatus("Success")
132 .setEmoji("CHANNEL.TEXT.CREATE")
133 .setFooter({text: "Message closed"})
134 ], components: [new MessageActionRow().addComponents([new MessageButton()
135 .setCustomId("clear")
136 .setLabel("Clear")
137 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
138 .setStyle("SECONDARY")
139 .setDisabled(true)
140 ])]});
pineafan63fc5e22022-08-04 22:04:10 +0100141};
pineafan708692b2022-07-24 22:16:22 +0100142
pineafan63fc5e22022-08-04 22:04:10 +0100143const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
144 const member = (interaction.member as Discord.GuildMember);
145 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100146 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100147};
pineafan708692b2022-07-24 22:16:22 +0100148
149export { command };
150export { callback };
151export { check };