PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../../utils/defaults.js"; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 2 | import Discord, { CommandInteraction, ActionRowBuilder, ChannelSelectMenuBuilder, ChannelType, ButtonBuilder, ButtonStyle, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, ButtonInteraction, StringSelectMenuInteraction, ChannelSelectMenuInteraction, APIMessageComponentEmoji } from "discord.js"; |
| 3 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | import client from "../../../utils/client.js"; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 5 | import compare from "lodash"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 6 | import { toHexArray, toHexInteger } from "../../../utils/calculate.js"; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 7 | import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
| 8 | import getEmojiByName from "../../../utils/getEmojiByName.js"; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 9 | |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 10 | const logs: Record<string, string> = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 11 | channelUpdate: "Channels created, deleted or modified", |
| 12 | emojiUpdate: "Server emojis modified", |
| 13 | stickerUpdate: "Server stickers modified", |
| 14 | guildUpdate: "Server settings updated", |
| 15 | guildMemberUpdate: "Member updated (i.e. nickname)", |
| 16 | guildMemberPunish: "Members punished (i.e. muted, banned, kicked)", |
| 17 | guildRoleUpdate: "Role settings changed", |
| 18 | guildInviteUpdate: "Server invite created or deleted", |
| 19 | messageUpdate: "Message edited", |
| 20 | messageDelete: "Message deleted", |
| 21 | messageDeleteBulk: "Messages purged", |
| 22 | messageReactionUpdate: "Message reactions cleared", |
| 23 | messageMassPing: "Message pings multiple members at once", |
| 24 | messageAnnounce: "Message published in announcement channel", |
| 25 | threadUpdate: "Thread created or deleted", |
| 26 | webhookUpdate: "Webhooks created or deleted", |
| 27 | guildMemberVerify: "Member runs verify", |
| 28 | autoModeratorDeleted: "Messages auto deleted by Nucleus", |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 29 | ticketUpdate: "Tickets created or deleted", |
| 30 | //nucleusSettingsUpdated: "Nucleus' settings updated by a moderator" // TODO |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 32 | |
| 33 | const command = (builder: SlashCommandSubcommandBuilder) => |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 34 | builder |
| 35 | .setName("events") |
| 36 | .setDescription("The general log channel for the server, and setting what events to show") |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 37 | |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 38 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 39 | const m = (await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 40 | embeds: LoadingEmbed, |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 41 | ephemeral: true, |
| 42 | fetchReply: true |
| 43 | })) as Discord.Message; |
| 44 | |
| 45 | let config = await client.database.guilds.read(interaction.guild!.id); |
| 46 | let data = Object.assign({}, config.logging.logs); |
| 47 | let closed = false; |
| 48 | let show = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 49 | do { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 50 | const channelMenu = new ActionRowBuilder<ChannelSelectMenuBuilder>() |
| 51 | .addComponents( |
| 52 | new ChannelSelectMenuBuilder() |
| 53 | .setCustomId("channel") |
| 54 | .setPlaceholder("Select a channel") |
| 55 | .setChannelTypes(ChannelType.GuildText) |
| 56 | ) |
| 57 | const buttons = new ActionRowBuilder<ButtonBuilder>() |
| 58 | .addComponents( |
| 59 | new ButtonBuilder() |
| 60 | .setCustomId("switch") |
| 61 | .setLabel(data.enabled ? "Enabled" : "Disabled") |
| 62 | .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger) |
| 63 | .setEmoji(getEmojiByName((data.enabled ? "CONTROL.TICK" : "CONTROL.CROSS"), "id") as APIMessageComponentEmoji), |
| 64 | new ButtonBuilder() |
| 65 | .setCustomId("remove") |
| 66 | .setLabel("Remove") |
| 67 | .setStyle(ButtonStyle.Danger) |
| 68 | .setDisabled(!data.channel), |
| 69 | new ButtonBuilder() |
| 70 | .setCustomId("show") |
| 71 | .setLabel("Manage Events") |
| 72 | .setStyle(ButtonStyle.Primary), |
| 73 | new ButtonBuilder() |
| 74 | .setCustomId("save") |
| 75 | .setLabel("Save") |
| 76 | .setStyle(ButtonStyle.Success) |
| 77 | .setDisabled(compare.isEqual(data, config.logging.logs)) |
| 78 | ) |
| 79 | |
| 80 | const converted = toHexArray(data.toLog); |
| 81 | const toLogMenu = new ActionRowBuilder<StringSelectMenuBuilder>() |
| 82 | .addComponents( |
| 83 | new StringSelectMenuBuilder() |
| 84 | .setPlaceholder("Set events to log") |
| 85 | .setMaxValues(Object.keys(logs).length) |
| 86 | .setCustomId("logs") |
| 87 | .setMinValues(0) |
| 88 | ) |
| 89 | Object.keys(logs).map((e) => { |
| 90 | toLogMenu.components[0]!.addOptions( |
| 91 | new StringSelectMenuOptionBuilder() |
| 92 | .setLabel(logs[e]!) |
| 93 | .setValue(e) |
| 94 | .setDefault(converted.includes(e)) |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 95 | ) |
| 96 | }); |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 97 | |
| 98 | const embed = new EmojiEmbed() |
| 99 | .setTitle("General Log Channel") |
| 100 | .setStatus("Success") |
| 101 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 102 | .setDescription( |
| 103 | `This is the channel that all events you set to be logged will be stored\n` + |
| 104 | `**Channel:** ${data.channel ? `<#${data.channel}>` : "None"}\n` |
| 105 | ) |
| 106 | |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 107 | const components: ActionRowBuilder<ButtonBuilder | ChannelSelectMenuBuilder | StringSelectMenuBuilder>[] = [channelMenu, buttons]; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 108 | if(show) components.push(toLogMenu); |
| 109 | |
| 110 | await interaction.editReply({ |
| 111 | embeds: [embed], |
| 112 | components: components |
| 113 | }); |
| 114 | |
| 115 | let i: ButtonInteraction | StringSelectMenuInteraction | ChannelSelectMenuInteraction; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 116 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 117 | i = await m.awaitMessageComponent({ |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 118 | filter: (i) => i.user.id === interaction.user.id, |
| 119 | time: 300000 |
| 120 | }) as ButtonInteraction | StringSelectMenuInteraction | ChannelSelectMenuInteraction; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 121 | } catch (e) { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 122 | closed = true; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 123 | continue; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 124 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 125 | |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 126 | await i.deferUpdate(); |
| 127 | |
| 128 | if(i.isButton()) { |
| 129 | switch(i.customId) { |
| 130 | case "show": { |
| 131 | show = !show; |
| 132 | break; |
| 133 | } |
| 134 | case "switch": { |
| 135 | data.enabled = !data.enabled; |
| 136 | break; |
| 137 | } |
| 138 | case "save": { |
| 139 | await client.database.guilds.write(interaction.guild!.id, {"logging.logs": data}); |
| 140 | config = await client.database.guilds.read(interaction.guild!.id); |
| 141 | data = Object.assign({}, config.logging.logs); |
| 142 | break; |
| 143 | } |
| 144 | case "remove": { |
| 145 | data.channel = null; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } else if(i.isStringSelectMenu()) { |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 150 | const hex = toHexInteger(i.values); |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 151 | data.toLog = hex; |
| 152 | } else if(i.isChannelSelectMenu()) { |
| 153 | data.channel = i.values[0]!; |
| 154 | } |
| 155 | |
| 156 | } while (!closed); |
| 157 | await interaction.deleteReply() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 158 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 159 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 160 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 161 | const member = interaction.member as Discord.GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 162 | if (!member.permissions.has("ManageGuild")) |
| 163 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 164 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 165 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 166 | |
| 167 | export { command }; |
| 168 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 169 | export { check }; |