From 410625e2b38910046a9afe40dea24eff348ed3a3 Mon Sep 17 00:00:00 2001 From: tretrauit Date: Sun, 7 Jan 2024 13:23:25 +0700 Subject: [PATCH] chore: move debug features to a file --- swordfish/src/debug.rs | 237 ++++++++++++++++++++++++++++++++++++++++ swordfish/src/main.rs | 243 ++--------------------------------------- 2 files changed, 249 insertions(+), 231 deletions(-) create mode 100644 swordfish/src/debug.rs diff --git a/swordfish/src/debug.rs b/swordfish/src/debug.rs new file mode 100644 index 0000000..30fa388 --- /dev/null +++ b/swordfish/src/debug.rs @@ -0,0 +1,237 @@ +use crate::CONFIG; +use crate::helper; +use crate::katana; +use crate::utils; +use serenity::framework::standard::CommandResult; +use serenity::model::{ + channel::Message, + id::{ChannelId, MessageId}, +}; +use serenity::prelude::*; +use tokio::time::Instant; + +pub async fn dbg_get_message(command: &str, ctx: &Context, msg: &Message) -> Result { + let mut args = msg.content.split(" "); + let target_channel_id = match args.nth(2) { + Some(content) => match content.parse::() { + Ok(id) => id, + Err(why) => { + helper::error_message( + ctx, + msg, + format!("Failed to parse channel ID: `{:?}`", why), + None, + ) + .await; + return Err(()); + } + }, + None => { + helper::error_message( + ctx, + msg, + format!("Usage: `{} `", command), + None, + ) + .await; + return Err(()); + } + }; + let target_msg_id = match args.nth(0) { + Some(content) => match content.parse::() { + Ok(id) => id, + Err(why) => { + helper::error_message( + ctx, + msg, + format!("Failed to parse message ID: `{:?}`", why), + None, + ) + .await; + return Err(()); + } + }, + None => { + helper::error_message( + ctx, + msg, + format!("Usage: `{} `", command), + None, + ) + .await; + return Err(()); + } + }; + let target_msg = match ctx + .http() + .get_message( + ChannelId::new(target_channel_id), + MessageId::new(target_msg_id), + ) + .await + { + Ok(msg) => msg, + Err(why) => { + helper::error_message( + ctx, + msg, + format!("Failed to get message: `{:?}`", why), + None, + ) + .await; + return Err(()); + } + }; + Ok(target_msg) +} + +pub async fn dbg_parse_qingque_atopwl(ctx: &Context, msg: &Message) -> CommandResult { + let target_msg = match dbg_get_message("embed", ctx, msg).await { + Ok(msg) => msg, + Err(_) => { + return Ok(()); + } + }; + if target_msg.embeds.len() == 0 { + helper::error_message( + ctx, + msg, + "Message does not contain any embeds".to_string(), + None, + ) + .await; + return Ok(()); + } + let embed = &target_msg.embeds[0]; + let embed_description = match embed.description { + Some(ref description) => description, + None => { + helper::error_message( + ctx, + msg, + "Embed does not contain a description".to_string(), + None, + ) + .await; + return Ok(()); + } + }; + let cards = utils::katana::parse_cards_from_qingque_atopwl(embed_description); + helper::info_message( + ctx, + msg, + format!("Parsed cards: ```\n{:?}\n```", cards), + None, + ) + .await; + Ok(()) +} + +pub async fn dbg_embed(ctx: &Context, msg: &Message) -> CommandResult { + let target_msg = match dbg_get_message("embed", ctx, msg).await { + Ok(msg) => msg, + Err(_) => { + return Ok(()); + } + }; + if target_msg.embeds.len() == 0 { + helper::error_message( + ctx, + msg, + "Message does not contain any embeds".to_string(), + None, + ) + .await; + return Ok(()); + } + let embed = &target_msg.embeds[0]; + let embed_title = match embed.title { + Some(ref title) => title, + None => "None", + }; + let embed_description = match embed.description { + Some(ref description) => description, + None => "None", + }; + helper::info_message( + ctx, + msg, + format!( + "Title: \n\ + ```\ + {}\n\ + ```\n\ + Description: \n\ + ```\n\ + {}\n\ + ```", + embed_title, embed_description + ), + Some("Embed information".to_string()), + ) + .await; + Ok(()) +} + +pub async fn dbg_kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult { + let target_msg = match dbg_get_message("embed", ctx, msg).await { + Ok(msg) => msg, + Err(_) => { + return Ok(()); + } + }; + let start = Instant::now(); + match katana::analyze_drop_message(&target_msg).await { + Ok(cards) => { + let duration = start.elapsed(); + let mut reply_str = String::new(); + for card in cards { + // reply_str.push_str(&format!("{:?}\n", card)); + let wishlist_str: String = match card.wishlist { + Some(wishlist) => { + let mut out_str = wishlist.to_string(); + while out_str.len() < 5 { + out_str.push(' '); + } + out_str + } + None => "None ".to_string(), + }; + let last_update_ts_str = match card.last_update_ts { + 0 => "`Never`".to_string(), + ts => { + format!("", ts.to_string()) + } + }; + reply_str.push_str( + format!( + ":heart: `{}` • `{}` • **{}** • {} • {}\n", + wishlist_str, card.print, card.name, card.series, last_update_ts_str + ) + .as_str(), + ) + } + reply_str.push_str(&format!("Time taken (to analyze): `{:?}`", duration)); + msg.reply(ctx, reply_str).await?; + } + Err(why) => { + helper::error_message( + ctx, + msg, + format!("Failed to analyze drop: `{:?}`", why), + None, + ) + .await; + } + }; + Ok(()) +} + +pub async fn dbg_info(ctx: &Context, msg: &Message) -> CommandResult { + let reply_str = format!( + "Tesseract backend: {}", + CONFIG.get().unwrap().tesseract.backend, + ); + helper::info_message(ctx, msg, reply_str, Some("Debug Information".to_string())).await; + Ok(()) +} diff --git a/swordfish/src/main.rs b/swordfish/src/main.rs index c45857f..9b3ee5c 100644 --- a/swordfish/src/main.rs +++ b/swordfish/src/main.rs @@ -4,20 +4,18 @@ use serenity::all::MessageUpdateEvent; use serenity::async_trait; use serenity::framework::standard::macros::{command, group}; use serenity::framework::standard::{CommandResult, Configuration, StandardFramework}; -use serenity::model::{ - channel::Message, - id::{ChannelId, MessageId}, -}; +use serenity::model::channel::Message; use serenity::prelude::*; use std::env; use std::path::Path; use std::sync::OnceLock; -use std::time::Instant; use swordfish_common::*; +use tokio::time::Instant; use crate::config::Config; mod config; +mod debug; mod helper; mod katana; mod template; @@ -231,6 +229,9 @@ async fn ping(ctx: &Context, msg: &Message) -> CommandResult { #[command] async fn debug(ctx: &Context, msg: &Message) -> CommandResult { let config = CONFIG.get().unwrap(); + if ["debug", "trace"].contains(&config.log.level.as_str()){ + return Ok(()); + } if !config.debug.allowed_users.contains(&msg.author.id.get()) { return Ok(()); } @@ -249,10 +250,11 @@ async fn debug(ctx: &Context, msg: &Message) -> CommandResult { } }; match subcommand { - "kdropanalyze" => dbg_kdropanalyze(ctx, msg).await?, - "kda" => dbg_kdropanalyze(ctx, msg).await?, - "embed" => dbg_embed(ctx, msg).await?, - "parse-qingque-atopwl" => dbg_parse_qingque_atopwl(ctx, msg).await?, + "info" => debug::dbg_info(ctx, msg).await?, + "kdropanalyze" => debug::dbg_kdropanalyze(ctx, msg).await?, + "kda" => debug::dbg_kdropanalyze(ctx, msg).await?, + "embed" => debug::dbg_embed(ctx, msg).await?, + "parse-qingque-atopwl" => debug::dbg_parse_qingque_atopwl(ctx, msg).await?, _ => { helper::error_message( ctx, @@ -273,233 +275,12 @@ async fn info(ctx: &Context, msg: &Message) -> CommandResult { "Swordfish v{} - {}\n\ Log level: `{}`\n\ Build type: `{}`\n\n\ - Like my work? Consider supporting me at my [Ko-fi](https://ko-fi.com/tretrauit) or [Patreon](https://patreon.com/tretrauit)!\n\n\ - *Debug information*\n\ - Tesseract backend: `{}`\n\ - ", + Like my work? Consider supporting me at my [Ko-fi](https://ko-fi.com/tretrauit) or [Patreon](https://patreon.com/tretrauit)!", env!("CARGO_PKG_VERSION"), GITHUB_URL, CONFIG.get().unwrap().log.level.clone().as_str(), env!("BUILD_PROFILE"), - CONFIG.get().unwrap().tesseract.backend.clone().as_str(), ); helper::info_message(ctx, msg, reply_str, Some("Information".to_string())).await; Ok(()) } - -async fn dbg_get_message(command: &str, ctx: &Context, msg: &Message) -> Result { - let mut args = msg.content.split(" "); - let target_channel_id = match args.nth(2) { - Some(content) => match content.parse::() { - Ok(id) => id, - Err(why) => { - helper::error_message( - ctx, - msg, - format!("Failed to parse channel ID: `{:?}`", why), - None, - ) - .await; - return Ok(()); - } - }, - None => { - helper::error_message( - ctx, - msg, - format!("Usage: `{} `", command), - None, - ) - .await; - return Err(()); - } - }; - let target_msg_id = match args.nth(0) { - Some(content) => match content.parse::() { - Ok(id) => id, - Err(why) => { - helper::error_message( - ctx, - msg, - format!("Failed to parse message ID: `{:?}`", why), - None, - ) - .await; - return Err(()); - } - }, - None => { - helper::error_message( - ctx, - msg, - format!("Usage: `{} `", command), - None, - ) - .await; - return Err(()); - } - }; - let target_msg = match ctx - .http() - .get_message( - ChannelId::new(target_channel_id), - MessageId::new(target_msg_id), - ) - .await - { - Ok(msg) => msg, - Err(why) => { - helper::error_message( - ctx, - msg, - format!("Failed to get message: `{:?}`", why), - None, - ) - .await; - return Err(()); - } - }; - target_msg -} - -async fn dbg_parse_qingque_atopwl(ctx: &Context, msg: &Message) -> CommandResult { - let target_msg = match dbg_get_message("embed", ctx, msg).await { - Ok(msg) => msg, - Err(_) => { - return Ok(()); - } - }; - if target_msg.embeds.len() == 0 { - helper::error_message( - ctx, - msg, - "Message does not contain any embeds".to_string(), - None, - ) - .await; - return Ok(()); - } - let embed = &target_msg.embeds[0]; - let embed_description = match embed.description { - Some(ref description) => description, - None => { - helper::error_message( - ctx, - msg, - "Embed does not contain a description".to_string(), - None, - ) - .await; - return Ok(()); - } - }; - let cards = utils::katana::parse_cards_from_qingque_atopwl(embed_description); - helper::info_message( - ctx, - msg, - format!("Parsed cards: ```\n{:?}\n```", cards), - None, - ) - .await; - Ok(()) -} - -async fn dbg_embed(ctx: &Context, msg: &Message) -> CommandResult { - let target_msg = match dbg_get_message("embed", ctx, msg).await { - Ok(msg) => msg, - Err(_) => { - return Ok(()); - } - }; - if target_msg.embeds.len() == 0 { - helper::error_message( - ctx, - msg, - "Message does not contain any embeds".to_string(), - None, - ) - .await; - return Ok(()); - } - let embed = &target_msg.embeds[0]; - let embed_title = match embed.title { - Some(ref title) => title, - None => "None", - }; - let embed_description = match embed.description { - Some(ref description) => description, - None => "None", - }; - helper::info_message( - ctx, - msg, - format!( - "Title: \n\ - ```\ - {}\n\ - ```\n\ - Description: \n\ - ```\n\ - {}\n\ - ```", - embed_title, embed_description - ), - Some("Embed information".to_string()), - ) - .await; - Ok(()) -} - -async fn dbg_kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult { - let target_msg = match dbg_get_message("embed", ctx, msg).await { - Ok(msg) => msg, - Err(_) => { - return Ok(()); - } - }; - let start = Instant::now(); - match katana::analyze_drop_message(&target_msg).await { - Ok(cards) => { - let duration = start.elapsed(); - let mut reply_str = String::new(); - for card in cards { - // reply_str.push_str(&format!("{:?}\n", card)); - let wishlist_str: String = match card.wishlist { - Some(wishlist) => { - let mut out_str = wishlist.to_string(); - while out_str.len() < 5 { - out_str.push(' '); - } - out_str - } - None => "None ".to_string(), - }; - let last_update_ts_str = match card.last_update_ts { - 0 => "`Never`".to_string(), - ts => { - format!("", ts.to_string()) - } - }; - reply_str.push_str( - format!( - ":heart: `{}` • `{}` • **{}** • {} • {}\n", - wishlist_str, card.print, card.name, card.series, last_update_ts_str - ) - .as_str(), - ) - } - reply_str.push_str(&format!("Time taken (to analyze): `{:?}`", duration)); - msg.reply(ctx, reply_str).await?; - } - Err(why) => { - helper::error_message( - ctx, - msg, - format!("Failed to analyze drop: `{:?}`", why), - None, - ) - .await; - } - }; - Ok(()) -}