From ae651c8a705fa82379d3b687e7fe46c2de474d56 Mon Sep 17 00:00:00 2001 From: Tiago R Date: Sun, 26 Nov 2023 10:55:47 +0000 Subject: [PATCH 1/3] paginated user cases Signed-off-by: GitHub --- .../ModActions/commands/CasesUserCmd.ts | 83 +++++++++++-------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/backend/src/plugins/ModActions/commands/CasesUserCmd.ts b/backend/src/plugins/ModActions/commands/CasesUserCmd.ts index 069ad31f..6e5aaf88 100644 --- a/backend/src/plugins/ModActions/commands/CasesUserCmd.ts +++ b/backend/src/plugins/ModActions/commands/CasesUserCmd.ts @@ -5,6 +5,7 @@ import { sendErrorMessage } from "../../../pluginUtils"; import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin"; import { UnknownUser, chunkArray, emptyEmbedValue, renderUserUsername, resolveUser, trimLines } from "../../../utils"; import { asyncMap } from "../../../utils/async"; +import { createPaginatedMessage } from "../../../utils/createPaginatedMessage.js"; import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields"; import { getGuildPrefix } from "../../../utils/getGuildPrefix"; import { modActionsCmd } from "../types"; @@ -21,6 +22,8 @@ const opts = { unbans: ct.switchOption({ def: false, shortcut: "ub" }), }; +const casesPerPage = 10; + export const CasesUserCmd = modActionsCmd({ trigger: ["cases", "modlogs"], permission: "can_view", @@ -90,49 +93,57 @@ export const CasesUserCmd = modActionsCmd({ } else { // Compact view (= regular message with a preview of each case) const casesPlugin = pluginData.getPlugin(CasesPlugin); - const lines = await asyncMap(casesToDisplay, (c) => casesPlugin.getCaseSummary(c, true, msg.author.id)); + const totalPages = Math.max(Math.ceil(cases.length / casesPerPage), 1); const prefix = getGuildPrefix(pluginData); - const linesPerChunk = 10; - const lineChunks = chunkArray(lines, linesPerChunk); - const footerField = { - name: emptyEmbedValue, - value: trimLines(` - Use \`${prefix}case \` to see more information about an individual case - `), - }; + createPaginatedMessage( + pluginData.client, + msg.channel, + totalPages, + async (page) => { + const chunkedCases = chunkArray(cases, casesPerPage)[page - 1]; + const lines = await asyncMap(chunkedCases, (c) => casesPlugin.getCaseSummary(c, true, msg.author.id)); - for (const [i, linesInChunk] of lineChunks.entries()) { - const isLastChunk = i === lineChunks.length - 1; + const isLastPage = page === totalPages; + const firstCaseNum = (page - 1) * casesPerPage + 1; + const lastCaseNum = page * casesPerPage; + const title = + totalPages === 1 + ? `Cases for ${userName} (${lines.length} total)` + : `Most recent cases ${firstCaseNum}-${lastCaseNum} of ${cases.length} for ${userName}`; - if (isLastChunk && !args.hidden && hiddenCases.length) { - if (hiddenCases.length === 1) { - linesInChunk.push(`*+${hiddenCases.length} hidden case, use "-hidden" to show it*`); - } else { - linesInChunk.push(`*+${hiddenCases.length} hidden cases, use "-hidden" to show them*`); - } - } + const embed = { + author: { + name: title, + icon_url: user instanceof User ? user.displayAvatarURL() : undefined, + }, + fields: [ + ...getChunkedEmbedFields(emptyEmbedValue, lines.join("\n")), + { + name: emptyEmbedValue, + value: trimLines(` + Use \`${prefix}case \` to see more information about an individual case + `), + }, + ], + } satisfies APIEmbed; - const chunkStart = i * linesPerChunk + 1; - const chunkEnd = Math.min((i + 1) * linesPerChunk, lines.length); + if (isLastPage && !args.hidden && hiddenCases.length) + embed.fields.push({ + name: emptyEmbedValue, + value: + hiddenCases.length === 1 + ? `*+${hiddenCases.length} hidden case, use "-hidden" to show it*` + : `*+${hiddenCases.length} hidden cases, use "-hidden" to show them*`, + }); - const embed = { - author: { - name: - lineChunks.length === 1 - ? `Cases for ${userName} (${lines.length} total)` - : `Cases ${chunkStart}–${chunkEnd} of ${lines.length} for ${userName}`, - icon_url: user instanceof User ? user.displayAvatarURL() : undefined, - }, - fields: [ - ...getChunkedEmbedFields(emptyEmbedValue, linesInChunk.join("\n")), - ...(isLastChunk ? [footerField] : []), - ], - } satisfies APIEmbed; - - msg.channel.send({ embeds: [embed] }); - } + return { embeds: [embed] }; + }, + { + limitToUserId: msg.author.id, + }, + ); } } }, From bb4fdf653095002a916b93e4daf8cceaea511022 Mon Sep 17 00:00:00 2001 From: Tiago R Date: Sun, 26 Nov 2023 11:49:33 +0000 Subject: [PATCH 2/3] move embed fields to embed description Signed-off-by: GitHub --- backend/src/plugins/ModActions/commands/CasesModCmd.ts | 8 ++++---- backend/src/plugins/ModActions/commands/CasesUserCmd.ts | 9 +++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/backend/src/plugins/ModActions/commands/CasesModCmd.ts b/backend/src/plugins/ModActions/commands/CasesModCmd.ts index 5b0e3273..d520eede 100644 --- a/backend/src/plugins/ModActions/commands/CasesModCmd.ts +++ b/backend/src/plugins/ModActions/commands/CasesModCmd.ts @@ -4,7 +4,6 @@ import { sendErrorMessage } from "../../../pluginUtils"; import { emptyEmbedValue, resolveUser, trimLines } from "../../../utils"; import { asyncMap } from "../../../utils/async"; import { createPaginatedMessage } from "../../../utils/createPaginatedMessage"; -import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields"; import { getGuildPrefix } from "../../../utils/getGuildPrefix"; import { CasesPlugin } from "../../Cases/CasesPlugin"; import { modActionsCmd } from "../types"; @@ -13,7 +12,7 @@ const opts = { mod: ct.userId({ option: true }), }; -const casesPerPage = 5; +const casesPerPage = 10; export const CasesModCmd = modActionsCmd({ trigger: ["cases", "modlogs", "infractions"], @@ -50,8 +49,9 @@ export const CasesModCmd = modActionsCmd({ const cases = await casesPlugin.getRecentCasesByMod(modId, casesPerPage, (page - 1) * casesPerPage); const lines = await asyncMap(cases, (c) => casesPlugin.getCaseSummary(c, true, msg.author.id)); + const isLastPage = page === totalPages; const firstCaseNum = (page - 1) * casesPerPage + 1; - const lastCaseNum = page * casesPerPage; + const lastCaseNum = isLastPage ? totalCases : page * casesPerPage; const title = `Most recent cases ${firstCaseNum}-${lastCaseNum} of ${totalCases} by ${modName}`; const embed = { @@ -59,8 +59,8 @@ export const CasesModCmd = modActionsCmd({ name: title, icon_url: mod instanceof User ? mod.displayAvatarURL() : undefined, }, + description: lines.join("\n"), fields: [ - ...getChunkedEmbedFields(emptyEmbedValue, lines.join("\n")), { name: emptyEmbedValue, value: trimLines(` diff --git a/backend/src/plugins/ModActions/commands/CasesUserCmd.ts b/backend/src/plugins/ModActions/commands/CasesUserCmd.ts index 6e5aaf88..15f42cf6 100644 --- a/backend/src/plugins/ModActions/commands/CasesUserCmd.ts +++ b/backend/src/plugins/ModActions/commands/CasesUserCmd.ts @@ -6,7 +6,6 @@ import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin"; import { UnknownUser, chunkArray, emptyEmbedValue, renderUserUsername, resolveUser, trimLines } from "../../../utils"; import { asyncMap } from "../../../utils/async"; import { createPaginatedMessage } from "../../../utils/createPaginatedMessage.js"; -import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields"; import { getGuildPrefix } from "../../../utils/getGuildPrefix"; import { modActionsCmd } from "../types"; @@ -107,7 +106,7 @@ export const CasesUserCmd = modActionsCmd({ const isLastPage = page === totalPages; const firstCaseNum = (page - 1) * casesPerPage + 1; - const lastCaseNum = page * casesPerPage; + const lastCaseNum = isLastPage ? cases.length : page * casesPerPage; const title = totalPages === 1 ? `Cases for ${userName} (${lines.length} total)` @@ -118,13 +117,11 @@ export const CasesUserCmd = modActionsCmd({ name: title, icon_url: user instanceof User ? user.displayAvatarURL() : undefined, }, + description: lines.join("\n"), fields: [ - ...getChunkedEmbedFields(emptyEmbedValue, lines.join("\n")), { name: emptyEmbedValue, - value: trimLines(` - Use \`${prefix}case \` to see more information about an individual case - `), + value: trimLines(`Use \`${prefix}case \` to see more information about an individual case`), }, ], } satisfies APIEmbed; From c53bfaa2332e4fc948ba338b1188625103f5528e Mon Sep 17 00:00:00 2001 From: Tiago R Date: Fri, 29 Dec 2023 17:03:45 +0000 Subject: [PATCH 3/3] remove trimLines Signed-off-by: GitHub --- backend/src/plugins/ModActions/commands/CasesUserCmd.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/ModActions/commands/CasesUserCmd.ts b/backend/src/plugins/ModActions/commands/CasesUserCmd.ts index 15f42cf6..e8ce50ad 100644 --- a/backend/src/plugins/ModActions/commands/CasesUserCmd.ts +++ b/backend/src/plugins/ModActions/commands/CasesUserCmd.ts @@ -3,7 +3,7 @@ import { commandTypeHelpers as ct } from "../../../commandTypes"; import { CaseTypes } from "../../../data/CaseTypes"; import { sendErrorMessage } from "../../../pluginUtils"; import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin"; -import { UnknownUser, chunkArray, emptyEmbedValue, renderUserUsername, resolveUser, trimLines } from "../../../utils"; +import { UnknownUser, chunkArray, emptyEmbedValue, renderUserUsername, resolveUser } from "../../../utils"; import { asyncMap } from "../../../utils/async"; import { createPaginatedMessage } from "../../../utils/createPaginatedMessage.js"; import { getGuildPrefix } from "../../../utils/getGuildPrefix"; @@ -121,7 +121,7 @@ export const CasesUserCmd = modActionsCmd({ fields: [ { name: emptyEmbedValue, - value: trimLines(`Use \`${prefix}case \` to see more information about an individual case`), + value: `Use \`${prefix}case \` to see more information about an individual case`, }, ], } satisfies APIEmbed;