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