Skip to content

Commit

Permalink
Handle Discord no-show notif. delivery failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Nov 20, 2024
1 parent b999c83 commit c38c861
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 64 deletions.
142 changes: 78 additions & 64 deletions vzdv-bot/src/tasks/no_shows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,82 @@ async fn send_dm(http: &Arc<Client>, user_id: &str, message: &str) -> Result<()>
Ok(())
}

async fn handle_single(
entry: &NoShow,
entries: &[NoShow],
cid_name_map: &HashMap<u32, (String, String)>,
config: &Arc<Config>,
db: &Pool<Sqlite>,
http: &Arc<Client>,
) -> Result<()> {
debug!("Need to notify {} of no-show", entry.cid);
let controller: Controller = sqlx::query_as(sql::GET_CONTROLLER_BY_CID)
.bind(entry.cid)
.fetch_one(db)
.await?;
if let Some(ref discord_user_id) = controller.discord_id {
let occurrence_count = entries
.iter()
.filter(|e| e.cid == entry.cid && e.entry_type == entry.entry_type)
.count();
// notify the controller
send_dm(
http,
discord_user_id,
&create_message(entry, &cid_name_map, occurrence_count, config),
)
.await?;
sqlx::query(sql::UPDATE_NO_SHOW_NOTIFIED)
.bind(entry.id)
.execute(db)
.await?;
// if this isn't the first instance, notify the appropriate staff member
if occurrence_count > 1 {
let staff_role = if entry.entry_type == "training" {
"TA"
} else {
"EC"
};
let staff_member = get_staff_member_by_role(db, staff_role).await?;
if let Some(staff_member) = staff_member.first() {
if let Some(ref discord_user_id) = staff_member.discord_id {
send_dm(
http,
discord_user_id,
&format!(
"Controller {} has been added to the {} no-show list {} times.\n\nSee the no-show list [here](https://{}/admin/no_show_list).",
entry.cid, entry.entry_type, occurrence_count, config.staff.email_domain
),
)
.await?;
info!(
"Notified {} {} ({}) of {} no-show occurrences for {}",
staff_member.first_name,
staff_member.last_name,
staff_member.cid,
occurrence_count,
entry.cid
);
} else {
warn!(
"Staff member {} does not have their Discord linked",
staff_member.cid
);
}
} else {
warn!("Could not find staff member with role {staff_role}");
}
}
info!("Notified {} of their new no-show entry", entry.cid);
} else {
debug!(
"Controller {} does not have their Discord linked; cannot notify",
entry.cid
);
}
Ok(())
}

/// Single loop execution.
async fn tick(config: &Arc<Config>, db: &Pool<Sqlite>, http: &Arc<Client>) -> Result<()> {
debug!("Checking for new no-show entries");
Expand All @@ -52,70 +128,8 @@ async fn tick(config: &Arc<Config>, db: &Pool<Sqlite>, http: &Arc<Client>) -> Re
let cid_name_map = get_controller_cids_and_names(db).await?;
for entry in &entries {
if !entry.notified {
debug!("Need to notify {} of no-show", entry.cid);
let controller: Controller = sqlx::query_as(sql::GET_CONTROLLER_BY_CID)
.bind(entry.cid)
.fetch_one(db)
.await?;
if let Some(ref discord_user_id) = controller.discord_id {
let occurrence_count = entries
.iter()
.filter(|e| e.cid == entry.cid && e.entry_type == entry.entry_type)
.count();
// notify the controller
send_dm(
http,
discord_user_id,
&create_message(entry, &cid_name_map, occurrence_count, config),
)
.await?;
sqlx::query(sql::UPDATE_NO_SHOW_NOTIFIED)
.bind(entry.id)
.execute(db)
.await?;
// if this isn't the first instance, notify the appropriate staff member
if occurrence_count > 1 {
let staff_role = if entry.entry_type == "training" {
"TA"
} else {
"EC"
};
let staff_member = get_staff_member_by_role(db, staff_role).await?;
if let Some(staff_member) = staff_member.first() {
if let Some(ref discord_user_id) = staff_member.discord_id {
send_dm(
http,
discord_user_id,
&format!(
"Controller {} has been added to the {} no-show list {} times.\n\nSee the no-show list [here](https://{}/admin/no_show_list).",
entry.cid, entry.entry_type, occurrence_count, config.staff.email_domain
),
)
.await?;
info!(
"Notified {} {} ({}) of {} no-show occurrences for {}",
staff_member.first_name,
staff_member.last_name,
staff_member.cid,
occurrence_count,
entry.cid
);
} else {
warn!(
"Staff member {} does not have their Discord linked",
staff_member.cid
);
}
} else {
warn!("Could not find staff member with role {staff_role}");
}
}
info!("Notified {} of their new no-show entry", entry.cid);
} else {
debug!(
"Controller {} does not have their Discord linked; cannot notify",
entry.cid
);
if let Err(e) = handle_single(entry, &entries, &cid_name_map, config, db, http).await {
error!("Error processing no-show tick for entry {}: {e}", entry.id);
}
}
}
Expand Down
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>Styling improvements for modals on mobile</li>
<li>Improvements for Discord no-show notifications</li>
</ul>
</div>
</div>
Expand Down

0 comments on commit c38c861

Please sign in to comment.