feat: add ~info command

This commit is contained in:
tretrauit 2024-01-05 01:28:58 +07:00
parent d1cee333ed
commit 3c47a90611
4 changed files with 98 additions and 15 deletions

7
swordfish/build.rs Normal file
View File

@ -0,0 +1,7 @@
// Example custom build script.
fn main() {
println!(
"cargo:rustc-env=BUILD_PROFILE={}",
std::env::var("PROFILE").unwrap()
);
}

View File

@ -4,12 +4,28 @@ use serenity::client::Context;
use serenity::model::channel::Message; use serenity::model::channel::Message;
use swordfish_common::error; use swordfish_common::error;
pub async fn error_message(ctx: &Context, msg: &Message, content: String) { pub async fn error_message(ctx: &Context, msg: &Message, content: String, title: Option<String>) {
match msg match msg
.channel_id .channel_id
.send_message( .send_message(
ctx, ctx,
CreateMessage::new().add_embed(message::error_embed(ctx, None, Some(content)).await), CreateMessage::new().add_embed(message::error_embed(ctx, title, Some(content)).await),
)
.await
{
Ok(_) => (),
Err(why) => {
error!("Failed to send error message: {:?}", why);
}
};
}
pub async fn info_message(ctx: &Context, msg: &Message, content: String, title: Option<String>) {
match msg
.channel_id
.send_message(
ctx,
CreateMessage::new().add_embed(message::info_embed(ctx, title, Some(content)).await),
) )
.await .await
{ {

View File

@ -10,8 +10,6 @@ use swordfish_common::structs::Card;
use swordfish_common::tesseract; use swordfish_common::tesseract;
use swordfish_common::{trace, warn}; use swordfish_common::{trace, warn};
static TEXT_NUM_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"[A-Za-z0-9]").unwrap()); static TEXT_NUM_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"[A-Za-z0-9]").unwrap());
static ALLOWED_CHARS_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"['-: ]").unwrap()); static ALLOWED_CHARS_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"['-: ]").unwrap());
@ -91,7 +89,9 @@ fn fix_tesseract_string(text: &mut String) {
replace_string(text, "\n", " "); replace_string(text, "\n", " ");
// Remove all non-alphanumeric characters // Remove all non-alphanumeric characters
trace!("Text: {}", text); trace!("Text: {}", text);
text.retain(|c| TEXT_NUM_REGEX.is_match(&c.to_string()) || ALLOWED_CHARS_REGEX.is_match(&c.to_string())); text.retain(|c| {
TEXT_NUM_REGEX.is_match(&c.to_string()) || ALLOWED_CHARS_REGEX.is_match(&c.to_string())
});
// Fix "mn" -> "III" // Fix "mn" -> "III"
trace!("Text: {}", text); trace!("Text: {}", text);
if text.ends_with("mn") { if text.ends_with("mn") {
@ -168,7 +168,10 @@ pub fn analyze_card(card: image::DynamicImage, count: u32) -> Card {
panic!("{}", format!("Failed to write image: {:?}", why)); panic!("{}", format!("Failed to write image: {:?}", why));
} }
}; };
save_image_if_trace(&series_img, format!("debug/4-{}-series.png", count).as_str()); save_image_if_trace(
&series_img,
format!("debug/4-{}-series.png", count).as_str(),
);
leptess.set_image_from_mem(&buffer.get_mut()).unwrap(); leptess.set_image_from_mem(&buffer.get_mut()).unwrap();
let mut series_str = leptess.get_utf8_text().expect("Failed to read name"); let mut series_str = leptess.get_utf8_text().expect("Failed to read name");
fix_tesseract_string(&mut series_str); fix_tesseract_string(&mut series_str);

View File

