Tweaks to time/date tag functions
This commit is contained in:
parent
d4f954240d
commit
d526632d57
1 changed files with 60 additions and 18 deletions
|
@ -89,11 +89,22 @@ export class TagsPlugin extends ZeppelinPlugin<TConfigSchema> {
|
||||||
this.savedMessages.events.on("delete", this.onMessageDeleteFn);
|
this.savedMessages.events.on("delete", this.onMessageDeleteFn);
|
||||||
|
|
||||||
this.tagFunctions = {
|
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) {
|
countdown(toDate) {
|
||||||
if (typeof toDate !== "string") return "";
|
const target = this.parseDateTime(toDate);
|
||||||
|
|
||||||
const now = moment();
|
const now = moment();
|
||||||
const target = moment(toDate, "YYYY-MM-DD HH:mm:ss");
|
|
||||||
if (!target.isValid()) return "";
|
if (!target.isValid()) return "";
|
||||||
|
|
||||||
const diff = target.diff(now);
|
const diff = target.diff(now);
|
||||||
|
@ -101,39 +112,70 @@ export class TagsPlugin extends ZeppelinPlugin<TConfigSchema> {
|
||||||
return diff >= 0 ? result : `${result} ago`;
|
return diff >= 0 ? result : `${result} ago`;
|
||||||
},
|
},
|
||||||
|
|
||||||
today() {
|
now() {
|
||||||
return moment();
|
return Date.now();
|
||||||
},
|
},
|
||||||
|
|
||||||
timeXAgo(timeDiff) {
|
timeAdd(...args) {
|
||||||
if (typeof timeDiff !== "string") {
|
let reference;
|
||||||
return 'Please pass a valid delay as a string to timeXAgo (e.g. timeXAgo("1w"))';
|
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);
|
const delayMS = convertDelayStringToMS(delay);
|
||||||
return moment(moment().valueOf() - delay).valueOf();
|
return moment(reference)
|
||||||
|
.add(delayMS)
|
||||||
|
.valueOf();
|
||||||
},
|
},
|
||||||
|
|
||||||
humanizeTime(timems) {
|
timeSub(...args) {
|
||||||
if (typeof timems !== "number") {
|
let reference;
|
||||||
return moment().format("DD-MM-YYYY HH:mm");
|
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) {
|
timeAgo(delay) {
|
||||||
if (typeof timems !== "number") {
|
return this.timeSub(delay);
|
||||||
return moment().format("YYYY-MM-DD");
|
},
|
||||||
}
|
|
||||||
|
|
||||||
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 => {
|
mention: input => {
|
||||||
if (typeof input !== "string") {
|
if (typeof input !== "string") {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input.match(/^<(@#)(!&)\d+>$/)) {
|
if (input.match(/^<(@#)(!&)\d+>$/)) {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue