PineaFan | 0d06edc | 2023-01-17 22:10:31 +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, |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 13 | APIMessageComponentEmoji, |
| 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 |
| 107 | .sort( |
| 108 | (a: { occurredAt: Date }, b: { occurredAt: Date }) => |
| 109 | b.occurredAt.getTime() - a.occurredAt.getTime() |
| 110 | ) |
| 111 | .reverse(); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 112 | let m: Message; |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 113 | let refresh = false; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 114 | let filteredTypes: string[] = []; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 115 | let openFilterPane = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 116 | let timedOut = false; |
| 117 | let showHistorySelected = false; |
| 118 | while (!timedOut && !showHistorySelected) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 119 | if (refresh) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 120 | history = await client.database.history.read(member.guild.id, member.id, currentYear); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 121 | history = history |
| 122 | .sort( |
| 123 | (a: { occurredAt: Date }, b: { occurredAt: Date }) => |
| 124 | b.occurredAt.getTime() - a.occurredAt.getTime() |
| 125 | ) |
| 126 | .reverse(); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 127 | if (openFilterPane) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 128 | let tempFilteredTypes = filteredTypes; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 129 | if (filteredTypes.length === 0) { |
| 130 | tempFilteredTypes = Object.keys(types); |
| 131 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 132 | history = history.filter((h: { type: string }) => tempFilteredTypes.includes(h.type)); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 133 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 134 | refresh = false; |
| 135 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | const groups: TimelineSection[] = []; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 137 | if (history.length > 0) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 138 | current = new TimelineSection(); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 139 | history.forEach((event: HistorySchema) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 140 | if (current.contentLength() + historyToString(event).length > 2000 || current.content.length === 5) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 141 | groups.push(current); |
| 142 | current.generateName(); |
| 143 | current = new TimelineSection(); |
| 144 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 145 | current.addContent({ |
| 146 | data: event, |
| 147 | rendered: historyToString(event) |
| 148 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 149 | }); |
| 150 | current.generateName(); |
| 151 | groups.push(current); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 152 | if (pageIndex === null) { |
| 153 | pageIndex = groups.length - 1; |
| 154 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 155 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 156 | if (pageIndex === null) pageIndex = 0; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 157 | let components: (ActionRowBuilder<Discord.StringSelectMenuBuilder> | ActionRowBuilder<ButtonBuilder>)[] = [] |
| 158 | if (openFilterPane) components = components.concat([ |
| 159 | new ActionRowBuilder<Discord.StringSelectMenuBuilder>().addComponents( |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 160 | new Discord.StringSelectMenuBuilder() |
| 161 | .setMinValues(1) |
| 162 | .setMaxValues(Object.keys(types).length) |
| 163 | .setCustomId("filter") |
| 164 | .setPlaceholder("Select events to show") |
| 165 | .setOptions(...Object.entries(types).map(([key, value]) => new Discord.StringSelectMenuOptionBuilder() |
| 166 | .setLabel(value.text) |
| 167 | .setValue(key) |
| 168 | .setDefault(filteredTypes.includes(key)) |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 169 | .setEmoji(getEmojiByName(value.emoji, "id") as APIMessageComponentEmoji) |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 170 | ))) |
| 171 | ]); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 172 | components = components.concat([new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
| 173 | new ButtonBuilder() |
| 174 | .setCustomId("prevYear") |
| 175 | .setLabel((currentYear - 1).toString()) |
| 176 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 177 | .setStyle(ButtonStyle.Secondary), |
| 178 | new ButtonBuilder() |
| 179 | .setCustomId("prevPage") |
| 180 | .setLabel("Previous page") |
| 181 | .setStyle(ButtonStyle.Primary), |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 182 | new ButtonBuilder() |
| 183 | .setCustomId("today") |
| 184 | .setLabel("Today") |
| 185 | .setStyle(ButtonStyle.Primary), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 186 | new ButtonBuilder() |
| 187 | .setCustomId("nextPage") |
| 188 | .setLabel("Next page") |
| 189 | .setStyle(ButtonStyle.Primary) |
| 190 | .setDisabled(pageIndex >= groups.length - 1 && currentYear === new Date().getFullYear()), |
| 191 | new ButtonBuilder() |
| 192 | .setCustomId("nextYear") |
| 193 | .setLabel((currentYear + 1).toString()) |
| 194 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 195 | .setStyle(ButtonStyle.Secondary) |
| 196 | .setDisabled(currentYear === new Date().getFullYear()) |
| 197 | ])]) |
| 198 | components = components.concat([new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
| 199 | new ButtonBuilder() |
| 200 | .setLabel("Mod notes") |
| 201 | .setCustomId("modNotes") |
| 202 | .setStyle(ButtonStyle.Primary) |
| 203 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")), |
| 204 | new ButtonBuilder() |
| 205 | .setLabel("Filter") |
| 206 | .setCustomId("openFilter") |
| 207 | .setStyle(openFilterPane ? ButtonStyle.Success : ButtonStyle.Primary) |
| 208 | .setEmoji(getEmojiByName("ICONS.FILTER", "id")) |
| 209 | ])]) |
| 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({ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 230 | text: openFilterPane && filteredTypes.length ? "Filters are currently enabled" : "No filters selected" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 231 | }) |
| 232 | ], |
| 233 | components: components |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 234 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 235 | } else { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 236 | m = await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 237 | embeds: [ |
| 238 | new EmojiEmbed() |
| 239 | .setEmoji("MEMBER.JOIN") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 240 | .setTitle("Moderation history for " + member.user.username) |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 241 | .setDescription(`**${currentYear}**\n\n*No events*`) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 242 | .setStatus("Success") |
| 243 | .setFooter({ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 244 | text: openFilterPane && filteredTypes.length ? "Filters are currently enabled" : "No filters selected" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 245 | }) |
| 246 | ], |
| 247 | components: components |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 248 | }) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 249 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 250 | let i: MessageComponentInteraction; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 251 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 252 | i = await m.awaitMessageComponent({ |
| 253 | time: 300000, |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 254 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 255 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 256 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 257 | interaction.editReply({ |
| 258 | embeds: [ |
| 259 | new EmojiEmbed() |
| 260 | .setEmoji("MEMBER.JOIN") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 261 | .setTitle("Moderation history for " + member.user.username) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 262 | .setDescription(m.embeds[0]!.description!) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 263 | .setStatus("Danger") |
| 264 | .setFooter({ text: "Message timed out" }) |
| 265 | ] |
| 266 | }); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 267 | timedOut = true; |
| 268 | continue; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 269 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 270 | await i.deferUpdate(); |
| 271 | if (i.customId === "filter" && i.isStringSelectMenu()) { |
| 272 | filteredTypes = i.values; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 273 | pageIndex = null; |
| 274 | refresh = true; |
| 275 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 276 | if (i.customId === "prevYear") { |
| 277 | currentYear--; |
| 278 | pageIndex = null; |
| 279 | refresh = true; |
| 280 | } |
| 281 | if (i.customId === "nextYear") { |
| 282 | currentYear++; |
| 283 | pageIndex = null; |
| 284 | refresh = true; |
| 285 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 286 | if (i.customId === "prevPage") { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 287 | pageIndex!--; |
| 288 | if (pageIndex! < 0) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 289 | pageIndex = null; |
| 290 | currentYear--; |
| 291 | refresh = true; |
| 292 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 293 | } |
| 294 | if (i.customId === "nextPage") { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 295 | pageIndex!++; |
| 296 | if (pageIndex! >= groups.length) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 297 | pageIndex = 0; |
| 298 | currentYear++; |
| 299 | refresh = true; |
| 300 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 301 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 302 | if (i.customId === "today") { |
| 303 | currentYear = new Date().getFullYear(); |
| 304 | pageIndex = null; |
| 305 | refresh = true; |
| 306 | } |
| 307 | if (i.customId === "modNotes") { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 308 | showHistorySelected = true; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 309 | } |
| 310 | if (i.customId === "openFilter") { |
| 311 | openFilterPane = !openFilterPane; |
| 312 | refresh = true; |
| 313 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 314 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 315 | return timedOut ? 0 : 1; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 316 | } |
| 317 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 318 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
| 319 | let m: Message; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 320 | const member = interaction.options.getMember("user") as Discord.GuildMember; |
| 321 | await interaction.reply({ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 322 | embeds: LoadingEmbed, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 323 | ephemeral: true, |
| 324 | fetchReply: true |
| 325 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 326 | let note; |
| 327 | let firstLoad = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 328 | let timedOut = false; |
| 329 | while (!timedOut) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 330 | note = await client.database.notes.read(member.guild.id, member.id); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 331 | if (firstLoad && !note) { await showHistory(member, interaction); } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 332 | firstLoad = false; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 333 | m = (await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 334 | embeds: [ |
| 335 | new EmojiEmbed() |
| 336 | .setEmoji("MEMBER.JOIN") |
| 337 | .setTitle("Mod notes for " + member.user.username) |
| 338 | .setDescription(note ? note : "*No note set*") |
| 339 | .setStatus("Success") |
| 340 | ], |
| 341 | components: [ |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 342 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 343 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 344 | .setLabel(`${note ? "Modify" : "Create"} note`) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 345 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 346 | .setCustomId("modify") |
| 347 | .setEmoji(getEmojiByName("ICONS.EDIT", "id")), |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 348 | new ButtonBuilder() |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 349 | .setLabel("Moderation history") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 350 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 351 | .setCustomId("history") |
| 352 | .setEmoji(getEmojiByName("ICONS.HISTORY", "id")) |
| 353 | ]) |
| 354 | ] |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 355 | })) as Message; |
| 356 | let i: MessageComponentInteraction; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 357 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 358 | i = await m.awaitMessageComponent({ |
| 359 | time: 300000, |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 360 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 361 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 362 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 363 | timedOut = true; |
| 364 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 365 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 366 | if (i.customId === "modify") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 367 | await i.showModal( |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 368 | new Discord.ModalBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 369 | .setCustomId("modal") |
| 370 | .setTitle("Editing moderator note") |
| 371 | .addComponents( |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 372 | new ActionRowBuilder<Discord.TextInputBuilder>().addComponents( |
| 373 | new Discord.TextInputBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 374 | .setCustomId("note") |
| 375 | .setLabel("Note") |
| 376 | .setMaxLength(4000) |
| 377 | .setRequired(false) |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 378 | .setStyle(TextInputStyle.Paragraph) |
| 379 | .setValue(note ? note : " ") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 380 | ) |
| 381 | ) |
| 382 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 383 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 384 | embeds: [ |
| 385 | new EmojiEmbed() |
| 386 | .setTitle("Mod notes for " + member.user.username) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 387 | .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] | 388 | .setStatus("Success") |
| 389 | .setEmoji("GUILD.TICKET.OPEN") |
| 390 | ], |
| 391 | components: [ |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 392 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 393 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 394 | .setLabel("Back") |
| 395 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 396 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 397 | .setCustomId("back") |
| 398 | ]) |
| 399 | ] |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 400 | }); |
| 401 | let out; |
| 402 | try { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 403 | out = await modalInteractionCollector(m, interaction.user); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 404 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 405 | timedOut = true; |
| 406 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 407 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 408 | if (out === null || out.isButton()) { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 409 | continue; |
| 410 | } else if (out instanceof ModalSubmitInteraction) { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 411 | let toAdd = out.fields.getTextInputValue("note") || null; |
| 412 | if (toAdd === " ") toAdd = null; |
| 413 | if (toAdd) toAdd = toAdd.trim(); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 414 | await client.database.notes.create(member.guild.id, member.id, toAdd); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 415 | } else { |
| 416 | continue; |
| 417 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 418 | } else if (i.customId === "history") { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 419 | await i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 420 | if (!(await showHistory(member, interaction))) return; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 421 | } |
| 422 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 423 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 424 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 425 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 426 | const member = interaction.member as GuildMember; |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 427 | if (!member.permissions.has("ModerateMembers")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 428 | return "You do not have the *Moderate Members* permission"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 429 | return true; |
| 430 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 431 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 432 | export { command, callback, check }; |
| 433 | export const metadata = { |
| 434 | longDescription: "Shows the moderation history (all previous bans, kicks, warns etc.), and moderator notes for a user.", |
| 435 | premiumOnly: true, |
| 436 | } |