@ -1,5 +1,5 @@
use dotenvy::dotenv; use dotenvy::dotenv;
use once_cell::sync::Lazy;
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};
@ -21,9 +21,10 @@ mod katana;
mod template; mod template;
const GITHUB_URL: &str = "https://github.com/teppyboy/swordfish"; const GITHUB_URL: &str = "https://github.com/teppyboy/swordfish";
static mut LOG_LEVEL: Lazy<String> = Lazy::new(|| "unknown".to_string());
#[group] #[group]
#[commands(ping, kdropanalyze)] #[commands(ping, kdropanalyze, info)]
struct General; struct General;
struct Handler; struct Handler;
#[async_trait] #[async_trait]
@ -78,6 +79,20 @@ async fn main() {
} }
let level_str = config.log.level; let level_str = config.log.level;
let log_level = env::var("LOG_LEVEL").unwrap_or(level_str); let log_level = env::var("LOG_LEVEL").unwrap_or(level_str);
unsafe {
// 1st way to kys
LOG_LEVEL = Lazy::new(|| {
let config: Config;
if Path::new("./config.toml").exists() {
config = config::Config::load("./config.toml");
} else {
config = config::Config::new();
config.save("./config.toml");
}
let level_str = config.log.level;
env::var("LOG_LEVEL").unwrap_or(level_str)
});
}
setup_logger(&log_level).expect("Failed to setup logger"); setup_logger(&log_level).expect("Failed to setup logger");
info!("Swordfish v{} - {}", env!("CARGO_PKG_VERSION"), GITHUB_URL); info!("Swordfish v{} - {}", env!("CARGO_PKG_VERSION"), GITHUB_URL);
info!("Log level: {}", log_level); info!("Log level: {}", log_level);
@ -115,7 +130,12 @@ async fn kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult {
Some(content) => match content.parse::<u64>() { Some(content) => match content.parse::<u64>() {
Ok(id) => id, Ok(id) => id,
Err(why) => { Err(why) => {
helper::error_message(ctx, msg, format!("Failed to parse channel ID: `{:?}`", why)) helper::error_message(
ctx,
msg,
format!("Failed to parse channel ID: `{:?}`", why),
None,
)
.await; .await;
return Ok(()); return Ok(());
} }
@ -125,6 +145,7 @@ async fn kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult {
ctx, ctx,
msg, msg,
"Usage: `kdropanalyze <channel ID> <message ID>`".to_string(), "Usage: `kdropanalyze <channel ID> <message ID>`".to_string(),
None,
) )
.await; .await;
return Ok(()); return Ok(());
@ -134,7 +155,12 @@ async fn kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult {
Some(content) => match content.parse::<u64>() { Some(content) => match content.parse::<u64>() {
Ok(id) => id, Ok(id) => id,
Err(why) => { Err(why) => {
helper::error_message(ctx, msg, format!("Failed to parse message ID: `{:?}`", why)) helper::error_message(
ctx,
msg,
format!("Failed to parse message ID: `{:?}`", why),
None,
)
.await; .await;
return Ok(()); return Ok(());
} }
@ -144,6 +170,7 @@ async fn kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult {
ctx, ctx,
msg, msg,
"Usage: `kdropanalyze <channel ID> <message ID>`".to_string(), "Usage: `kdropanalyze <channel ID> <message ID>`".to_string(),
None,
) )
.await; .await;
return Ok(()); return Ok(());
@ -159,7 +186,13 @@ async fn kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult {
{ {
Ok(msg) => msg, Ok(msg) => msg,
Err(why) => { Err(why) => {
helper::error_message(ctx, msg, format!("Failed to get message: `{:?}`", why)).await; helper::error_message(
ctx,
msg,
format!("Failed to get message: `{:?}`", why),
None,
)
.await;
return Ok(()); return Ok(());
} }
}; };
@ -182,9 +215,33 @@ async fn kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(ctx, reply_str).await?; msg.reply(ctx, reply_str).await?;
} }
Err(why) => { Err(why) => {
helper::error_message(ctx, msg, format!("Failed to analyze drop: `{:?}`", why)) helper::error_message(
ctx,
msg,
format!("Failed to analyze drop: `{:?}`", why),
None,
)
.await; .await;
} }
}; };
Ok(()) Ok(())
} }
#[command]
async fn info(ctx: &Context, msg: &Message) -> CommandResult {
unsafe {
let reply_str = format!(
"Swordfish v{} - {}\n\
Log level: `{}`\n\
Build type: `{}`\n\n\
Like my work? Consider donating to my [Ko-fi](https://ko-fi.com/teppyboy) or [Patreon](https://patreon.com/teppyboy)!\n\
",
env!("CARGO_PKG_VERSION"),
GITHUB_URL,
LOG_LEVEL.as_str(),
env!("BUILD_PROFILE"),
);
helper::info_message(ctx, msg, reply_str, Some("Information".to_string())).await;
}
Ok(())
}