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