mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Add time_and_date plugin. Use it for timezones and date formats around the bot.
This commit is contained in:
parent
cffb0dbd6b
commit
4ae8cf85a3
67 changed files with 543 additions and 177 deletions
|
@ -1,17 +0,0 @@
|
|||
import { PluginData } from "knub";
|
||||
import { DateFormats } from "../types";
|
||||
|
||||
const defaultDateFormats: DateFormats = {
|
||||
date: "MMM D, YYYY",
|
||||
time: "H:mm",
|
||||
pretty_datetime: "MMM D, YYYY [at] H:mm z",
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the guild-specific date format, falling back to the defaults if one has not been specified
|
||||
*/
|
||||
export function getDateFormat(pluginData: PluginData<any>, formatName: keyof DateFormats) {
|
||||
return pluginData.guildConfig.date_formats?.[formatName] || defaultDateFormats[formatName];
|
||||
}
|
||||
|
||||
export const DBDateFormat = "YYYY-MM-DD HH:mm:ss";
|
7
backend/src/utils/isValidTimezone.ts
Normal file
7
backend/src/utils/isValidTimezone.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import moment from "moment-timezone";
|
||||
|
||||
const validTimezones = moment.tz.names();
|
||||
|
||||
export function isValidTimezone(input: string) {
|
||||
return validTimezones.includes(input);
|
||||
}
|
13
backend/src/utils/tValidTimezone.ts
Normal file
13
backend/src/utils/tValidTimezone.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
import * as t from "io-ts";
|
||||
import { either } from "fp-ts/lib/Either";
|
||||
import { isValidTimezone } from "./isValidTimezone";
|
||||
|
||||
export const tValidTimezone = new t.Type<string, string>(
|
||||
"tValidTimezone",
|
||||
(s): s is string => typeof s === "string",
|
||||
(from, to) =>
|
||||
either.chain(t.string.validate(from, to), input => {
|
||||
return isValidTimezone(input) ? t.success(input) : t.failure(from, to, `Invalid timezone: ${input}`);
|
||||
}),
|
||||
s => s,
|
||||
);
|
|
@ -1,21 +0,0 @@
|
|||
import moment from "moment-timezone";
|
||||
import { PluginData } from "knub";
|
||||
import { ZeppelinGuildConfig } from "../types";
|
||||
|
||||
export function getGuildTz(pluginData: PluginData<any>) {
|
||||
const guildConfig = pluginData.guildConfig as ZeppelinGuildConfig;
|
||||
return guildConfig.timezone || "Etc/UTC";
|
||||
}
|
||||
|
||||
export function inGuildTz(pluginData: PluginData<any>, input?: moment.Moment | number) {
|
||||
let momentObj: moment.Moment;
|
||||
if (typeof input === "number") {
|
||||
momentObj = moment.utc(input, "x");
|
||||
} else if (moment.isMoment(input)) {
|
||||
momentObj = input.clone();
|
||||
} else {
|
||||
momentObj = moment.utc();
|
||||
}
|
||||
|
||||
return momentObj.tz(getGuildTz(pluginData));
|
||||
}
|
2
backend/src/utils/typeUtils.ts
Normal file
2
backend/src/utils/typeUtils.ts
Normal file
|
@ -0,0 +1,2 @@
|
|||
// From https://stackoverflow.com/a/56370310/316944
|
||||
export type Tail<T extends any[]> = ((...t: T) => void) extends (h: any, ...r: infer R) => void ? R : never;
|
Loading…
Add table
Add a link
Reference in a new issue