chore: move debug features to a file

This commit is contained in:
tretrauit 2024-01-07 13:23:25 +07:00
parent 3dcfdafc37
commit 410625e2b3
2 changed files with 249 additions and 231 deletions

237
swordfish/src/debug.rs Normal file
View File

@ -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<Message, ()> {
let mut args = msg.content.split(" ");
let target_channel_id = match args.nth(2) {
Some(content) => match content.parse::<u64>() {
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: `{} <channel ID> <message ID>`", command),
None,
)
.await;
return Err(());
}
};
let target_msg_id = match args.nth(0) {
Some(content) => match content.parse::<u64>() {
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: `{} <channel ID> <message ID>`", 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!("<t:{}:R>", 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(())
}

View File

@ -4,20 +4,18 @@ use serenity::all::MessageUpdateEvent;
use serenity::async_trait; use serenity::async_trait;
use serenity::framework::standard::macros::{command, group}; use serenity::framework::standard::macros::{command, group};
use serenity::framework::standard::{CommandResult, Configuration, StandardFramework}; use serenity::framework::standard::{CommandResult, Configuration, StandardFramework};
use serenity::model::{ use serenity::model::channel::Message;
channel::Message,
id::{ChannelId, MessageId},
};
use serenity::prelude::*; use serenity::prelude::*;
use std::env; use std::env;
use std::path::Path; use std::path::Path;
use std::sync::OnceLock; use std::sync::OnceLock;
use std::time::Instant;
use swordfish_common::*; use swordfish_common::*;
use tokio::time::Instant;
use crate::config::Config; use crate::config::Config;
mod config; mod config;
mod debug;
mod helper; mod helper;
mod katana; mod katana;
mod template; mod template;
@ -231,6 +229,9 @@ async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
#[command] #[command]
async fn debug(ctx: &Context, msg: &Message) -> CommandResult { async fn debug(ctx: &Context, msg: &Message) -> CommandResult {
let config = CONFIG.get().unwrap(); 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()) { if !config.debug.allowed_users.contains(&msg.author.id.get()) {
return Ok(()); return Ok(());
} }
@ -249,10 +250,11 @@ async fn debug(ctx: &Context, msg: &Message) -> CommandResult {
} }
}; };
match subcommand { match subcommand {
"kdropanalyze" => dbg_kdropanalyze(ctx, msg).await?, "info" => debug::dbg_info(ctx, msg).await?,
"kda" => dbg_kdropanalyze(ctx, msg).await?, "kdropanalyze" => debug::dbg_kdropanalyze(ctx, msg).await?,
"embed" => dbg_embed(ctx, msg).await?, "kda" => debug::dbg_kdropanalyze(ctx, msg).await?,
"parse-qingque-atopwl" => dbg_parse_qingque_atopwl(ctx, msg).await?, "embed" => debug::dbg_embed(ctx, msg).await?,
"parse-qingque-atopwl" => debug::dbg_parse_qingque_atopwl(ctx, msg).await?,
_ => { _ => {
helper::error_message( helper::error_message(
ctx, ctx,
@ -273,233 +275,12 @@ async fn info(ctx: &Context, msg: &Message) -> CommandResult {
"Swordfish v{} - {}\n\ "Swordfish v{} - {}\n\
Log level: `{}`\n\ Log level: `{}`\n\
Build type: `{}`\n\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\ Like my work? Consider supporting me at my [Ko-fi](https://ko-fi.com/tretrauit) or [Patreon](https://patreon.com/tretrauit)!",
*Debug information*\n\
Tesseract backend: `{}`\n\
",
env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_VERSION"),
GITHUB_URL, GITHUB_URL,
CONFIG.get().unwrap().log.level.clone().as_str(), CONFIG.get().unwrap().log.level.clone().as_str(),
env!("BUILD_PROFILE"), env!("BUILD_PROFILE"),
CONFIG.get().unwrap().tesseract.backend.clone().as_str(),
); );
helper::info_message(ctx, msg, reply_str, Some("Information".to_string())).await; helper::info_message(ctx, msg, reply_str, Some("Information".to_string())).await;
Ok(()) Ok(())
} }
async fn dbg_get_message(command: &str, ctx: &Context, msg: &Message) -> Result<Message, ()> {
let mut args = msg.content.split(" ");
let target_channel_id = match args.nth(2) {
Some(content) => match content.parse::<u64>() {
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: `{} <channel ID> <message ID>`", command),
None,
)
.await;
return Err(());
}
};
let target_msg_id = match args.nth(0) {
Some(content) => match content.parse::<u64>() {
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: `{} <channel ID> <message ID>`", 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!("<t:{}:R>", 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(())
}