reminder-bot/postman/src/lib.rs

34 lines
864 B
Rust
Raw Normal View History

2022-02-06 15:47:59 +00:00
mod sender;
2022-02-19 22:11:21 +00:00
use std::env;
2022-02-06 15:47:59 +00:00
use log::info;
use serenity::client::Context;
use sqlx::{Executor, MySql};
2022-02-19 22:11:21 +00:00
use tokio::time::{sleep_until, Duration, Instant};
2022-02-06 15:47:59 +00:00
type Database = MySql;
pub async fn initialize(ctx: Context, pool: impl Executor<'_, Database = Database> + Copy) {
2022-02-19 22:11:21 +00:00
let remind_interval = env::var("REMIND_INTERVAL")
2022-02-06 15:47:59 +00:00
.map(|inner| inner.parse::<u64>().ok())
.ok()
.flatten()
.unwrap_or(10);
loop {
2022-02-19 22:11:21 +00:00
let sleep_to = Instant::now() + Duration::from_secs(remind_interval);
2022-02-06 15:47:59 +00:00
let reminders = sender::Reminder::fetch_reminders(pool).await;
if reminders.len() > 0 {
info!("Preparing to send {} reminders.", reminders.len());
for reminder in reminders {
reminder.send(pool, ctx.clone()).await;
}
}
sleep_until(sleep_to).await;
}
}