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