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