3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-11 04:45: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

@ -12,6 +12,7 @@ import { ScheduledPostsShowCmd } from "./commands/ScheduledPostsShowCmd";
import { ScheduledPostsListCmd } from "./commands/ScheduledPostsListCmd";
import { ScheduledPostsDeleteCmd } from "./commands/SchedluedPostsDeleteCmd";
import { scheduledPostLoop } from "./util/scheduledPostLoop";
import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
const defaultOptions: PluginOptions<PostPluginType> = {
config: {
@ -33,6 +34,7 @@ export const PostPlugin = zeppelinPlugin<PostPluginType>()("post", {
prettyName: "Post",
},
dependencies: [TimeAndDatePlugin],
configSchema: ConfigSchema,
defaultOptions,

View file

@ -1,9 +1,15 @@
import { postCmd } from "../types";
import { trimLines, sorter, disableCodeBlocks, deactivateMentions, createChunkedMessage } from "src/utils";
import {
trimLines,
sorter,
disableCodeBlocks,
deactivateMentions,
createChunkedMessage,
DBDateFormat,
} from "src/utils";
import humanizeDuration from "humanize-duration";
import moment from "moment-timezone";
import { inGuildTz } from "../../../utils/timezones";
import { DBDateFormat, getDateFormat } from "../../../utils/dateFormats";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
const SCHEDULED_POST_PREVIEW_TEXT_LENGTH = 50;
@ -31,9 +37,10 @@ export const ScheduledPostsListCmd = postCmd({
.replace(/\s+/g, " ")
.slice(0, SCHEDULED_POST_PREVIEW_TEXT_LENGTH);
const prettyPostAt = inGuildTz(pluginData, moment.utc(p.post_at, DBDateFormat)).format(
getDateFormat(pluginData, "pretty_datetime"),
);
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const prettyPostAt = timeAndDate
.inGuildTz(moment.utc(p.post_at, DBDateFormat))
.format(timeAndDate.getDateFormat("pretty_datetime"));
const parts = [`\`#${i++}\` \`[${prettyPostAt}]\` ${previewText}${isTruncated ? "..." : ""}`];
if (p.attachments.length) parts.push("*(with attachment)*");
if (p.content.embed) parts.push("*(embed)*");

View file

@ -1,5 +1,5 @@
import { Message, Channel, TextChannel } from "eris";
import { StrictMessageContent, errorMessage, stripObjectToScalars, MINUTES } from "src/utils";
import { StrictMessageContent, errorMessage, stripObjectToScalars, MINUTES, DBDateFormat } from "src/utils";
import moment from "moment-timezone";
import { LogType } from "src/data/LogType";
import humanizeDuration from "humanize-duration";
@ -8,7 +8,7 @@ import { PluginData } from "knub";
import { PostPluginType } from "../types";
import { parseScheduleTime } from "./parseScheduleTime";
import { postMessage } from "./postMessage";
import { DBDateFormat, getDateFormat } from "../../../utils/dateFormats";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
const MIN_REPEAT_TIME = 5 * MINUTES;
const MAX_REPEAT_TIME = Math.pow(2, 32);
@ -54,7 +54,7 @@ export async function actualPostCmd(
let postAt;
if (opts.schedule) {
// Schedule the post to be posted later
postAt = parseScheduleTime(pluginData, opts.schedule);
postAt = await parseScheduleTime(pluginData, msg.author.id, opts.schedule);
if (!postAt) {
return sendErrorMessage(pluginData, msg.channel, "Invalid schedule time");
}
@ -68,7 +68,7 @@ export async function actualPostCmd(
let repeatDetailsStr: string = null;
if (opts["repeat-until"]) {
repeatUntil = parseScheduleTime(pluginData, opts["repeat-until"]);
repeatUntil = await parseScheduleTime(pluginData, msg.author.id, opts["repeat-until"]);
// Invalid time
if (!repeatUntil) {
@ -109,6 +109,8 @@ export async function actualPostCmd(
: `every ${humanizeDuration(opts.repeat)}, ${repeatTimes} times in total`;
}
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
// Save schedule/repeat information in DB
if (postAt) {
if (postAt < moment.utc()) {
@ -140,9 +142,9 @@ export async function actualPostCmd(
pluginData.state.logs.log(LogType.SCHEDULED_REPEATED_MESSAGE, {
author: stripObjectToScalars(msg.author),
channel: stripObjectToScalars(targetChannel),
datetime: postAt.format(getDateFormat(pluginData, "pretty_datetime")),
date: postAt.format(getDateFormat(pluginData, "date")),
time: postAt.format(getDateFormat(pluginData, "time")),
datetime: postAt.format(timeAndDate.getDateFormat("pretty_datetime")),
date: postAt.format(timeAndDate.getDateFormat("date")),
time: postAt.format(timeAndDate.getDateFormat("time")),
repeatInterval: humanizeDuration(opts.repeat),
repeatDetails: repeatDetailsStr,
});
@ -150,9 +152,9 @@ export async function actualPostCmd(
pluginData.state.logs.log(LogType.SCHEDULED_MESSAGE, {
author: stripObjectToScalars(msg.author),
channel: stripObjectToScalars(targetChannel),
datetime: postAt.format(getDateFormat(pluginData, "pretty_datetime")),
date: postAt.format(getDateFormat(pluginData, "date")),
time: postAt.format(getDateFormat(pluginData, "time")),
datetime: postAt.format(timeAndDate.getDateFormat("pretty_datetime")),
date: postAt.format(timeAndDate.getDateFormat("date")),
time: postAt.format(timeAndDate.getDateFormat("time")),
});
}
}
@ -166,9 +168,9 @@ export async function actualPostCmd(
pluginData.state.logs.log(LogType.REPEATED_MESSAGE, {
author: stripObjectToScalars(msg.author),
channel: stripObjectToScalars(targetChannel),
datetime: postAt.format(getDateFormat(pluginData, "pretty_datetime")),
date: postAt.format(getDateFormat(pluginData, "date")),
time: postAt.format(getDateFormat(pluginData, "time")),
datetime: postAt.format(timeAndDate.getDateFormat("pretty_datetime")),
date: postAt.format(timeAndDate.getDateFormat("date")),
time: postAt.format(timeAndDate.getDateFormat("time")),
repeatInterval: humanizeDuration(opts.repeat),
repeatDetails: repeatDetailsStr,
});
@ -177,7 +179,7 @@ export async function actualPostCmd(
// Bot reply schenanigans
let successMessage = opts.schedule
? `Message scheduled to be posted in <#${targetChannel.id}> on ${postAt.format(
getDateFormat(pluginData, "pretty_datetime"),
timeAndDate.getDateFormat("pretty_datetime"),
)}`
: `Message posted in <#${targetChannel.id}>`;
@ -185,7 +187,7 @@ export async function actualPostCmd(
successMessage += `. Message will be automatically reposted every ${humanizeDuration(opts.repeat)}`;
if (repeatUntil) {
successMessage += ` until ${repeatUntil.format(getDateFormat(pluginData, "pretty_datetime"))}`;
successMessage += ` until ${repeatUntil.format(timeAndDate.getDateFormat("pretty_datetime"))}`;
} else if (repeatTimes) {
successMessage += `, ${repeatTimes} times in total`;
}

View file

@ -1,11 +1,11 @@
import moment, { Moment } from "moment-timezone";
import { convertDelayStringToMS } from "src/utils";
import { PluginData } from "knub";
import { getGuildTz } from "../../../utils/timezones";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
// TODO: Extract out of the Post plugin, use everywhere with a date input
export function parseScheduleTime(pluginData: PluginData<any>, str: string): Moment {
const tz = getGuildTz(pluginData);
export async function parseScheduleTime(pluginData: PluginData<any>, memberId: string, str: string): Promise<Moment> {
const tz = await pluginData.getPlugin(TimeAndDatePlugin).getMemberTz(memberId);
const dt1 = moment.tz(str, "YYYY-MM-DD HH:mm:ss", tz);
if (dt1 && dt1.isValid()) return dt1;

View file

@ -1,12 +1,11 @@
import { PluginData } from "knub";
import { PostPluginType } from "../types";
import { logger } from "src/logger";
import { stripObjectToScalars, SECONDS } from "src/utils";
import { stripObjectToScalars, SECONDS, DBDateFormat } from "src/utils";
import { LogType } from "src/data/LogType";
import moment from "moment-timezone";
import { TextChannel, User } from "eris";
import { postMessage } from "./postMessage";
import { DBDateFormat } from "../../../utils/dateFormats";
const SCHEDULED_POST_CHECK_INTERVAL = 5 * SECONDS;