From 2c230ec00dcbf2c7d7b52e35e0f857454c30483f Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Sat, 11 Apr 2020 17:59:04 +0200 Subject: [PATCH 1/4] Added today, timeXAgo, humanizeTime and discordDateFormat --- backend/src/plugins/Tags.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/backend/src/plugins/Tags.ts b/backend/src/plugins/Tags.ts index 4a2511f1..c763fc6b 100644 --- a/backend/src/plugins/Tags.ts +++ b/backend/src/plugins/Tags.ts @@ -1,6 +1,6 @@ import { decorators as d, IPluginOptions, logger } from "knub"; import { Member, Message, TextChannel } from "eris"; -import { errorMessage, successMessage, stripObjectToScalars, tNullable } from "../utils"; +import { errorMessage, successMessage, stripObjectToScalars, tNullable, convertDelayStringToMS } from "../utils"; import { GuildTags } from "../data/GuildTags"; import { GuildSavedMessages } from "../data/GuildSavedMessages"; import { SavedMessage } from "../data/entities/SavedMessage"; @@ -101,6 +101,30 @@ export class TagsPlugin extends ZeppelinPlugin { return diff >= 0 ? result : `${result} ago`; }, + today() { + return moment(); + }, + + timeXAgo(timeDiff) { + if (typeof timeDiff !== "string") + return 'Please pass a valid delay as a string to timeXAgo (e.g. timeXAgo("1w"))'; + + const delay = convertDelayStringToMS(timeDiff); + return moment(moment().valueOf() - delay); + }, + + humanizeTime(timems) { + if (typeof timems !== "number") return moment().format("DD-MM-YYYY HH:mm"); + + return moment(timems).format("DD-MM-YYYY HH:mm"); + }, + + discordDateFormat(timems) { + if (typeof timems !== "number") return moment().format("YYYY-MM-DD"); + + return moment(timems).format("YYYY-MM-DD"); + }, + mention: input => { if (typeof input !== "string") return ""; if (input.match(/^<(@#)(!&)\d+>$/)) { From 40f5a7f043339f29d398d0800d1f7ec4ce1f6909 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Sat, 11 Apr 2020 18:03:48 +0200 Subject: [PATCH 2/4] If statements must be braced --- backend/src/plugins/Tags.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/backend/src/plugins/Tags.ts b/backend/src/plugins/Tags.ts index c763fc6b..429a9ea5 100644 --- a/backend/src/plugins/Tags.ts +++ b/backend/src/plugins/Tags.ts @@ -106,27 +106,34 @@ export class TagsPlugin extends ZeppelinPlugin { }, timeXAgo(timeDiff) { - if (typeof timeDiff !== "string") + if (typeof timeDiff !== "string") { return 'Please pass a valid delay as a string to timeXAgo (e.g. timeXAgo("1w"))'; + } const delay = convertDelayStringToMS(timeDiff); return moment(moment().valueOf() - delay); }, humanizeTime(timems) { - if (typeof timems !== "number") return moment().format("DD-MM-YYYY HH:mm"); + if (typeof timems !== "number") { + return moment().format("DD-MM-YYYY HH:mm"); + } return moment(timems).format("DD-MM-YYYY HH:mm"); }, discordDateFormat(timems) { - if (typeof timems !== "number") return moment().format("YYYY-MM-DD"); + if (typeof timems !== "number") { + return moment().format("YYYY-MM-DD"); + } return moment(timems).format("YYYY-MM-DD"); }, mention: input => { - if (typeof input !== "string") return ""; + if (typeof input !== "string") { + return ""; + } if (input.match(/^<(@#)(!&)\d+>$/)) { return input; } From 0f77f82a139db93c43da325365eef7eca341ca37 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Sat, 25 Apr 2020 17:34:22 +0200 Subject: [PATCH 3/4] Fix return value of timeXAgo being incompatible with format functions --- backend/src/plugins/Tags.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/plugins/Tags.ts b/backend/src/plugins/Tags.ts index 429a9ea5..c8e5589f 100644 --- a/backend/src/plugins/Tags.ts +++ b/backend/src/plugins/Tags.ts @@ -111,7 +111,7 @@ export class TagsPlugin extends ZeppelinPlugin { } const delay = convertDelayStringToMS(timeDiff); - return moment(moment().valueOf() - delay); + return moment(moment().valueOf() - delay).valueOf(); }, humanizeTime(timems) { From d526632d5702cca1668a3b1a91492dad4bc133dc Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Fri, 22 May 2020 21:14:26 +0300 Subject: [PATCH 4/4] Tweaks to time/date tag functions --- backend/src/plugins/Tags.ts | 78 ++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/backend/src/plugins/Tags.ts b/backend/src/plugins/Tags.ts index c8e5589f..76668b50 100644 --- a/backend/src/plugins/Tags.ts +++ b/backend/src/plugins/Tags.ts @@ -89,11 +89,22 @@ export class TagsPlugin extends ZeppelinPlugin { this.savedMessages.events.on("delete", this.onMessageDeleteFn); this.tagFunctions = { + parseDateTime(str) { + if (typeof str === "number") { + return str; // Unix timestamp + } + + if (typeof str !== "string") { + return Date.now(); + } + + return moment(str, "YYYY-MM-DD HH:mm:ss").valueOf(); + }, + countdown(toDate) { - if (typeof toDate !== "string") return ""; + const target = this.parseDateTime(toDate); const now = moment(); - const target = moment(toDate, "YYYY-MM-DD HH:mm:ss"); if (!target.isValid()) return ""; const diff = target.diff(now); @@ -101,39 +112,70 @@ export class TagsPlugin extends ZeppelinPlugin { return diff >= 0 ? result : `${result} ago`; }, - today() { - return moment(); + now() { + return Date.now(); }, - timeXAgo(timeDiff) { - if (typeof timeDiff !== "string") { - return 'Please pass a valid delay as a string to timeXAgo (e.g. timeXAgo("1w"))'; + timeAdd(...args) { + let reference; + let delay; + + if (args.length >= 2) { + // (time, delay) + reference = this.parseDateTime(args[0]); + delay = args[1]; + } else { + // (delay), implicit "now" as time + reference = Date.now(); + delay = args[0]; } - const delay = convertDelayStringToMS(timeDiff); - return moment(moment().valueOf() - delay).valueOf(); + const delayMS = convertDelayStringToMS(delay); + return moment(reference) + .add(delayMS) + .valueOf(); }, - humanizeTime(timems) { - if (typeof timems !== "number") { - return moment().format("DD-MM-YYYY HH:mm"); + timeSub(...args) { + let reference; + let delay; + + if (args.length >= 2) { + // (time, delay) + reference = this.parseDateTime(args[0]); + delay = args[1]; + } else { + // (delay), implicit "now" as time + reference = Date.now(); + delay = args[0]; } - return moment(timems).format("DD-MM-YYYY HH:mm"); + const delayMS = convertDelayStringToMS(delay); + return moment(reference) + .subtract(delayMS) + .valueOf(); }, - discordDateFormat(timems) { - if (typeof timems !== "number") { - return moment().format("YYYY-MM-DD"); - } + timeAgo(delay) { + return this.timeSub(delay); + }, - return moment(timems).format("YYYY-MM-DD"); + timeFormat(time, format) { + const parsed = this.parseDateTime(time); + return moment(parsed).format(format); + }, + + discordDateFormat(time) { + const parsed = time ? this.parseDateTime(time) : Date.now(); + + return moment(parsed).format("YYYY-MM-DD"); }, mention: input => { if (typeof input !== "string") { return ""; } + if (input.match(/^<(@#)(!&)\d+>$/)) { return input; }