3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-17 15:15:02 +00:00

Add case icons. Clean up !cases. Allow customizing case colors and icons.

This commit is contained in:
Dragory 2020-08-11 04:16:06 +03:00
parent ad24d166ce
commit 131a79ffd4
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
24 changed files with 138 additions and 22 deletions

View file

@ -0,0 +1,8 @@
import { PluginData } from "knub";
import { CasesPluginType } from "../types";
import { CaseTypes, CaseTypeToName } from "../../../data/CaseTypes";
import { caseColors } from "../caseColors";
export function getCaseColor(pluginData: PluginData<CasesPluginType>, caseType: CaseTypes) {
return pluginData.config.get().case_colors?.[CaseTypeToName[caseType]] ?? caseColors[caseType];
}

View file

@ -4,11 +4,11 @@ import moment from "moment-timezone";
import { CaseTypes } from "../../../data/CaseTypes";
import { PluginData, helpers } from "knub";
import { CasesPluginType } from "../types";
import { CaseTypeColors } from "../../../data/CaseTypeColors";
import { resolveCaseId } from "./resolveCaseId";
import { chunkLines, chunkMessageLines, emptyEmbedValue, messageLink } from "../../../utils";
import { inGuildTz } from "../../../utils/timezones";
import { getDateFormat } from "../../../utils/dateFormats";
import { getCaseColor } from "./getCaseColor";
export async function getCaseEmbed(
pluginData: PluginData<CasesPluginType>,
@ -53,9 +53,7 @@ export async function getCaseEmbed(
embed.title += " (hidden)";
}
if (CaseTypeColors[theCase.type]) {
embed.color = CaseTypeColors[theCase.type];
}
embed.color = getCaseColor(pluginData, theCase.type);
if (theCase.notes.length) {
theCase.notes.forEach((note: any) => {

View file

@ -0,0 +1,8 @@
import { PluginData } from "knub";
import { CasesPluginType } from "../types";
import { CaseTypes, CaseTypeToName } from "../../../data/CaseTypes";
import { caseIcons } from "../caseIcons";
export function getCaseIcon(pluginData: PluginData<CasesPluginType>, caseType: CaseTypes) {
return pluginData.config.get().case_icons?.[CaseTypeToName[caseType]] ?? caseIcons[caseType];
}

View file

@ -1,17 +1,19 @@
import { PluginData } from "knub";
import { CasesPluginType } from "../types";
import { convertDelayStringToMS, DAYS, disableLinkPreviews, messageLink } from "../../../utils";
import { convertDelayStringToMS, DAYS, disableLinkPreviews, emptyEmbedValue, messageLink } from "../../../utils";
import { DBDateFormat, getDateFormat } from "../../../utils/dateFormats";
import { CaseTypes } from "../../../data/CaseTypes";
import { CaseTypes, CaseTypeToName } from "../../../data/CaseTypes";
import moment from "moment-timezone";
import { Case } from "../../../data/entities/Case";
import { inGuildTz } from "../../../utils/timezones";
import humanizeDuration from "humanize-duration";
import { humanizeDurationShort } from "../../../humanizeDurationShort";
import { caseAbbreviations } from "../caseAbbreviations";
import { getCaseIcon } from "./getCaseIcon";
const CASE_SUMMARY_REASON_MAX_LENGTH = 300;
const INCLUDE_MORE_NOTES_THRESHOLD = 20;
const UPDATED_STR = "__[Update]__";
const UPDATE_STR = "**[Update]**";
const RELATIVE_TIME_THRESHOLD = 7 * DAYS;
@ -20,8 +22,9 @@ export async function getCaseSummary(
caseOrCaseId: Case | number,
withLinks = false,
) {
const caseId = caseOrCaseId instanceof Case ? caseOrCaseId.id : caseOrCaseId;
const config = pluginData.config.get();
const caseId = caseOrCaseId instanceof Case ? caseOrCaseId.id : caseOrCaseId;
const theCase = await pluginData.state.cases.with("notes").find(caseId);
const firstNote = theCase.notes[0];
@ -29,8 +32,8 @@ export async function getCaseSummary(
let leftoverNotes = Math.max(0, theCase.notes.length - 1);
for (let i = 1; i < theCase.notes.length; i++) {
if (reason.length >= CASE_SUMMARY_REASON_MAX_LENGTH - UPDATED_STR.length - INCLUDE_MORE_NOTES_THRESHOLD) break;
reason += ` ${UPDATED_STR} ${theCase.notes[i].body}`;
if (reason.length >= CASE_SUMMARY_REASON_MAX_LENGTH - UPDATE_STR.length - INCLUDE_MORE_NOTES_THRESHOLD) break;
reason += ` ${UPDATE_STR} ${theCase.notes[i].body}`;
leftoverNotes--;
}
@ -45,23 +48,26 @@ export async function getCaseSummary(
reason = disableLinkPreviews(reason);
const timestamp = moment.utc(theCase.created_at, DBDateFormat);
const config = pluginData.config.get();
const relativeTimeCutoff = convertDelayStringToMS(config.relative_time_cutoff);
const useRelativeTime = config.show_relative_times && Date.now() - timestamp.valueOf() < relativeTimeCutoff;
const prettyTimestamp = useRelativeTime
? moment.utc().to(timestamp)
: inGuildTz(pluginData, timestamp).format(getDateFormat(pluginData, "date"));
let caseTitle = `\`Case #${theCase.case_number}\``;
const icon = getCaseIcon(pluginData, theCase.type);
let caseTitle = `\`#${theCase.case_number}\``;
if (withLinks && theCase.log_message_id) {
const [channelId, messageId] = theCase.log_message_id.split("-");
caseTitle = `[${caseTitle}](${messageLink(pluginData.guild.id, channelId, messageId)})`;
} else {
caseTitle = `\`${caseTitle}\``;
}
const caseType = `__${CaseTypes[theCase.type]}__`;
let line = `\`[${prettyTimestamp}]\` ${caseTitle} ${caseType} ${reason}`;
let caseType = (caseAbbreviations[theCase.type] || String(theCase.type)).toUpperCase();
caseType = (caseType + " ").slice(0, 4);
let line = `${icon} **\`${caseType}\`** \`[${prettyTimestamp}]\` ${caseTitle} ${reason}`;
if (leftoverNotes > 1) {
line += ` *(+${leftoverNotes} ${leftoverNotes === 1 ? "note" : "notes"})*`;
}
@ -70,5 +76,5 @@ export async function getCaseSummary(
line += " *(hidden)*";
}
return line;
return line.trim();
}