mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Migrate Reminders to new Plugin structure
This commit is contained in:
parent
b6257b9189
commit
7ce1ebfee2
7 changed files with 247 additions and 0 deletions
48
backend/src/plugins/Reminders/utils/postDueRemindersLoop.ts
Normal file
48
backend/src/plugins/Reminders/utils/postDueRemindersLoop.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import { TextChannel } from "eris";
|
||||
import { PluginData } from "knub";
|
||||
import { RemindersPluginType } from "../types";
|
||||
import moment from "moment-timezone";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { disableLinkPreviews } from "knub/dist/helpers";
|
||||
import { SECONDS } from "src/utils";
|
||||
|
||||
const REMINDER_LOOP_TIME = 10 * SECONDS;
|
||||
const MAX_TRIES = 3;
|
||||
|
||||
export async function postDueRemindersLoop(pluginData: PluginData<RemindersPluginType>) {
|
||||
const pendingReminders = await pluginData.state.reminders.getDueReminders();
|
||||
for (const reminder of pendingReminders) {
|
||||
const channel = pluginData.guild.channels.get(reminder.channel_id);
|
||||
if (channel && channel instanceof TextChannel) {
|
||||
try {
|
||||
// Only show created at date if one exists
|
||||
if (moment(reminder.created_at).isValid()) {
|
||||
const target = moment();
|
||||
const diff = target.diff(moment(reminder.created_at, "YYYY-MM-DD HH:mm:ss"));
|
||||
const result = humanizeDuration(diff, { largest: 2, round: true });
|
||||
await channel.createMessage(
|
||||
disableLinkPreviews(
|
||||
`Reminder for <@!${reminder.user_id}>: ${reminder.body} \n\`Set at ${reminder.created_at} (${result} ago)\``,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
await channel.createMessage(disableLinkPreviews(`Reminder for <@!${reminder.user_id}>: ${reminder.body}`));
|
||||
}
|
||||
} catch (e) {
|
||||
// Probably random Discord internal server error or missing permissions or somesuch
|
||||
// Try again next round unless we've already tried to post this a bunch of times
|
||||
const tries = pluginData.state.tries.get(reminder.id) || 0;
|
||||
if (tries < MAX_TRIES) {
|
||||
pluginData.state.tries.set(reminder.id, tries + 1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await pluginData.state.reminders.delete(reminder.id);
|
||||
}
|
||||
|
||||
if (!pluginData.state.unloaded) {
|
||||
pluginData.state.postRemindersTimeout = setTimeout(() => postDueRemindersLoop(pluginData), REMINDER_LOOP_TIME);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue