fix error code regex
All checks were successful
Build and Test / splatie (push) Successful in 14m55s
All checks were successful
Build and Test / splatie (push) Successful in 14m55s
This commit is contained in:
parent
d0e0e6a4eb
commit
1fd94cf343
1 changed files with 31 additions and 34 deletions
|
|
@ -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<Regex> =
|
||||
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<EditMessage>{
|
||||
fn create_error_explain_message(str_code: &str, expanded: bool) -> Option<EditMessage> {
|
||||
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<EditMe
|
|||
|
||||
let (category_info, error_info) = errors::get_error_code_and_category(category, error);
|
||||
|
||||
let mut embed = CreateEmbed::new()
|
||||
.title(str_code);
|
||||
let mut embed = CreateEmbed::new().title(str_code);
|
||||
|
||||
if expanded {
|
||||
embed = embed
|
||||
|
|
@ -101,22 +100,20 @@ fn create_error_explain_message(str_code: &str, expanded: bool) -> Option<EditMe
|
|||
|
||||
let message = EditMessage::new()
|
||||
.embed(embed)
|
||||
.components(vec![
|
||||
CreateActionRow::Buttons(vec![expand_button])
|
||||
])
|
||||
.components(vec![CreateActionRow::Buttons(vec![expand_button])])
|
||||
.content("");
|
||||
|
||||
Some(message)
|
||||
}
|
||||
|
||||
fn start_timed_explanation_collapse(mut message: Message, http: Arc<Http>, error_code: &str){
|
||||
fn start_timed_explanation_collapse(mut message: Message, http: Arc<Http>, error_code: &str) {
|
||||
let error_code: Box<str> = 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<Http>, 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 {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue