blob: 37c5ac895d3499e237647f2c946888d9d665016b [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LoadingEmbed } from "../../../utils/defaults.js";
Skyler Greyda16adf2023-03-05 10:22:12 +00002import Discord, {
3 CommandInteraction,
4 ActionRowBuilder,
5 ButtonBuilder,
6 ButtonStyle,
7 ChannelSelectMenuBuilder,
Skyler Grey0d885222023-03-08 21:46:37 +00008 ChannelType,
9 ComponentType
Skyler Greyda16adf2023-03-05 10:22:12 +000010} from "discord.js";
pineafan708692b2022-07-24 22:16:22 +010011import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
pineafan708692b2022-07-24 22:16:22 +010012import getEmojiByName from "../../../utils/getEmojiByName.js";
TheCodedProff86ba092023-01-27 17:10:07 -050013import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan708692b2022-07-24 22:16:22 +010014import client from "../../../utils/client.js";
Skyler Grey0d885222023-03-08 21:46:37 +000015import _ from "lodash";
pineafan708692b2022-07-24 22:16:22 +010016
17const command = (builder: SlashCommandSubcommandBuilder) =>
Skyler Greyda16adf2023-03-05 10:22:12 +000018 builder.setName("warnings").setDescription("Settings for the staff notifications channel");
pineafan708692b2022-07-24 22:16:22 +010019
pineafan3a02ea32022-08-11 21:35:04 +010020const callback = async (interaction: CommandInteraction): Promise<unknown> => {
pineafan63fc5e22022-08-04 22:04:10 +010021 if (!interaction.guild) return;
TheCodedProf80ad8542023-03-10 12:52:33 -050022 const m = await interaction.reply({
Skyler Grey75ea9172022-08-06 10:22:23 +010023 embeds: LoadingEmbed,
24 ephemeral: true,
25 fetchReply: true
Skyler Greyda16adf2023-03-05 10:22:12 +000026 });
TheCodedProf01cba762023-02-18 15:55:05 -050027
28 let data = await client.database.guilds.read(interaction.guild.id);
Skyler Grey0d885222023-03-08 21:46:37 +000029 let channel = _.clone(data.logging.staff.channel);
TheCodedProf01cba762023-02-18 15:55:05 -050030 let closed = false;
31 do {
Skyler Greyda16adf2023-03-05 10:22:12 +000032 const channelMenu = new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents(
TheCodedProf01cba762023-02-18 15:55:05 -050033 new ChannelSelectMenuBuilder()
34 .setCustomId("channel")
35 .setPlaceholder("Select a channel")
TheCodedProf94ff6de2023-02-22 17:47:26 -050036 .setChannelTypes(ChannelType.GuildText)
TheCodedProf01cba762023-02-18 15:55:05 -050037 );
Skyler Greyda16adf2023-03-05 10:22:12 +000038 const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
39 new ButtonBuilder()
40 .setCustomId("clear")
41 .setLabel("Clear")
42 .setStyle(ButtonStyle.Danger)
43 .setEmoji(getEmojiByName("CONTROL.CROSS", "id") as Discord.APIMessageComponentEmoji)
44 .setDisabled(!channel),
45 new ButtonBuilder()
46 .setCustomId("save")
47 .setLabel("Save")
48 .setStyle(ButtonStyle.Success)
49 .setEmoji(getEmojiByName("ICONS.SAVE", "id") as Discord.APIMessageComponentEmoji)
Skyler Grey0d885222023-03-08 21:46:37 +000050 .setDisabled(_.isEqual(channel, data.logging.staff.channel))
Skyler Greyda16adf2023-03-05 10:22:12 +000051 );
TheCodedProf01cba762023-02-18 15:55:05 -050052
53 const embed = new EmojiEmbed()
54 .setTitle("Staff Notifications Channel")
55 .setStatus("Success")
56 .setEmoji("CHANNEL.TEXT.CREATE")
57 .setDescription(
58 `Logs which require an action from a moderator or administrator will be sent to this channel.\n` +
Skyler Greyda16adf2023-03-05 10:22:12 +000059 `**Channel:** ${channel ? `<#${channel}>` : "*None*"}\n`
60 );
TheCodedProf01cba762023-02-18 15:55:05 -050061
62 await interaction.editReply({
63 embeds: [embed],
64 components: [channelMenu, buttons]
65 });
66
Skyler Grey0d885222023-03-08 21:46:37 +000067 let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
TheCodedProf01cba762023-02-18 15:55:05 -050068 try {
Skyler Greyea0937b2023-03-09 00:36:38 +000069 i = await interaction.channel!.awaitMessageComponent<ComponentType.Button | ComponentType.ChannelSelect>({
Skyler Grey21f52292023-03-10 17:58:30 +000070 filter: (i: Discord.Interaction) =>
71 i.user.id === interaction.user.id && i.isMessageComponent() && i.message.id === m.id,
TheCodedProf01cba762023-02-18 15:55:05 -050072 time: 300000
Skyler Greyea0937b2023-03-09 00:36:38 +000073 });
TheCodedProf01cba762023-02-18 15:55:05 -050074 } catch (e) {
75 closed = true;
TheCodedProf1807fb32023-02-20 14:33:48 -050076 continue;
TheCodedProf01cba762023-02-18 15:55:05 -050077 }
78 await i.deferUpdate();
Skyler Greyda16adf2023-03-05 10:22:12 +000079 if (i.isButton()) {
TheCodedProf01cba762023-02-18 15:55:05 -050080 switch (i.customId) {
81 case "clear": {
82 channel = null;
83 break;
84 }
85 case "save": {
86 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey0d885222023-03-08 21:46:37 +000087 "logging.staff.channel": channel
TheCodedProf01cba762023-02-18 15:55:05 -050088 });
89 data = await client.database.guilds.read(interaction.guild!.id);
Skyler Grey16ecb172023-03-05 07:30:32 +000090 await client.memory.forceUpdate(interaction.guild!.id);
TheCodedProf01cba762023-02-18 15:55:05 -050091 break;
92 }
93 }
94 } else {
95 channel = i.values[0]!;
96 }
97 } while (!closed);
98
Skyler Greyda16adf2023-03-05 10:22:12 +000099 await interaction.deleteReply();
pineafan63fc5e22022-08-04 22:04:10 +0100100};
pineafan708692b2022-07-24 22:16:22 +0100101
TheCodedProff86ba092023-01-27 17:10:07 -0500102const check = (interaction: CommandInteraction, _partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 const member = interaction.member as Discord.GuildMember;
PineaFan0d06edc2023-01-17 22:10:31 +0000104 if (!member.permissions.has("ManageGuild"))
105 return "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100106 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100107};
pineafan708692b2022-07-24 22:16:22 +0100108
109export { command };
110export { callback };
111export { check };