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