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