mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-11 04:45:02 +00:00
Add support for server-specific timezone and date format settings
This commit is contained in:
parent
ddbbc543c2
commit
c67a1df11d
51 changed files with 326 additions and 168 deletions
|
@ -1,6 +1,9 @@
|
|||
import { postCmd } from "../types";
|
||||
import { trimLines, sorter, disableCodeBlocks, deactivateMentions, createChunkedMessage } from "src/utils";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import moment from "moment-timezone";
|
||||
import { inGuildTz } from "../../../utils/timezones";
|
||||
import { DBDateFormat, getDateFormat } from "../../../utils/dateFormats";
|
||||
|
||||
const SCHEDULED_POST_PREVIEW_TEXT_LENGTH = 50;
|
||||
|
||||
|
@ -28,7 +31,10 @@ export const ScheduledPostsListCmd = postCmd({
|
|||
.replace(/\s+/g, " ")
|
||||
.slice(0, SCHEDULED_POST_PREVIEW_TEXT_LENGTH);
|
||||
|
||||
const parts = [`\`#${i++}\` \`[${p.post_at}]\` ${previewText}${isTruncated ? "..." : ""}`];
|
||||
const prettyPostAt = inGuildTz(pluginData, moment.utc(p.post_at, DBDateFormat)).format(
|
||||
getDateFormat(pluginData, "pretty_datetime"),
|
||||
);
|
||||
const parts = [`\`#${i++}\` \`[${prettyPostAt}]\` ${previewText}${isTruncated ? "..." : ""}`];
|
||||
if (p.attachments.length) parts.push("*(with attachment)*");
|
||||
if (p.content.embed) parts.push("*(embed)*");
|
||||
if (p.repeat_until) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Message, Channel, TextChannel } from "eris";
|
||||
import { StrictMessageContent, errorMessage, DBDateFormat, stripObjectToScalars, MINUTES } from "src/utils";
|
||||
import { StrictMessageContent, errorMessage, stripObjectToScalars, MINUTES } from "src/utils";
|
||||
import moment from "moment-timezone";
|
||||
import { LogType } from "src/data/LogType";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
|
@ -8,10 +8,11 @@ import { PluginData } from "knub";
|
|||
import { PostPluginType } from "../types";
|
||||
import { parseScheduleTime } from "./parseScheduleTime";
|
||||
import { postMessage } from "./postMessage";
|
||||
import { DBDateFormat, getDateFormat } from "../../../utils/dateFormats";
|
||||
|
||||
const MIN_REPEAT_TIME = 5 * MINUTES;
|
||||
const MAX_REPEAT_TIME = Math.pow(2, 32);
|
||||
const MAX_REPEAT_UNTIL = moment().add(100, "years");
|
||||
const MAX_REPEAT_UNTIL = moment.utc().add(100, "years");
|
||||
|
||||
export async function actualPostCmd(
|
||||
pluginData: PluginData<PostPluginType>,
|
||||
|
@ -53,12 +54,12 @@ export async function actualPostCmd(
|
|||
let postAt;
|
||||
if (opts.schedule) {
|
||||
// Schedule the post to be posted later
|
||||
postAt = parseScheduleTime(opts.schedule);
|
||||
postAt = parseScheduleTime(pluginData, opts.schedule);
|
||||
if (!postAt) {
|
||||
return sendErrorMessage(pluginData, msg.channel, "Invalid schedule time");
|
||||
}
|
||||
} else if (opts.repeat) {
|
||||
postAt = moment().add(opts.repeat, "ms");
|
||||
postAt = moment.utc().add(opts.repeat, "ms");
|
||||
}
|
||||
|
||||
// For repeated posts, make sure repeat-until or repeat-times is specified
|
||||
|
@ -67,13 +68,13 @@ export async function actualPostCmd(
|
|||
let repeatDetailsStr: string = null;
|
||||
|
||||
if (opts["repeat-until"]) {
|
||||
repeatUntil = parseScheduleTime(opts["repeat-until"]);
|
||||
repeatUntil = parseScheduleTime(pluginData, opts["repeat-until"]);
|
||||
|
||||
// Invalid time
|
||||
if (!repeatUntil) {
|
||||
return sendErrorMessage(pluginData, msg.channel, "Invalid time specified for -repeat-until");
|
||||
}
|
||||
if (repeatUntil.isBefore(moment())) {
|
||||
if (repeatUntil.isBefore(moment.utc())) {
|
||||
return sendErrorMessage(pluginData, msg.channel, "You can't set -repeat-until in the past");
|
||||
}
|
||||
if (repeatUntil.isAfter(MAX_REPEAT_UNTIL)) {
|
||||
|
@ -110,7 +111,7 @@ export async function actualPostCmd(
|
|||
|
||||
// Save schedule/repeat information in DB
|
||||
if (postAt) {
|
||||
if (postAt < moment()) {
|
||||
if (postAt < moment.utc()) {
|
||||
return sendErrorMessage(pluginData, msg.channel, "Post can't be scheduled to be posted in the past");
|
||||
}
|
||||
|
||||
|
@ -120,10 +121,18 @@ export async function actualPostCmd(
|
|||
channel_id: targetChannel.id,
|
||||
content,
|
||||
attachments: msg.attachments,
|
||||
post_at: postAt.format(DBDateFormat),
|
||||
post_at: postAt
|
||||
.clone()
|
||||
.tz("Etc/UTC")
|
||||
.format(DBDateFormat),
|
||||
enable_mentions: opts["enable-mentions"],
|
||||
repeat_interval: opts.repeat,
|
||||
repeat_until: repeatUntil ? repeatUntil.format(DBDateFormat) : null,
|
||||
repeat_until: repeatUntil
|
||||
? repeatUntil
|
||||
.clone()
|
||||
.tz("Etc/UTC")
|
||||
.format(DBDateFormat)
|
||||
: null,
|
||||
repeat_times: repeatTimes ?? null,
|
||||
});
|
||||
|
||||
|
@ -131,8 +140,9 @@ export async function actualPostCmd(
|
|||
pluginData.state.logs.log(LogType.SCHEDULED_REPEATED_MESSAGE, {
|
||||
author: stripObjectToScalars(msg.author),
|
||||
channel: stripObjectToScalars(targetChannel),
|
||||
date: postAt.format("YYYY-MM-DD"),
|
||||
time: postAt.format("HH:mm:ss"),
|
||||
datetime: postAt.format(getDateFormat(pluginData, "pretty_datetime")),
|
||||
date: postAt.format(getDateFormat(pluginData, "date")),
|
||||
time: postAt.format(getDateFormat(pluginData, "time")),
|
||||
repeatInterval: humanizeDuration(opts.repeat),
|
||||
repeatDetails: repeatDetailsStr,
|
||||
});
|
||||
|
@ -140,8 +150,9 @@ export async function actualPostCmd(
|
|||
pluginData.state.logs.log(LogType.SCHEDULED_MESSAGE, {
|
||||
author: stripObjectToScalars(msg.author),
|
||||
channel: stripObjectToScalars(targetChannel),
|
||||
date: postAt.format("YYYY-MM-DD"),
|
||||
time: postAt.format("HH:mm:ss"),
|
||||
datetime: postAt.format(getDateFormat(pluginData, "pretty_datetime")),
|
||||
date: postAt.format(getDateFormat(pluginData, "date")),
|
||||
time: postAt.format(getDateFormat(pluginData, "time")),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -155,8 +166,9 @@ export async function actualPostCmd(
|
|||
pluginData.state.logs.log(LogType.REPEATED_MESSAGE, {
|
||||
author: stripObjectToScalars(msg.author),
|
||||
channel: stripObjectToScalars(targetChannel),
|
||||
date: postAt.format("YYYY-MM-DD"),
|
||||
time: postAt.format("HH:mm:ss"),
|
||||
datetime: postAt.format(getDateFormat(pluginData, "pretty_datetime")),
|
||||
date: postAt.format(getDateFormat(pluginData, "date")),
|
||||
time: postAt.format(getDateFormat(pluginData, "time")),
|
||||
repeatInterval: humanizeDuration(opts.repeat),
|
||||
repeatDetails: repeatDetailsStr,
|
||||
});
|
||||
|
@ -164,14 +176,16 @@ export async function actualPostCmd(
|
|||
|
||||
// Bot reply schenanigans
|
||||
let successMessage = opts.schedule
|
||||
? `Message scheduled to be posted in <#${targetChannel.id}> on ${postAt.format("YYYY-MM-DD [at] HH:mm:ss")} (UTC)`
|
||||
? `Message scheduled to be posted in <#${targetChannel.id}> on ${postAt.format(
|
||||
getDateFormat(pluginData, "pretty_datetime"),
|
||||
)}`
|
||||
: `Message posted in <#${targetChannel.id}>`;
|
||||
|
||||
if (opts.repeat) {
|
||||
successMessage += `. Message will be automatically reposted every ${humanizeDuration(opts.repeat)}`;
|
||||
|
||||
if (repeatUntil) {
|
||||
successMessage += ` until ${repeatUntil.format("YYYY-MM-DD [at] HH:mm:ss")} (UTC)`;
|
||||
successMessage += ` until ${repeatUntil.format(getDateFormat(pluginData, "pretty_datetime"))}`;
|
||||
} else if (repeatTimes) {
|
||||
successMessage += `, ${repeatTimes} times in total`;
|
||||
}
|
||||
|
|
|
@ -1,31 +1,36 @@
|
|||
import moment, { Moment } from "moment-timezone";
|
||||
import { convertDelayStringToMS } from "src/utils";
|
||||
import { PluginData } from "knub";
|
||||
import { getGuildTz } from "../../../utils/timezones";
|
||||
|
||||
export function parseScheduleTime(str): Moment {
|
||||
const dt1 = moment(str, "YYYY-MM-DD HH:mm:ss");
|
||||
// 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);
|
||||
|
||||
const dt1 = moment.tz(str, "YYYY-MM-DD HH:mm:ss", tz);
|
||||
if (dt1 && dt1.isValid()) return dt1;
|
||||
|
||||
const dt2 = moment(str, "YYYY-MM-DD HH:mm");
|
||||
const dt2 = moment.tz(str, "YYYY-MM-DD HH:mm", tz);
|
||||
if (dt2 && dt2.isValid()) return dt2;
|
||||
|
||||
const date = moment(str, "YYYY-MM-DD");
|
||||
const date = moment.tz(str, "YYYY-MM-DD", tz);
|
||||
if (date && date.isValid()) return date;
|
||||
|
||||
const t1 = moment(str, "HH:mm:ss");
|
||||
const t1 = moment.tz(str, "HH:mm:ss", tz);
|
||||
if (t1 && t1.isValid()) {
|
||||
if (t1.isBefore(moment())) t1.add(1, "day");
|
||||
if (t1.isBefore(moment.utc())) t1.add(1, "day");
|
||||
return t1;
|
||||
}
|
||||
|
||||
const t2 = moment(str, "HH:mm");
|
||||
const t2 = moment.tz(str, "HH:mm", tz);
|
||||
if (t2 && t2.isValid()) {
|
||||
if (t2.isBefore(moment())) t2.add(1, "day");
|
||||
if (t2.isBefore(moment.utc())) t2.add(1, "day");
|
||||
return t2;
|
||||
}
|
||||
|
||||
const delayStringMS = convertDelayStringToMS(str, "m");
|
||||
if (delayStringMS) {
|
||||
return moment().add(delayStringMS, "ms");
|
||||
return moment.tz(tz).add(delayStringMS, "ms");
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import { PluginData } from "knub";
|
||||
import { PostPluginType } from "../types";
|
||||
import { logger } from "src/logger";
|
||||
import { stripObjectToScalars, DBDateFormat, SECONDS } from "src/utils";
|
||||
import { stripObjectToScalars, SECONDS } 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;
|
||||
|
||||
|
@ -49,10 +50,10 @@ export async function scheduledPostLoop(pluginData: PluginData<PostPluginType>)
|
|||
let shouldClear = true;
|
||||
|
||||
if (post.repeat_interval) {
|
||||
const nextPostAt = moment().add(post.repeat_interval, "ms");
|
||||
const nextPostAt = moment.utc().add(post.repeat_interval, "ms");
|
||||
|
||||
if (post.repeat_until) {
|
||||
const repeatUntil = moment(post.repeat_until, DBDateFormat);
|
||||
const repeatUntil = moment.utc(post.repeat_until, DBDateFormat);
|
||||
if (nextPostAt.isSameOrBefore(repeatUntil)) {
|
||||
await pluginData.state.scheduledPosts.update(post.id, {
|
||||
post_at: nextPostAt.format(DBDateFormat),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue