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