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