3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-15 14:15:03 +00:00

Migrate Reminders to new Plugin structure

This commit is contained in:
Dark 2020-07-16 22:54:02 +02:00
parent b6257b9189
commit 7ce1ebfee2
7 changed files with 247 additions and 0 deletions

View file

@ -0,0 +1,65 @@
import { commandTypeHelpers as ct } from "../../../commandTypes";
import moment from "moment-timezone";
import { convertDelayStringToMS } from "src/utils";
import humanizeDuration from "humanize-duration";
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
import { remindersCommand } from "../types";
export const RemindCmd = remindersCommand({
trigger: ["remind", "remindme"],
usage: "!remind 3h Remind me of this in 3 hours please",
permission: "can_use",
signature: {
time: ct.string(),
reminder: ct.string({ required: false, catchAll: true }),
},
async run({ message: msg, args, pluginData }) {
const now = moment();
let reminderTime;
if (args.time.match(/^\d{4}-\d{1,2}-\d{1,2}$/)) {
// Date in YYYY-MM-DD format, remind at current time on that date
reminderTime = moment(args.time, "YYYY-M-D").set({
hour: now.hour(),
minute: now.minute(),
second: now.second(),
});
} else if (args.time.match(/^\d{4}-\d{1,2}-\d{1,2}T\d{2}:\d{2}$/)) {
// Date and time in YYYY-MM-DD[T]HH:mm format
reminderTime = moment(args.time, "YYYY-M-D[T]HH:mm").second(0);
} else {
// "Delay string" i.e. e.g. "2h30m"
const ms = convertDelayStringToMS(args.time);
if (ms === null) {
sendErrorMessage(pluginData, msg.channel, "Invalid reminder time");
return;
}
reminderTime = moment().add(ms, "millisecond");
}
if (!reminderTime.isValid() || reminderTime.isBefore(now)) {
sendErrorMessage(pluginData, msg.channel, "Invalid reminder time");
return;
}
const reminderBody = args.reminder || `https://discord.com/channels/${this.guildId}/${msg.channel.id}/${msg.id}`;
await pluginData.state.reminders.add(
msg.author.id,
msg.channel.id,
reminderTime.format("YYYY-MM-DD HH:mm:ss"),
reminderBody,
moment().format("YYYY-MM-DD HH:mm:ss"),
);
const msUntilReminder = reminderTime.diff(now);
const timeUntilReminder = humanizeDuration(msUntilReminder, { largest: 2, round: true });
sendSuccessMessage(
pluginData,
msg.channel,
`I will remind you in **${timeUntilReminder}** at **${reminderTime.format("YYYY-MM-DD, HH:mm")}**`,
);
},
});

View file

@ -0,0 +1,31 @@
import { remindersCommand } from "../types";
import { sendErrorMessage } from "src/pluginUtils";
import { sorter, createChunkedMessage } from "src/utils";
import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
export const RemindersCmd = remindersCommand({
trigger: "reminders",
permission: "can_use",
async run({ message: msg, args, pluginData }) {
const reminders = await pluginData.state.reminders.getRemindersByUserId(msg.author.id);
if (reminders.length === 0) {
sendErrorMessage(pluginData, msg.channel, "No reminders");
return;
}
reminders.sort(sorter("remind_at"));
const longestNum = (reminders.length + 1).toString().length;
const lines = Array.from(reminders.entries()).map(([i, reminder]) => {
const num = i + 1;
const paddedNum = num.toString().padStart(longestNum, " ");
const target = moment(reminder.remind_at, "YYYY-MM-DD HH:mm:ss");
const diff = target.diff(moment());
const result = humanizeDuration(diff, { largest: 2, round: true });
return `\`${paddedNum}.\` \`${reminder.remind_at} (${result})\` ${reminder.body}`;
});
createChunkedMessage(msg.channel, lines.join("\n"));
},
});

View file

@ -0,0 +1,29 @@
import { remindersCommand } from "../types";
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
import { sorter } from "src/utils";
import { commandTypeHelpers as ct } from "../../../commandTypes";
export const RemindersDeleteCmd = remindersCommand({
trigger: ["reminders delete", "reminders d"],
permission: "can_use",
signature: {
num: ct.number(),
},
async run({ message: msg, args, pluginData }) {
const reminders = await pluginData.state.reminders.getRemindersByUserId(msg.author.id);
reminders.sort(sorter("remind_at"));
const lastNum = reminders.length + 1;
if (args.num > lastNum || args.num < 0) {
sendErrorMessage(pluginData, msg.channel, "Unknown reminder");
return;
}
const toDelete = reminders[args.num - 1];
await pluginData.state.reminders.delete(toDelete.id);
sendSuccessMessage(pluginData, msg.channel, "Reminder deleted");
},
});