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