Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../utils/defaults.js"; |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 2 | import type { HistorySchema, FlagColors } from "../../utils/database.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 3 | import Discord, { |
| 4 | CommandInteraction, |
| 5 | GuildMember, |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 6 | Message, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 7 | ActionRowBuilder, |
| 8 | ButtonBuilder, |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 9 | MessageComponentInteraction, |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 10 | ButtonStyle, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 11 | TextInputStyle, |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 12 | APIMessageComponentEmoji, |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 13 | SlashCommandSubcommandBuilder, |
| 14 | StringSelectMenuBuilder, |
| 15 | StringSelectMenuOptionBuilder, |
| 16 | ContextMenuCommandInteraction |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 17 | } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 18 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 19 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 20 | import client from "../../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
| 22 | import pageIndicator from "../../utils/createPageIndicator.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 23 | |
| 24 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 25 | builder |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 26 | .setName("about") |
| 27 | // .setNameLocalizations({"ru": "info", "zh-CN": "history", "zh-TW": "notes", "pt-BR": "flags"}) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | .setDescription("Shows moderator information about a user") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 29 | .addUserOption((option) => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 30 | option.setName("user").setDescription("The user to get information about").setRequired(true) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 31 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 32 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 33 | const types: Record<string, { emoji: string; text: string }> = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | warn: { emoji: "PUNISH.WARN.YELLOW", text: "Warned" }, |
| 35 | mute: { emoji: "PUNISH.MUTE.YELLOW", text: "Muted" }, |
| 36 | unmute: { emoji: "PUNISH.MUTE.GREEN", text: "Unmuted" }, |
| 37 | join: { emoji: "MEMBER.JOIN", text: "Joined" }, |
| 38 | leave: { emoji: "MEMBER.LEAVE", text: "Left" }, |
| 39 | kick: { emoji: "MEMBER.KICK", text: "Kicked" }, |
| 40 | softban: { emoji: "PUNISH.SOFTBAN", text: "Softbanned" }, |
| 41 | ban: { emoji: "MEMBER.BAN", text: "Banned" }, |
| 42 | unban: { emoji: "MEMBER.UNBAN", text: "Unbanned" }, |
| 43 | purge: { emoji: "PUNISH.CLEARHISTORY", text: "Messages cleared" }, |
| 44 | nickname: { emoji: "PUNISH.NICKNAME.YELLOW", text: "Nickname changed" } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 45 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 46 | |
| 47 | function historyToString(history: HistorySchema) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 48 | if (!Object.keys(types).includes(history.type)) throw new Error("Invalid history type"); |
| 49 | let s = `${getEmojiByName(types[history.type]!.emoji)} ${history.amount ? history.amount + " " : ""}${ |
| 50 | types[history.type]!.text |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 51 | } on <t:${Math.round(history.occurredAt.getTime() / 1000)}:F>`; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 52 | if (history.moderator) { |
| 53 | s += ` by <@${history.moderator}>`; |
| 54 | } |
| 55 | if (history.reason) { |
| 56 | s += `\n**Reason:**\n> ${history.reason}`; |
| 57 | } |
| 58 | if (history.before) { |
| 59 | s += `\n**Before:**\n> ${history.before}`; |
| 60 | } |
| 61 | if (history.after) { |
| 62 | s += `\n**After:**\n> ${history.after}`; |
| 63 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 64 | return s + "\n"; |
| 65 | } |
| 66 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 67 | class TimelineSection { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 68 | name: string = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | content: { data: HistorySchema; rendered: string }[] = []; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 70 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | addContent = (content: { data: HistorySchema; rendered: string }) => { |
| 72 | this.content.push(content); |
| 73 | return this; |
| 74 | }; |
| 75 | contentLength = () => { |
| 76 | return this.content.reduce((acc, cur) => acc + cur.rendered.length, 0); |
| 77 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 78 | generateName = () => { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 79 | const first = Math.round(this.content[0]!.data.occurredAt.getTime() / 1000); |
| 80 | const last = Math.round(this.content[this.content.length - 1]!.data.occurredAt.getTime() / 1000); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 81 | if (first === last) { |
| 82 | return (this.name = `<t:${first}:F>`); |
| 83 | } |
| 84 | return (this.name = `<t:${first}:F> - <t:${last}:F>`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | const monthNames = [ |
| 89 | "January", |
| 90 | "February", |
| 91 | "March", |
| 92 | "April", |
| 93 | "May", |
| 94 | "June", |
| 95 | "July", |
| 96 | "August", |
| 97 | "September", |
| 98 | "October", |
| 99 | "November", |
| 100 | "December" |
| 101 | ]; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 102 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 103 | async function showHistory(member: Discord.GuildMember, interaction: CommandInteraction) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 104 | let currentYear = new Date().getFullYear(); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 105 | let pageIndex: number | null = null; |
| 106 | let history, current: TimelineSection; |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 107 | history = await client.database.history.read(member.guild.id, member.id, currentYear); |
| 108 | history = history |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 109 | .sort((a: { occurredAt: Date }, b: { occurredAt: Date }) => b.occurredAt.getTime() - a.occurredAt.getTime()) |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 110 | .reverse(); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 111 | let m: Message; |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 112 | let refresh = false; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 113 | let filteredTypes: string[] = []; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 114 | let openFilterPane = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 115 | let timedOut = false; |
| 116 | let showHistorySelected = false; |
| 117 | while (!timedOut && !showHistorySelected) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 118 | if (refresh) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 119 | history = await client.database.history.read(member.guild.id, member.id, currentYear); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 120 | history = history |
| 121 | .sort( |
| 122 | (a: { occurredAt: Date }, b: { occurredAt: Date }) => |
| 123 | b.occurredAt.getTime() - a.occurredAt.getTime() |
| 124 | ) |
| 125 | .reverse(); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 126 | if (openFilterPane) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 127 | let tempFilteredTypes = filteredTypes; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | if (filteredTypes.length === 0) { |
| 129 | tempFilteredTypes = Object.keys(types); |
| 130 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 131 | history = history.filter((h: { type: string }) => tempFilteredTypes.includes(h.type)); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 132 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 133 | refresh = false; |
| 134 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 135 | const groups: TimelineSection[] = []; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 136 | if (history.length > 0) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 137 | current = new TimelineSection(); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 138 | history.forEach((event: HistorySchema) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 139 | if (current.contentLength() + historyToString(event).length > 2000 || current.content.length === 5) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 140 | groups.push(current); |
| 141 | current.generateName(); |
| 142 | current = new TimelineSection(); |
| 143 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | current.addContent({ |
| 145 | data: event, |
| 146 | rendered: historyToString(event) |
| 147 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 148 | }); |
| 149 | current.generateName(); |
| 150 | groups.push(current); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 151 | if (pageIndex === null) { |
| 152 | pageIndex = groups.length - 1; |
| 153 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 154 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 155 | if (pageIndex === null) pageIndex = 0; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 156 | let components: (ActionRowBuilder<Discord.StringSelectMenuBuilder> | ActionRowBuilder<ButtonBuilder>)[] = []; |
| 157 | if (openFilterPane) |
| 158 | components = components.concat([ |
| 159 | new ActionRowBuilder<Discord.StringSelectMenuBuilder>().addComponents( |
| 160 | new Discord.StringSelectMenuBuilder() |
| 161 | .setMinValues(1) |
| 162 | .setMaxValues(Object.keys(types).length) |
| 163 | .setCustomId("filter") |
| 164 | .setPlaceholder("Select events to show") |
| 165 | .setOptions( |
| 166 | ...Object.entries(types).map(([key, value]) => |
| 167 | new Discord.StringSelectMenuOptionBuilder() |
| 168 | .setLabel(value.text) |
| 169 | .setValue(key) |
| 170 | .setDefault(filteredTypes.includes(key)) |
| 171 | .setEmoji(getEmojiByName(value.emoji, "id") as APIMessageComponentEmoji) |
| 172 | ) |
| 173 | ) |
| 174 | ) |
| 175 | ]); |
| 176 | components = components.concat([ |
| 177 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
| 178 | new ButtonBuilder() |
| 179 | .setCustomId("prevYear") |
| 180 | .setLabel((currentYear - 1).toString()) |
| 181 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 182 | .setStyle(ButtonStyle.Secondary), |
| 183 | new ButtonBuilder().setCustomId("prevPage").setLabel("Previous page").setStyle(ButtonStyle.Primary), |
| 184 | new ButtonBuilder().setCustomId("today").setLabel("Today").setStyle(ButtonStyle.Primary), |
| 185 | new ButtonBuilder() |
| 186 | .setCustomId("nextPage") |
| 187 | .setLabel("Next page") |
| 188 | .setStyle(ButtonStyle.Primary) |
| 189 | .setDisabled(pageIndex >= groups.length - 1 && currentYear === new Date().getFullYear()), |
| 190 | new ButtonBuilder() |
| 191 | .setCustomId("nextYear") |
| 192 | .setLabel((currentYear + 1).toString()) |
| 193 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 194 | .setStyle(ButtonStyle.Secondary) |
| 195 | .setDisabled(currentYear === new Date().getFullYear()) |
| 196 | ]) |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 197 | ]); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 198 | components = components.concat([ |
| 199 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
| 200 | new ButtonBuilder() |
| 201 | .setLabel("Mod notes") |
| 202 | .setCustomId("modNotes") |
| 203 | .setStyle(ButtonStyle.Primary) |
| 204 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")), |
| 205 | new ButtonBuilder() |
| 206 | .setLabel("Filter") |
| 207 | .setCustomId("openFilter") |
| 208 | .setStyle(openFilterPane ? ButtonStyle.Success : ButtonStyle.Primary) |
| 209 | .setEmoji(getEmojiByName("ICONS.FILTER", "id")) |
| 210 | ]) |
| 211 | ]); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 212 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 213 | const end = |
| 214 | "\n\nJanuary " + |
| 215 | currentYear.toString() + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 216 | pageIndicator(Math.max(groups.length, 1), groups.length === 0 ? 1 : pageIndex) + |
| 217 | (currentYear === new Date().getFullYear() ? monthNames[new Date().getMonth()] : "December") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 218 | " " + |
| 219 | currentYear.toString(); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 220 | if (groups.length > 0) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 221 | const toRender = groups[Math.min(pageIndex, groups.length - 1)]!; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 222 | m = await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 223 | embeds: [ |
| 224 | new EmojiEmbed() |
| 225 | .setEmoji("MEMBER.JOIN") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 226 | .setTitle("Moderation history for " + member.user.username) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 227 | .setDescription( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 228 | `**${toRender.name}**\n\n` + toRender.content.map((c) => c.rendered).join("\n") + end |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 229 | ) |
| 230 | .setStatus("Success") |
| 231 | .setFooter({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 232 | text: |
| 233 | openFilterPane && filteredTypes.length |
| 234 | ? "Filters are currently enabled" |
| 235 | : "No filters selected" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 236 | }) |
| 237 | ], |
| 238 | components: components |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 239 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 240 | } else { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 241 | m = await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 242 | embeds: [ |
| 243 | new EmojiEmbed() |
| 244 | .setEmoji("MEMBER.JOIN") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 245 | .setTitle("Moderation history for " + member.user.username) |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 246 | .setDescription(`**${currentYear}**\n\n*No events*`) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 247 | .setStatus("Success") |
| 248 | .setFooter({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 249 | text: |
| 250 | openFilterPane && filteredTypes.length |
| 251 | ? "Filters are currently enabled" |
| 252 | : "No filters selected" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 253 | }) |
| 254 | ], |
| 255 | components: components |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 256 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 257 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 258 | let i: MessageComponentInteraction; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 259 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 260 | i = await m.awaitMessageComponent({ |
| 261 | time: 300000, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 262 | filter: (i) => { |
| 263 | return ( |
| 264 | i.user.id === interaction.user.id && |
| 265 | i.channel!.id === interaction.channel!.id && |
| 266 | i.message.id === m.id |
| 267 | ); |
| 268 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 269 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 270 | } catch (e) { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 271 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 272 | embeds: [ |
| 273 | new EmojiEmbed() |
| 274 | .setEmoji("MEMBER.JOIN") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 275 | .setTitle("Moderation history for " + member.user.username) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 276 | .setDescription(m.embeds[0]!.description!) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 277 | .setStatus("Danger") |
| 278 | .setFooter({ text: "Message timed out" }) |
| 279 | ] |
| 280 | }); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 281 | timedOut = true; |
| 282 | continue; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 283 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 284 | await i.deferUpdate(); |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 285 | if (i.customId === "filter" && i.isStringSelectMenu()) { |
| 286 | filteredTypes = i.values; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 287 | pageIndex = null; |
| 288 | refresh = true; |
| 289 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 290 | if (i.customId === "prevYear") { |
| 291 | currentYear--; |
| 292 | pageIndex = null; |
| 293 | refresh = true; |
| 294 | } |
| 295 | if (i.customId === "nextYear") { |
| 296 | currentYear++; |
| 297 | pageIndex = null; |
| 298 | refresh = true; |
| 299 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 300 | if (i.customId === "prevPage") { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 301 | pageIndex!--; |
| 302 | if (pageIndex! < 0) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 303 | pageIndex = null; |
| 304 | currentYear--; |
| 305 | refresh = true; |
| 306 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 307 | } |
| 308 | if (i.customId === "nextPage") { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 309 | pageIndex!++; |
| 310 | if (pageIndex! >= groups.length) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 311 | pageIndex = 0; |
| 312 | currentYear++; |
| 313 | refresh = true; |
| 314 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 315 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 316 | if (i.customId === "today") { |
| 317 | currentYear = new Date().getFullYear(); |
| 318 | pageIndex = null; |
| 319 | refresh = true; |
| 320 | } |
| 321 | if (i.customId === "modNotes") { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 322 | showHistorySelected = true; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 323 | } |
| 324 | if (i.customId === "openFilter") { |
| 325 | openFilterPane = !openFilterPane; |
| 326 | refresh = true; |
| 327 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 328 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 329 | return timedOut ? 0 : 1; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Skyler Grey | f25451c | 2023-04-23 21:26:11 +0000 | [diff] [blame] | 332 | export const noteMenu = async ( |
| 333 | member: GuildMember, |
| 334 | interaction: CommandInteraction | ContextMenuCommandInteraction |
| 335 | ): Promise<unknown> => { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 336 | let m: Message; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 337 | await interaction.reply({ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 338 | embeds: LoadingEmbed, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 339 | ephemeral: true, |
| 340 | fetchReply: true |
| 341 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 342 | let note; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 343 | let timedOut = false; |
| 344 | while (!timedOut) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 345 | note = await client.database.notes.read(member.guild.id, member.id); |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 346 | const colors: Record<string, Discord.ColorResolvable> = { |
| 347 | none: "#424242", |
| 348 | red: "#F27878", |
| 349 | yellow: "#EDC575", |
| 350 | green: "#68D49E", |
| 351 | blue: "#6576CC", |
| 352 | purple: "#D46899", |
| 353 | gray: "#C4C4C4" |
Skyler Grey | f25451c | 2023-04-23 21:26:11 +0000 | [diff] [blame] | 354 | }; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 355 | m = (await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 356 | embeds: [ |
| 357 | new EmojiEmbed() |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 358 | .setEmoji(`ICONS.FLAGS.${(note?.flag ?? "none").toUpperCase()}`) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 359 | .setTitle("Mod notes for " + member.user.username) |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 360 | .setDescription(note?.note ? note.note : "*No note set*") |
| 361 | .setColor(colors[note?.flag ?? "none"]!) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 362 | ], |
| 363 | components: [ |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 364 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 365 | new ButtonBuilder() |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 366 | .setLabel(`${note?.note ? "Modify" : "Create"} note`) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 367 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 368 | .setCustomId("modify") |
| 369 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 370 | new ButtonBuilder() |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 371 | .setLabel("Moderation history") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 372 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 373 | .setCustomId("history") |
| 374 | .setEmoji(getEmojiByName("ICONS.HISTORY", "id")) |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 375 | ]), |
| 376 | new ActionRowBuilder<Discord.StringSelectMenuBuilder>().addComponents([ |
| 377 | new StringSelectMenuBuilder() |
| 378 | .setCustomId("flag") |
| 379 | .setPlaceholder("Select a flag") |
| 380 | .addOptions( |
| 381 | new StringSelectMenuOptionBuilder() |
| 382 | .setLabel("None") |
| 383 | .setDefault(!note?.flag) |
| 384 | .setValue("none") |
| 385 | .setDescription("Clear") |
| 386 | .setEmoji(getEmojiByName("ICONS.FLAGS.NONE", "id")), |
| 387 | new StringSelectMenuOptionBuilder() |
| 388 | .setLabel("Red") |
| 389 | .setDefault(note?.flag === "red") |
| 390 | .setValue("red") |
| 391 | .setEmoji(getEmojiByName("ICONS.FLAGS.RED", "id")), |
| 392 | new StringSelectMenuOptionBuilder() |
| 393 | .setLabel("Yellow") |
| 394 | .setDefault(note?.flag === "yellow") |
| 395 | .setValue("yellow") |
| 396 | .setEmoji(getEmojiByName("ICONS.FLAGS.YELLOW", "id")), |
| 397 | new StringSelectMenuOptionBuilder() |
| 398 | .setLabel("Green") |
| 399 | .setDefault(note?.flag === "green") |
| 400 | .setValue("green") |
| 401 | .setEmoji(getEmojiByName("ICONS.FLAGS.GREEN", "id")), |
| 402 | new StringSelectMenuOptionBuilder() |
| 403 | .setLabel("Blue") |
| 404 | .setDefault(note?.flag === "blue") |
| 405 | .setValue("blue") |
| 406 | .setEmoji(getEmojiByName("ICONS.FLAGS.BLUE", "id")), |
| 407 | new StringSelectMenuOptionBuilder() |
| 408 | .setLabel("Purple") |
| 409 | .setDefault(note?.flag === "purple") |
| 410 | .setValue("purple") |
| 411 | .setEmoji(getEmojiByName("ICONS.FLAGS.PURPLE", "id")), |
| 412 | new StringSelectMenuOptionBuilder() |
| 413 | .setLabel("Gray") |
| 414 | .setDefault(note?.flag === "gray") |
| 415 | .setValue("gray") |
Skyler Grey | f25451c | 2023-04-23 21:26:11 +0000 | [diff] [blame] | 416 | .setEmoji(getEmojiByName("ICONS.FLAGS.GRAY", "id")) |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 417 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 418 | ]) |
| 419 | ] |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 420 | })) as Message; |
| 421 | let i: MessageComponentInteraction; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 422 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 423 | i = await m.awaitMessageComponent({ |
| 424 | time: 300000, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 425 | filter: (i) => { |
| 426 | return ( |
| 427 | i.user.id === interaction.user.id && |
| 428 | i.channel!.id === interaction.channel!.id && |
| 429 | i.message.id === m.id |
| 430 | ); |
| 431 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 432 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 433 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 434 | timedOut = true; |
| 435 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 436 | } |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 437 | if (i.isButton()) { |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 438 | if (i.customId === "modify") { |
| 439 | await i.showModal( |
| 440 | new Discord.ModalBuilder() |
| 441 | .setCustomId("modal") |
| 442 | .setTitle("Editing moderator note") |
| 443 | .addComponents( |
| 444 | new ActionRowBuilder<Discord.TextInputBuilder>().addComponents( |
| 445 | new Discord.TextInputBuilder() |
| 446 | .setCustomId("note") |
| 447 | .setLabel("Note") |
| 448 | .setMaxLength(4000) |
| 449 | .setRequired(false) |
| 450 | .setStyle(TextInputStyle.Paragraph) |
| 451 | .setValue(note?.note ? note.note : " ") |
| 452 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 453 | ) |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 454 | ); |
| 455 | await interaction.editReply({ |
| 456 | embeds: [ |
| 457 | new EmojiEmbed() |
| 458 | .setTitle("Mod notes for " + member.user.username) |
| 459 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 460 | .setStatus("Success") |
| 461 | .setEmoji("GUILD.TICKET.OPEN") |
| 462 | ], |
| 463 | components: [ |
| 464 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
| 465 | new ButtonBuilder() |
| 466 | .setLabel("Back") |
| 467 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 468 | .setStyle(ButtonStyle.Primary) |
| 469 | .setCustomId("back") |
| 470 | ]) |
| 471 | ] |
| 472 | }); |
| 473 | let out; |
| 474 | try { |
| 475 | out = await modalInteractionCollector(m, interaction.user); |
| 476 | } catch (e) { |
| 477 | timedOut = true; |
| 478 | continue; |
| 479 | } |
| 480 | if (out === null || out.isButton()) { |
| 481 | continue; |
| 482 | } else { |
| 483 | let toAdd = out.fields.getTextInputValue("note").trim() || null; |
| 484 | if (toAdd === "") toAdd = null; |
| 485 | await client.database.notes.create(member.guild.id, member.id, toAdd); |
| 486 | } |
| 487 | } else if (i.customId === "history") { |
| 488 | await i.deferUpdate(); |
| 489 | if (!(await showHistory(member, interaction))) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 490 | } |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 491 | } else if (i.isStringSelectMenu()) { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 492 | await i.deferUpdate(); |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 493 | let flag: string | null = i.values[0]!; |
| 494 | if (flag === "none") flag = null; |
| 495 | await client.database.notes.flag(member.guild.id, member.id, flag as FlagColors | null); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 496 | } |
| 497 | } |
Skyler Grey | f25451c | 2023-04-23 21:26:11 +0000 | [diff] [blame] | 498 | }; |
TheCodedProf | c016f9f | 2023-04-23 16:01:38 -0400 | [diff] [blame] | 499 | |
| 500 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
| 501 | const member = interaction.options.getMember("user") as Discord.GuildMember; |
| 502 | await noteMenu(member, interaction); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 503 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 504 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 505 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 506 | const member = interaction.member as GuildMember; |
TheCodedProf | a9e3e03 | 2023-04-22 17:32:19 -0400 | [diff] [blame] | 507 | if (!member.permissions.has("ManageMessages")) return "You do not have the *Manage Messages* permission"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 508 | return true; |
| 509 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 510 | |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 511 | export { command, callback, check }; |
| 512 | export const metadata = { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 513 | longDescription: |
| 514 | "Shows the moderation history (all previous bans, kicks, warns etc.), and moderator notes for a user.", |
| 515 | premiumOnly: true |
| 516 | }; |