Skip to content

Commit

Permalink
Discord alerts for expiring solo certs
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Nov 16, 2024
1 parent fd08b13 commit ef19550
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 2 deletions.
9 changes: 9 additions & 0 deletions vzdv-bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ async fn main() {
});
};

{
let config = config.clone();
let db = db.clone();
let http = http.clone();
tokio::spawn(async move {
tasks::solo_certs::process(config, db, http).await;
});
};

info!("Connected to Gateway");
loop {
let event = match shard.next_event().await {
Expand Down
1 change: 1 addition & 0 deletions vzdv-bot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod off_roster;
pub mod online;
pub mod roles;
pub mod solo_certs;
69 changes: 69 additions & 0 deletions vzdv-bot/src/tasks/solo_certs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use anyhow::Result;
use chrono::Utc;
use log::{debug, error, info};
use sqlx::{Pool, Sqlite};
use std::{sync::Arc, time::Duration};
use tokio::time::sleep;
use twilight_http::Client;
use twilight_model::id::Id;
use twilight_util::builder::embed::{EmbedBuilder, EmbedFieldBuilder};
use vzdv::{
config::Config,
get_controller_cids_and_names,
sql::{self, SoloCert},
};

/// Single loop execution.
async fn tick(config: &Arc<Config>, db: &Pool<Sqlite>, http: &Arc<Client>) -> Result<()> {
debug!("Checking for expiring solo certs");
let solo_certs: Vec<SoloCert> = sqlx::query_as(sql::GET_ALL_SOLO_CERTS)
.fetch_all(db)
.await?;
if solo_certs.is_empty() {
return Ok(());
}

let cids_and_names = get_controller_cids_and_names(db).await?;
let now = Utc::now();
for cert in solo_certs {
let delta = cert.expiration_date - now;
if delta.num_hours() <= 12 && delta.num_hours() > 0 {
info!("Expiration alert on solo cert #{}", cert.id);
let name = cids_and_names
.get(&cert.cid)
.map(|n| format!("{} {}", n.0, n.1))
.unwrap_or_else(|| String::from("? ?"));
http.create_message(Id::new(config.discord.solo_cert_expiration_channel))
.embeds(&[EmbedBuilder::new()
.title("Expiring solo cert")
.field(EmbedFieldBuilder::new(
"Controller",
format!("{} ({})", name, cert.cid),
))
.field(EmbedFieldBuilder::new("Position", cert.position))
.field(EmbedFieldBuilder::new(
"Expires",
cert.expiration_date.to_rfc2822(),
))
.validate()?
.build()])?
.await?;
debug!("Expiring solo cert message posted to Discord");
}
}

Ok(())
}

// Processing loop.
pub async fn process(config: Arc<Config>, db: Pool<Sqlite>, http: Arc<Client>) {
sleep(Duration::from_secs(30)).await;
debug!("Starting solo cert processing");

loop {
if let Err(e) = tick(&config, &db, &http).await {
error!("Error in solo cert processing tick: {e}");
}
sleep(Duration::from_secs(60 * 60 * 12)).await; // 12 hours
}
}
1 change: 1 addition & 0 deletions vzdv-site/templates/changelog.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<div class="card-text">
<ul>
<li>New staff page for all solo certs</li>
<li>Discord alerts for training staff of expiring solo certs</li>
</ul>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions vzdv.empty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ online_channel = 0
# online_message = 0
off_roster_channel = 0
owner_id = 0
solo_cert_expiration_channel = 0

[discord.auth]
client_id = ""
Expand Down
1 change: 1 addition & 0 deletions vzdv.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ online_channel = 0
# online_message = 0
off_roster_channel = 0
owner_id = 0
solo_cert_expiration_channel = 0

[discord.auth]
client_id = ""
Expand Down
1 change: 1 addition & 0 deletions vzdv/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub struct ConfigDiscord {
pub webhooks: ConfigDiscordWebhooks,
pub roles: ConfigDiscordRoles,
pub owner_id: u64,
pub solo_cert_expiration_channel: u64,
}

#[derive(Debug, Clone, Deserialize, Default)]
Expand Down
4 changes: 2 additions & 2 deletions vzdv/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ pub struct SoloCert {
pub issued_by: u32,
pub position: String,
pub reported: bool,
pub created_date: String,
pub expiration_date: String,
pub created_date: DateTime<Utc>,
pub expiration_date: DateTime<Utc>,
}

/// Statements to create tables. Only ran when the DB file does not exist,
Expand Down

0 comments on commit ef19550

Please sign in to comment.