diff --git a/swordfish-common/src/database/katana.rs b/swordfish-common/src/database/katana.rs index 5b4b10e..961a5e2 100644 --- a/swordfish-common/src/database/katana.rs +++ b/swordfish-common/src/database/katana.rs @@ -139,9 +139,13 @@ pub async fn write_card(mut card: Card) -> Result<(), String> { pub async fn write_cards(cards: Vec) -> Result<(), String> { let mut new_cards: Vec = Vec::new(); let mut handles: Vec, String>>> = Vec::new(); + let start = SystemTime::now(); + let current_time_ts = start + .duration_since(UNIX_EPOCH).unwrap(); for mut card in cards { + let current_time_ts_clone = current_time_ts.clone(); trace!("Writing card: {:?}", card); - handles.push(task::spawn(async { + handles.push(task::spawn(async move { let old_card = KATANA .get() .unwrap() @@ -154,11 +158,7 @@ pub async fn write_cards(cards: Vec) -> Result<(), String> { ) .await .unwrap(); - let start = SystemTime::now(); - let current_time_ts = start - .duration_since(UNIX_EPOCH) - .expect("Time went backwards"); - card.last_update_ts = current_time_ts.as_secs() as i64; + card.last_update_ts = current_time_ts_clone.as_secs() as i64; if old_card.is_some() { match KATANA .get() diff --git a/swordfish/src/main.rs b/swordfish/src/main.rs index 1c121ba..7f4fadb 100644 --- a/swordfish/src/main.rs +++ b/swordfish/src/main.rs @@ -9,6 +9,8 @@ use serenity::prelude::*; use std::env; use std::path::Path; use std::sync::OnceLock; +use std::thread::current; +use std::time::{SystemTime, UNIX_EPOCH}; use swordfish_common::*; use crate::config::Config; @@ -274,7 +276,20 @@ async fn main() { #[command] async fn ping(ctx: &Context, msg: &Message) -> CommandResult { - msg.reply(ctx, "Pong!").await?; + let start = SystemTime::now(); + let current_time_ts = start.duration_since(UNIX_EPOCH).unwrap().as_micros() as f64; + let msg_ts = msg.timestamp.timestamp_micros() as f64; + helper::info_message( + ctx, + msg, + format!( + "Time taken to receive message: `{}ms`\n\n\ + This only reflects the time taken for the bot to receive the message from Discord server.", + (current_time_ts - msg_ts) / 1000.0 // Message timestamp can't be negative + ), + Some("Ping".to_string()), + ) + .await; Ok(()) }