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

Add time_and_date plugin. Use it for timezones and date formats around the bot.

This commit is contained in:
Dragory 2020-08-19 00:19:12 +03:00
parent cffb0dbd6b
commit 4ae8cf85a3
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
67 changed files with 543 additions and 177 deletions

View file

@ -12,6 +12,8 @@ import { getCaseTypeAmountForUserId } from "./functions/getCaseTypeAmountForUser
import { getCaseEmbed } from "./functions/getCaseEmbed";
import { trimPluginDescription } from "../../utils";
import { getCaseSummary } from "./functions/getCaseSummary";
import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
import { mapToPublicFn } from "../../pluginUtils";
const defaultOptions = {
config: {
@ -33,6 +35,7 @@ export const CasesPlugin = zeppelinPlugin<CasesPluginType>()("cases", {
`),
},
dependencies: [TimeAndDatePlugin],
configSchema: ConfigSchema,
defaultOptions,
@ -61,17 +64,8 @@ export const CasesPlugin = zeppelinPlugin<CasesPluginType>()("cases", {
};
},
getCaseEmbed(pluginData) {
return (caseOrCaseId: Case | number) => {
return getCaseEmbed(pluginData, caseOrCaseId);
};
},
getCaseSummary(pluginData) {
return (caseOrCaseId: Case | number, withLinks = false) => {
return getCaseSummary(pluginData, caseOrCaseId, withLinks);
};
},
getCaseEmbed: mapToPublicFn(getCaseEmbed),
getCaseSummary: mapToPublicFn(getCaseSummary),
},
onLoad(pluginData) {

View file

@ -6,17 +6,19 @@ import { PluginData, helpers } from "knub";
import { CasesPluginType } from "../types";
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";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
export async function getCaseEmbed(
pluginData: PluginData<CasesPluginType>,
caseOrCaseId: Case | number,
requestMemberId?: string,
): Promise<AdvancedMessageContent> {
const theCase = await pluginData.state.cases.with("notes").find(resolveCaseId(caseOrCaseId));
if (!theCase) return null;
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const createdAt = moment.utc(theCase.created_at);
const actionTypeStr = CaseTypes[theCase.type].toUpperCase();
@ -26,10 +28,14 @@ export async function getCaseEmbed(
let modName = theCase.mod_name;
if (theCase.mod_id) modName += `\n<@!${theCase.mod_id}>`;
const createdAtWithTz = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const embed: any = {
title: `${actionTypeStr} - Case #${theCase.case_number}`,
footer: {
text: `Case created on ${inGuildTz(pluginData, createdAt).format(getDateFormat(pluginData, "pretty_datetime"))}`,
text: `Case created on ${createdAtWithTz.format(timeAndDate.getDateFormat("pretty_datetime"))}`,
},
fields: [
{
@ -56,7 +62,7 @@ export async function getCaseEmbed(
embed.color = getCaseColor(pluginData, theCase.type);
if (theCase.notes.length) {
theCase.notes.forEach((note: any) => {
for (const note of theCase.notes) {
const noteDate = moment.utc(note.created_at);
let noteBody = note.body.trim();
if (noteBody === "") {
@ -67,7 +73,10 @@ export async function getCaseEmbed(
for (let i = 0; i < chunks.length; i++) {
if (i === 0) {
const prettyNoteDate = inGuildTz(pluginData, noteDate).format(getDateFormat(pluginData, "pretty_datetime"));
const noteDateWithTz = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, noteDate)
: timeAndDate.inGuildTz(noteDate);
const prettyNoteDate = noteDateWithTz.format(timeAndDate.getDateFormat("pretty_datetime"));
embed.fields.push({
name: `${note.mod_name} at ${prettyNoteDate}:`,
value: chunks[i],
@ -79,7 +88,7 @@ export async function getCaseEmbed(
});
}
}
});
}
} else {
embed.fields.push({
name: "!!! THIS CASE HAS NO NOTES !!!",

View file

@ -1,15 +1,21 @@
import { PluginData } from "knub";
import { CasesPluginType } from "../types";
import { convertDelayStringToMS, DAYS, disableLinkPreviews, emptyEmbedValue, messageLink } from "../../../utils";
import { DBDateFormat, getDateFormat } from "../../../utils/dateFormats";
import {
convertDelayStringToMS,
DAYS,
DBDateFormat,
disableLinkPreviews,
emptyEmbedValue,
messageLink,
} from "../../../utils";
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";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
const CASE_SUMMARY_REASON_MAX_LENGTH = 300;
const INCLUDE_MORE_NOTES_THRESHOLD = 20;
@ -21,8 +27,10 @@ export async function getCaseSummary(
pluginData: PluginData<CasesPluginType>,
caseOrCaseId: Case | number,
withLinks = false,
requestMemberId?: string,
) {
const config = pluginData.config.get();
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const caseId = caseOrCaseId instanceof Case ? caseOrCaseId.id : caseOrCaseId;
const theCase = await pluginData.state.cases.with("notes").find(caseId);
@ -50,9 +58,12 @@ export async function getCaseSummary(
const timestamp = moment.utc(theCase.created_at, DBDateFormat);
const relativeTimeCutoff = convertDelayStringToMS(config.relative_time_cutoff);
const useRelativeTime = config.show_relative_times && Date.now() - timestamp.valueOf() < relativeTimeCutoff;
const timestampWithTz = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, timestamp)
: timeAndDate.inGuildTz(timestamp);
const prettyTimestamp = useRelativeTime
? moment.utc().to(timestamp)
: inGuildTz(pluginData, timestamp).format(getDateFormat(pluginData, "date"));
: timestampWithTz.format(timeAndDate.getDateFormat("date"));
const icon = getCaseIcon(pluginData, theCase.type);