-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some fixes to !cases #431
Closed
Closed
Some fixes to !cases #431
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ 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 { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields"; | ||
import { createPaginatedMessage } from "../../../utils/createPaginatedMessage.js"; | ||
import { getGuildPrefix } from "../../../utils/getGuildPrefix"; | ||
import { modActionsCmd } from "../types"; | ||
|
||
|
@@ -21,6 +21,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 +92,55 @@ 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 <num>\` to see more information about an individual case | ||
`), | ||
}; | ||
|
||
for (const [i, linesInChunk] of lineChunks.entries()) { | ||
const isLastChunk = i === lineChunks.length - 1; | ||
|
||
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 chunkStart = i * linesPerChunk + 1; | ||
const chunkEnd = Math.min((i + 1) * linesPerChunk, lines.length); | ||
|
||
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] }); | ||
} | ||
|
||
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)); | ||
|
||
const isLastPage = page === totalPages; | ||
const firstCaseNum = (page - 1) * casesPerPage + 1; | ||
const lastCaseNum = isLastPage ? cases.length : page * casesPerPage; | ||
const title = | ||
totalPages === 1 | ||
? `Cases for ${userName} (${lines.length} total)` | ||
: `Most recent cases ${firstCaseNum}-${lastCaseNum} of ${cases.length} for ${userName}`; | ||
|
||
const embed = { | ||
author: { | ||
name: title, | ||
icon_url: user instanceof User ? user.displayAvatarURL() : undefined, | ||
}, | ||
description: lines.join("\n"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
fields: [ | ||
{ | ||
name: emptyEmbedValue, | ||
value: `Use \`${prefix}case <num>\` to see more information about an individual case`, | ||
}, | ||
], | ||
} satisfies APIEmbed; | ||
|
||
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*`, | ||
}); | ||
|
||
return { embeds: [embed] }; | ||
}, | ||
{ | ||
limitToUserId: msg.author.id, | ||
}, | ||
); | ||
} | ||
} | ||
}, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make sure this is below 4096 characters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would require some major refactoring of the createPaginatedMessage since it's stateless and we just get a fixed # of cases per page currently, would have to offset the next page by X without being able to save this state anywhere.
Probably just lower cases per page, or implement ZDEV-62