Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../../utils/defaults.js"; |
| 2 | import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelSelectMenuBuilder, ChannelType } from "discord.js"; |
| 3 | import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
| 4 | import getEmojiByName from "../../../utils/getEmojiByName.js"; |
| 5 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
| 6 | import client from "../../../utils/client.js"; |
| 7 | |
| 8 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 9 | builder |
| 10 | .setName("warnings") |
| 11 | .setDescription("Settings for the staff notifications channel") |
| 12 | |
| 13 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
| 14 | if (!interaction.guild) return; |
| 15 | await interaction.reply({ |
| 16 | embeds: LoadingEmbed, |
| 17 | ephemeral: true, |
| 18 | fetchReply: true |
| 19 | }) |
| 20 | |
| 21 | let data = await client.database.guilds.read(interaction.guild.id); |
| 22 | let channel = data.logging.staff.channel; |
| 23 | let closed = false; |
| 24 | do { |
| 25 | const channelMenu = new ActionRowBuilder<ChannelSelectMenuBuilder>() |
| 26 | .addComponents( |
| 27 | new ChannelSelectMenuBuilder() |
| 28 | .setCustomId("channel") |
| 29 | .setPlaceholder("Select a channel") |
| 30 | .setChannelTypes(ChannelType.GuildText) |
| 31 | ); |
| 32 | const buttons = new ActionRowBuilder<ButtonBuilder>() |
| 33 | .addComponents( |
| 34 | new ButtonBuilder() |
| 35 | .setCustomId("clear") |
| 36 | .setLabel("Clear") |
| 37 | .setStyle(ButtonStyle.Danger) |
| 38 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id") as Discord.APIMessageComponentEmoji) |
| 39 | .setDisabled(!channel), |
| 40 | new ButtonBuilder() |
| 41 | .setCustomId("save") |
| 42 | .setLabel("Save") |
| 43 | .setStyle(ButtonStyle.Success) |
| 44 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as Discord.APIMessageComponentEmoji) |
| 45 | .setDisabled(channel === data.logging.staff.channel) |
| 46 | ); |
| 47 | |
| 48 | const embed = new EmojiEmbed() |
| 49 | .setTitle("Staff Notifications Channel") |
| 50 | .setStatus("Success") |
| 51 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 52 | .setDescription( |
| 53 | `Logs which require an action from a moderator or administrator will be sent to this channel.\n` + |
| 54 | `**Channel:** ${channel ? `<#${channel}>` : "*None*"}\n` |
| 55 | ) |
| 56 | |
| 57 | await interaction.editReply({ |
| 58 | embeds: [embed], |
| 59 | components: [channelMenu, buttons] |
| 60 | }); |
| 61 | |
| 62 | let i: Discord.ButtonInteraction | Discord.SelectMenuInteraction; |
| 63 | try { |
| 64 | i = (await interaction.channel!.awaitMessageComponent({ |
| 65 | filter: (i: Discord.Interaction) => i.user.id === interaction.user.id, |
| 66 | time: 300000 |
| 67 | })) as Discord.ButtonInteraction | Discord.SelectMenuInteraction; |
| 68 | } catch (e) { |
| 69 | closed = true; |
| 70 | continue; |
| 71 | } |
| 72 | await i.deferUpdate(); |
| 73 | if(i.isButton()) { |
| 74 | switch (i.customId) { |
| 75 | case "clear": { |
| 76 | channel = null; |
| 77 | break; |
| 78 | } |
| 79 | case "save": { |
| 80 | await client.database.guilds.write(interaction.guild!.id, { |
| 81 | "logging.warnings.channel": channel |
| 82 | }); |
| 83 | data = await client.database.guilds.read(interaction.guild!.id); |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | } else { |
| 88 | channel = i.values[0]!; |
| 89 | } |
| 90 | } while (!closed); |
| 91 | |
| 92 | await interaction.deleteReply() |
| 93 | }; |
| 94 | |
| 95 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
| 96 | const member = interaction.member as Discord.GuildMember; |
| 97 | if (!member.permissions.has("ManageGuild")) |
| 98 | return "You must have the *Manage Server* permission to use this command"; |
| 99 | return true; |
| 100 | }; |
| 101 | |
| 102 | export { command }; |
| 103 | export { callback }; |
| 104 | export { check }; |