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

@ -0,0 +1,50 @@
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
import { ConfigSchema, TimeAndDatePluginType } from "./types";
import { GuildMemberTimezones } from "../../data/GuildMemberTimezones";
import { PluginOptions } from "knub";
import { SetTimezoneCmd } from "./commands/SetTimezoneCmd";
import { ViewTimezoneCmd } from "./commands/ViewTimezoneCmd";
import { defaultDateFormats } from "./defaultDateFormats";
import { Tail } from "../../utils/typeUtils";
import { inGuildTz } from "./functions/inGuildTz";
import { mapToPublicFn } from "../../pluginUtils";
import { getGuildTz } from "./functions/getGuildTz";
import { getMemberTz } from "./functions/getMemberTz";
import { getDateFormat } from "./functions/getDateFormat";
import { inMemberTz } from "./functions/inMemberTz";
const defaultOptions: PluginOptions<TimeAndDatePluginType> = {
config: {
timezone: "Etc/UTC",
can_set_timezone: false,
date_formats: defaultDateFormats,
},
overrides: [
{
level: ">=50",
config: {
can_set_timezone: true,
},
},
],
};
export const TimeAndDatePlugin = zeppelinPlugin<TimeAndDatePluginType>()("time_and_date", {
configSchema: ConfigSchema,
defaultOptions,
commands: [SetTimezoneCmd, ViewTimezoneCmd],
public: {
getGuildTz: mapToPublicFn(getGuildTz),
inGuildTz: mapToPublicFn(inGuildTz),
getMemberTz: mapToPublicFn(getMemberTz),
inMemberTz: mapToPublicFn(inMemberTz),
getDateFormat: mapToPublicFn(getDateFormat),
},
onLoad(pluginData) {
pluginData.state.memberTimezones = GuildMemberTimezones.getGuildInstance(pluginData.guild.id);
},
});

View file

@ -0,0 +1,17 @@
import { timeAndDateCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendSuccessMessage } from "../../../pluginUtils";
export const SetTimezoneCmd = timeAndDateCmd({
trigger: "timezone",
permission: "can_set_timezone",
signature: {
timezone: ct.timezone(),
},
async run({ pluginData, message, args }) {
await pluginData.state.memberTimezones.set(message.author.id, args.timezone);
sendSuccessMessage(pluginData, message.channel, `Your timezone is now set to **${args.timezone}**`);
},
});

View file

@ -0,0 +1,23 @@
import { timeAndDateCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendSuccessMessage } from "../../../pluginUtils";
import { getMemberTz } from "../functions/getMemberTz";
import { getGuildTz } from "../functions/getGuildTz";
export const ViewTimezoneCmd = timeAndDateCmd({
trigger: "timezone",
permission: "can_set_timezone",
signature: {},
async run({ pluginData, message, args }) {
const memberTimezone = await pluginData.state.memberTimezones.get(message.author.id);
if (memberTimezone) {
message.channel.createMessage(`Your timezone is currently set to **${memberTimezone.timezone}**`);
return;
}
const serverTimezone = getGuildTz(pluginData);
message.channel.createMessage(`Your timezone is currently set to **${serverTimezone}** (server default)`);
},
});

View file

@ -0,0 +1,5 @@
export const defaultDateFormats = {
date: "MMM D, YYYY",
time: "H:mm",
pretty_datetime: "MMM D, YYYY [at] H:mm z",
};

View file

@ -0,0 +1,6 @@
import { PluginData } from "knub";
import { defaultDateFormats } from "../defaultDateFormats";
export function getDateFormat(pluginData: PluginData<any>, formatName: keyof typeof defaultDateFormats) {
return pluginData.config.get().date_formats?.[formatName] || defaultDateFormats[formatName];
}

View file

@ -0,0 +1,7 @@
import { PluginData } from "knub";
import { ZeppelinGuildConfig } from "../../../types";
import { TimeAndDatePluginType } from "../types";
export function getGuildTz(pluginData: PluginData<TimeAndDatePluginType>) {
return pluginData.config.get().timezone;
}

View file

@ -0,0 +1,8 @@
import { PluginData } from "knub";
import { TimeAndDatePluginType } from "../types";
import { getGuildTz } from "./getGuildTz";
export async function getMemberTz(pluginData: PluginData<TimeAndDatePluginType>, memberId: string) {
const memberTz = await pluginData.state.memberTimezones.get(memberId);
return memberTz?.timezone || getGuildTz(pluginData);
}

View file

@ -0,0 +1,17 @@
import { PluginData } from "knub";
import { TimeAndDatePluginType } from "../types";
import moment from "moment-timezone";
import { getGuildTz } from "./getGuildTz";
export function inGuildTz(pluginData: PluginData<TimeAndDatePluginType>, 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,22 @@
import { PluginData } from "knub";
import { TimeAndDatePluginType } from "../types";
import moment from "moment-timezone";
import { getGuildTz } from "./getGuildTz";
import { getMemberTz } from "./getMemberTz";
export async function inMemberTz(
pluginData: PluginData<TimeAndDatePluginType>,
memberId: string,
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(await getMemberTz(pluginData, memberId));
}

View file

@ -0,0 +1,22 @@
import * as t from "io-ts";
import { tNullable, tPartialDictionary } from "../../utils";
import { BasePluginType, command } from "knub";
import { GuildMemberTimezones } from "../../data/GuildMemberTimezones";
import { tValidTimezone } from "../../utils/tValidTimezone";
import { defaultDateFormats } from "./defaultDateFormats";
export const ConfigSchema = t.type({
timezone: tValidTimezone,
date_formats: tNullable(tPartialDictionary(t.keyof(defaultDateFormats), t.string)),
can_set_timezone: t.boolean,
});
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
export interface TimeAndDatePluginType extends BasePluginType {
config: TConfigSchema;
state: {
memberTimezones: GuildMemberTimezones;
};
}
export const timeAndDateCmd = command<TimeAndDatePluginType>();