blob: 3cb7230c823d2cb41eb4714b452cdbce9d7ea063 [file] [log] [blame]
pineafan708692b2022-07-24 22:16:22 +01001import { ChannelType } from 'discord-api-types';
2import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
3import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
4import confirmationMessage from "../../../utils/confirmationMessage.js";
5import getEmojiByName from "../../../utils/getEmojiByName.js";
6import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
7import { WrappedCheck } from "jshaiku";
8import client from "../../../utils/client.js";
9
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
12 .setName("staff")
13 .setDescription("Settings for the staff notifications channel")
14 .addChannelOption(option => option.setName("channel").setDescription("The channel to set the staff notifications channel to").addChannelTypes([
15 ChannelType.GuildNews, ChannelType.GuildText
16 ]).setRequired(false))
17
18const callback = async (interaction: CommandInteraction): Promise<any> => {
19 let m;
20 m = await interaction.reply({embeds: [new EmojiEmbed()
21 .setTitle("Loading")
22 .setStatus("Danger")
23 .setEmoji("NUCLEUS.LOADING")
24 ], ephemeral: true, fetchReply: true});
25 if (interaction.options.getChannel("channel")) {
26 let channel
27 try {
28 channel = interaction.options.getChannel("channel")
29 } catch {
30 return await interaction.editReply({embeds: [new EmojiEmbed()
31 .setEmoji("CHANNEL.TEXT.DELETE")
32 .setTitle("Staff Notifications Channel")
33 .setDescription("The channel you provided is not a valid channel")
34 .setStatus("Danger")
35 ]})
36 }
37 channel = channel as Discord.TextChannel
38 if (channel.guild.id != interaction.guild.id) {
39 return interaction.editReply({embeds: [new EmojiEmbed()
40 .setTitle("Staff Notifications Channel")
41 .setDescription(`You must choose a channel in this server`)
42 .setStatus("Danger")
43 .setEmoji("CHANNEL.TEXT.DELETE")
44 ]});
45 }
46 let confirmation = await new confirmationMessage(interaction)
47 .setEmoji("CHANNEL.TEXT.EDIT")
48 .setTitle("Staff Notifications Channel")
49 .setDescription(
50 `This will be the channel all notifications, updates, user reports etc. will be sent to.\n\n` +
51 `Are you sure you want to set the staff notifications channel to <#${channel.id}>?`
52 )
53 .setColor("Warning")
54 .setInverted(true)
55 .send(true)
56 if (confirmation.cancelled) return
57 if (confirmation.success) {
58 try {
59 await client.database.guilds.write(interaction.guild.id, {"logging.staff.channel": channel.id})
60 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
61 try {
62 let data = {
63 meta:{
64 type: 'staffChannelUpdate',
65 displayName: 'Staff Notifications Channel Updated',
66 calculateType: 'nucleusSettingsUpdated',
67 color: NucleusColors.yellow,
68 emoji: "CHANNEL.TEXT.EDIT",
69 timestamp: new Date().getTime()
70 },
71 list: {
72 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
73 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
74 channel: entry(channel.id, renderChannel(channel)),
75 },
76 hidden: {
77 guild: interaction.guild.id
78 }
79 }
80 log(data);
81 } catch {}
82 } catch (e) {
83 return interaction.editReply({embeds: [new EmojiEmbed()
84 .setTitle("Staff Notifications Channel")
85 .setDescription(`Something went wrong and the staff notifications channel could not be set`)
86 .setStatus("Danger")
87 .setEmoji("CHANNEL.TEXT.DELETE")
88 ], components: []});
89 }
90 } else {
91 return interaction.editReply({embeds: [new EmojiEmbed()
92 .setTitle("Staff Notifications Channel")
93 .setDescription(`No changes were made`)
94 .setStatus("Success")
95 .setEmoji("CHANNEL.TEXT.CREATE")
96 ], components: []});
97 }
98 }
99 let clicks = 0;
100 let data = await client.database.guilds.read(interaction.guild.id);
101 let channel = data.logging.staff.channel;
102 while (true) {
103 await interaction.editReply({embeds: [new EmojiEmbed()
104 .setTitle("Staff Notifications channel")
105 .setDescription(channel ? `Your staff notifications channel is currently set to <#${channel}>` : "This server does not have a staff notifications channel")
106 .setStatus("Success")
107 .setEmoji("CHANNEL.TEXT.CREATE")
108 ], components: [new MessageActionRow().addComponents([new MessageButton()
109 .setCustomId("clear")
110 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
111 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
112 .setStyle("DANGER")
113 .setDisabled(!channel)
114 ])]});
115 let i;
116 try {
117 i = await m.awaitMessageComponent({time: 300000});
118 } catch(e) { break }
119 i.deferUpdate()
120 if (i.component.customId == "clear") {
121 clicks += 1;
122 if (clicks == 2) {
123 clicks = 0;
124 await client.database.guilds.write(interaction.guild.id, {}, ["logging.staff.channel"])
125 channel = undefined;
126 }
127 } else {
128 break
129 }
130 }
131 await interaction.editReply({embeds: [new EmojiEmbed()
132 .setTitle("Staff Notifications channel")
133 .setDescription(channel ? `Your staff notifications channel is currently set to <#${channel}>` : "This server does not have a staff notifications channel")
134 .setStatus("Success")
135 .setEmoji("CHANNEL.TEXT.CREATE")
136 .setFooter({text: "Message closed"})
137 ], components: [new MessageActionRow().addComponents([new MessageButton()
138 .setCustomId("clear")
139 .setLabel("Clear")
140 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
141 .setStyle("SECONDARY")
142 .setDisabled(true)
143 ])]});
144}
145
146const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
147 let member = (interaction.member as Discord.GuildMember)
148 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command"
149 return true;
150}
151
152export { command };
153export { callback };
154export { check };