-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
173 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,118 @@ | ||
use common::setting::SettingAll; | ||
use serenity::all::{ | ||
ChannelId, Color, Context, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter, | ||
CreateInteractionResponse, CreateInteractionResponseMessage, CreateMessage, User, UserId, | ||
}; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum MyError { | ||
#[error("Test")] | ||
Test, | ||
} | ||
|
||
enum Severity { | ||
Critical, | ||
FalsePossitive, | ||
CanBeHandledManually, | ||
} | ||
|
||
impl MyError { | ||
pub fn severity(&self) -> Severity { | ||
todo!() | ||
} | ||
pub fn advice(&self) -> String { | ||
todo!() | ||
} | ||
pub fn log(&self) { | ||
todo!() | ||
} | ||
} | ||
|
||
pub struct ErrorHandling { | ||
pub err: MyError, | ||
pub author: User, | ||
pub effected_user: User, | ||
pub location: String, | ||
pub log_channel: u64, | ||
} | ||
|
||
impl ErrorHandling { | ||
pub async fn new( | ||
err: MyError, | ||
ctx: &Context, | ||
setting: &SettingAll, | ||
effected_user: User, | ||
location: String, | ||
) -> Self { | ||
let thor = UserId::new(setting.main.discord.author); | ||
let author = thor.to_user(&ctx.http).await.unwrap_or_default(); | ||
Self { | ||
err, | ||
author, | ||
effected_user, | ||
location, | ||
log_channel: setting.discord.channel.error_channel, | ||
} | ||
} | ||
|
||
pub fn response(&self) -> CreateInteractionResponse { | ||
CreateInteractionResponse::Message( | ||
CreateInteractionResponseMessage::new().embed(self.embed()), | ||
) | ||
} | ||
pub async fn channel_send(&self, ctx: &Context) { | ||
let ch = ChannelId::new(self.log_channel); | ||
if ch | ||
.send_message( | ||
&ctx.http, | ||
CreateMessage::new() | ||
.embed(self.embed()) | ||
.content(format!("for {}", self.effected_user)), | ||
) | ||
.await | ||
.is_err() | ||
{ | ||
log::error!( | ||
"error sending log to channel, the error is: {:?}, on: {}, and user effected: {}", | ||
self.err, | ||
self.location, | ||
self.effected_user.name | ||
) | ||
} | ||
} | ||
|
||
fn embed(&self) -> CreateEmbed { | ||
let color = match self.err.severity() { | ||
Severity::Critical => Color::RED, | ||
Severity::FalsePossitive => Color::DARK_GREEN, | ||
Severity::CanBeHandledManually => Color::ORANGE, | ||
}; | ||
CreateEmbed::default() | ||
.color(color) | ||
.title("🛑 Error Occured 🛑") | ||
.description("some cant be handled error occured") | ||
.fields(vec![ | ||
( | ||
"🚧 occured on", | ||
format!("**{}**", self.location.to_uppercase()), | ||
false, | ||
), | ||
("📜 error message", format!("> {}", &self.err), false), | ||
( | ||
"⛑ author advice", | ||
format!("```\n{}\n```", &self.err.advice()), | ||
false, | ||
), | ||
]) | ||
.author( | ||
CreateEmbedAuthor::new(&self.effected_user.name) | ||
.icon_url(self.effected_user.face()), | ||
) | ||
.footer( | ||
CreateEmbedFooter::new(format!("you can consult this to {}", self.author.tag())) | ||
.icon_url(self.author.face()), | ||
) | ||
.thumbnail("https://media.discordapp.net/attachments/1068877712841789490/1303771799246344272/panics.png?ex=6730435b&is=672ef1db&hm=5f6e31b20eb02607c29ae1a961d97b694dee45ccbdc7889638edc4a6f93903a3&=&format=webp&quality=lossless&width=1000&height=660") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters