3
0
Fork 0
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:
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

@ -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";

View file

@ -0,0 +1,7 @@
import moment from "moment-timezone";
const validTimezones = moment.tz.names();
export function isValidTimezone(input: string) {
return validTimezones.includes(input);
}

View 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,
);

View file

@ -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));
}

View 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;