ModActions: fix errors when listing too many cases
This commit is contained in:
parent
2bce771c59
commit
40a20f94c1
3 changed files with 59 additions and 7 deletions
|
@ -5,6 +5,8 @@ import humanizeDuration from "humanize-duration";
|
|||
import chunk from "lodash.chunk";
|
||||
import { GuildCases } from "../data/GuildCases";
|
||||
import {
|
||||
chunkLines,
|
||||
chunkMessageLines,
|
||||
convertDelayStringToMS,
|
||||
DBDateFormat,
|
||||
disableLinkPreviews,
|
||||
|
@ -862,6 +864,11 @@ export class ModActionsPlugin extends Plugin {
|
|||
msg.channel.createMessage("No cases found for the specified user!");
|
||||
} else {
|
||||
if (args.expanded && args.expanded.startsWith("expand")) {
|
||||
if (cases.length > 8) {
|
||||
msg.channel.createMessage("Too many cases for expanded view. Please use compact view instead.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Expanded view (= individual case embeds)
|
||||
for (const theCase of cases) {
|
||||
await this.displayCase(theCase.id, msg.channel.id);
|
||||
|
@ -895,7 +902,10 @@ export class ModActionsPlugin extends Plugin {
|
|||
Use \`${prefix}case <num>\` to see more info about individual cases
|
||||
`);
|
||||
|
||||
msg.channel.createMessage(finalMessage);
|
||||
const finalMessageChunks = chunkMessageLines(finalMessage);
|
||||
for (const msgChunk of finalMessageChunks) {
|
||||
msg.channel.createMessage(msgChunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -259,18 +259,20 @@ export class UtilityPlugin extends Plugin {
|
|||
const cases = await this.cases.getByUserId(args.userId);
|
||||
if (cases.length > 0) {
|
||||
cases.sort((a, b) => {
|
||||
return a.created_at < b.created_at ? -1 : 1;
|
||||
return a.created_at < b.created_at ? 1 : -1;
|
||||
});
|
||||
|
||||
const caseSummaries = cases.map(c => {
|
||||
const caseSummary = cases.slice(0, 3).map(c => {
|
||||
return `${CaseTypes[c.type]} (#${c.case_number})`;
|
||||
});
|
||||
|
||||
const summaryText = cases.length > 3 ? "Last 3 cases" : "Summary";
|
||||
|
||||
embed.fields.push({
|
||||
name: "Cases",
|
||||
value: trimLines(`
|
||||
Total cases: ${cases.length}
|
||||
Summary: ${caseSummaries.join(", ")}
|
||||
${summaryText}: ${caseSummary.join(", ")}
|
||||
`)
|
||||
});
|
||||
}
|
||||
|
|
40
src/utils.ts
40
src/utils.ts
|
@ -229,4 +229,44 @@ export function disableCodeBlocks(content: string): string {
|
|||
return content.replace(/`/g, "`\u200b");
|
||||
}
|
||||
|
||||
export function chunkLines(str: string, maxChunkLength = 2000): string[] {
|
||||
if (str.length < maxChunkLength) {
|
||||
return [str];
|
||||
}
|
||||
|
||||
const chunks = [];
|
||||
|
||||
while (str.length) {
|
||||
if (str.length <= maxChunkLength) {
|
||||
chunks.push(str);
|
||||
break;
|
||||
}
|
||||
|
||||
const slice = str.slice(0, maxChunkLength);
|
||||
|
||||
const lastLineBreakIndex = slice.lastIndexOf("\n");
|
||||
if (lastLineBreakIndex === -1) {
|
||||
chunks.push(str.slice(0, maxChunkLength));
|
||||
str = str.slice(maxChunkLength);
|
||||
} else {
|
||||
chunks.push(str.slice(0, lastLineBreakIndex));
|
||||
str = str.slice(lastLineBreakIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chunks a long message to multiple smaller messages, retaining leading and trailing line breaks
|
||||
*/
|
||||
export function chunkMessageLines(str: string): string[] {
|
||||
const chunks = chunkLines(str, 1999);
|
||||
return chunks.map(chunk => {
|
||||
if (chunk[0] === "\n") chunk = "\u200b" + chunk;
|
||||
if (chunk[chunk.length - 1] === "\n") chunk = chunk + "\u200b";
|
||||
return chunk;
|
||||
});
|
||||
}
|
||||
|
||||
export const DBDateFormat = "YYYY-MM-DD HH:mm:ss";
|
||||
|
|
Loading…
Add table
Reference in a new issue