diff --git a/src/error_codes.rs b/src/error_codes.rs index 19bb768..95125a0 100644 --- a/src/error_codes.rs +++ b/src/error_codes.rs @@ -1,34 +1,37 @@ +use once_cell::sync::Lazy; +use regex::Regex; +use serenity::all::CreateActionRow::Buttons; +use serenity::all::{ + Context, CreateActionRow, CreateButton, CreateEmbed, CreateEmbedFooter, CreateMessage, + EditInteractionResponse, EditMessage, EventHandler, Http, Interaction, Message, +}; +use serenity::async_trait; use std::collections::BTreeMap; use std::sync::Arc; use std::time::Duration; -use once_cell::sync::Lazy; -use regex::Regex; -use serenity::all::{Context, CreateActionRow, CreateButton, CreateEmbed, CreateEmbedFooter, CreateMessage, EditInteractionResponse, EditMessage, EventHandler, Http, Interaction, Message}; -use serenity::all::CreateActionRow::Buttons; -use serenity::async_trait; use tokio::time::sleep; -mod errors{ +mod errors { include!(concat!(env!("OUT_DIR"), "/errors.rs")); } -struct CategoryInfo{ +struct CategoryInfo { name: &'static str, description: &'static str, system: &'static str, } -impl Default for CategoryInfo{ +impl Default for CategoryInfo { fn default() -> Self { - Self{ + Self { name: "unknown", description: "unknown", - system: "unknown" + system: "unknown", } } } -struct ErrorInfo{ +struct ErrorInfo { name: &'static str, message: &'static str, short_description: &'static str, @@ -38,9 +41,9 @@ struct ErrorInfo{ support_link: &'static str, } -impl Default for ErrorInfo{ +impl Default for ErrorInfo { fn default() -> Self { - Self{ + Self { name: "unknown", message: "unknown", long_description: "unknown", @@ -53,14 +56,11 @@ impl Default for ErrorInfo{ } static ERROR_CODE_REGEX: Lazy = - Lazy::new(|| Regex::new("\\d\\d\\d-\\d\\d\\d\\d").expect("invalid regex")); - - - + Lazy::new(|| Regex::new(" \\d\\d\\d-\\d\\d\\d\\d ").expect("invalid regex")); pub struct ErrorCodeHandler; -fn create_error_explain_message(str_code: &str, expanded: bool) -> Option{ +fn create_error_explain_message(str_code: &str, expanded: bool) -> Option { let Ok(category) = str_code[0..3].parse() else { return None; }; @@ -71,8 +71,7 @@ fn create_error_explain_message(str_code: &str, expanded: bool) -> Option Option, error_code: &str){ +fn start_timed_explanation_collapse(mut message: Message, http: Arc, error_code: &str) { let error_code: Box = error_code.into(); tokio::spawn(async move { sleep(Duration::from_secs(30)).await; let Some(new_message) = create_error_explain_message(&error_code, false) else { - return + return; }; message.edit(&http, new_message).await.ok(); @@ -126,15 +123,15 @@ fn start_timed_explanation_collapse(mut message: Message, http: Arc, error #[async_trait] impl EventHandler for ErrorCodeHandler { async fn message(&self, ctx: Context, msg: Message) { - if msg.author.bot{ + if msg.author.bot { return; } - if let Some(err_code) = ERROR_CODE_REGEX.find(&msg.content){ + if let Some(err_code) = ERROR_CODE_REGEX.find(&msg.content) { let str_code = err_code.as_str(); let Some(message) = create_error_explain_message(str_code, true) else { - return + return; }; let response = CreateMessage::new() @@ -142,10 +139,10 @@ impl EventHandler for ErrorCodeHandler { .reference_message(&msg); let Ok(mut msg) = msg.channel_id.send_message(&ctx.http, response).await else { - return + return; }; - let Some(edited) = create_error_explain_message(str_code, true) else { + let Some(edited) = create_error_explain_message(str_code, true) else { return; }; @@ -156,14 +153,14 @@ impl EventHandler for ErrorCodeHandler { } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { - if let Interaction::Component(mut component) = interaction{ - if component.data.custom_id.starts_with("ERROR_EXPLAIN"){ - let Some((_, error_code)) = component.data.custom_id.split_once(":") else{ + if let Interaction::Component(mut component) = interaction { + if component.data.custom_id.starts_with("ERROR_EXPLAIN") { + let Some((_, error_code)) = component.data.custom_id.split_once(":") else { return; }; let Some(msg) = create_error_explain_message(error_code, true) else { - return + return; }; component.message.edit(&ctx.http, msg).await.ok(); @@ -174,4 +171,4 @@ impl EventHandler for ErrorCodeHandler { } } } -} \ No newline at end of file +}