mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
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 chunk from "lodash.chunk";
|
||||||
import { GuildCases } from "../data/GuildCases";
|
import { GuildCases } from "../data/GuildCases";
|
||||||
import {
|
import {
|
||||||
|
chunkLines,
|
||||||
|
chunkMessageLines,
|
||||||
convertDelayStringToMS,
|
convertDelayStringToMS,
|
||||||
DBDateFormat,
|
DBDateFormat,
|
||||||
disableLinkPreviews,
|
disableLinkPreviews,
|
||||||
|
@ -862,6 +864,11 @@ export class ModActionsPlugin extends Plugin {
|
||||||
msg.channel.createMessage("No cases found for the specified user!");
|
msg.channel.createMessage("No cases found for the specified user!");
|
||||||
} else {
|
} else {
|
||||||
if (args.expanded && args.expanded.startsWith("expand")) {
|
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)
|
// Expanded view (= individual case embeds)
|
||||||
for (const theCase of cases) {
|
for (const theCase of cases) {
|
||||||
await this.displayCase(theCase.id, msg.channel.id);
|
await this.displayCase(theCase.id, msg.channel.id);
|
||||||
|
@ -889,13 +896,16 @@ export class ModActionsPlugin extends Plugin {
|
||||||
|
|
||||||
const finalMessage = trimLines(`
|
const finalMessage = trimLines(`
|
||||||
Cases for **${userName}**:
|
Cases for **${userName}**:
|
||||||
|
|
||||||
${lines.join("\n")}
|
${lines.join("\n")}
|
||||||
|
|
||||||
Use \`${prefix}case <num>\` to see more info about individual cases
|
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);
|
const cases = await this.cases.getByUserId(args.userId);
|
||||||
if (cases.length > 0) {
|
if (cases.length > 0) {
|
||||||
cases.sort((a, b) => {
|
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})`;
|
return `${CaseTypes[c.type]} (#${c.case_number})`;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const summaryText = cases.length > 3 ? "Last 3 cases" : "Summary";
|
||||||
|
|
||||||
embed.fields.push({
|
embed.fields.push({
|
||||||
name: "Cases",
|
name: "Cases",
|
||||||
value: trimLines(`
|
value: trimLines(`
|
||||||
Total cases: ${cases.length}
|
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");
|
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";
|
export const DBDateFormat = "YYYY-MM-DD HH:mm:ss";
|
||||||
|
|
Loading…
Add table
Reference in a new issue