Skip to content

Commit

Permalink
feat: better logging + code cleanup (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
j1nxie authored Nov 17, 2024
2 parents 5326539 + bc2e370 commit 53b26d7
Show file tree
Hide file tree
Showing 8 changed files with 1,228 additions and 800 deletions.
1,133 changes: 723 additions & 410 deletions Cargo.lock

Large diffs are not rendered by default.

45 changes: 26 additions & 19 deletions src/chapter_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel, Set};

use crate::{constants::MD_BLOCKED_LIST, models::manga, Data, Error};

#[tracing::instrument(skip_all)]
pub async fn chapter_tracker(http: &Http, webhook: &Webhook, data: &Data) -> Result<(), Error> {
let manga_list = manga::Entity::find().all(&data.db).await?;

Expand All @@ -18,25 +19,29 @@ pub async fn chapter_tracker(http: &Http, webhook: &Webhook, data: &Data) -> Res
.id(db_manga.manga_dex_id)
.get()
.send()
.await?;
.await
.inspect_err(|e|
tracing::error!(err = ?e, uuid = %db_manga.manga_dex_id, "an error occurred when fetching manga"),
)?;

let manga_id = manga.data.id;
let manga = manga.data.attributes;

let title =
if let Some(en_title) = manga.title.get(&mangadex_api_types_rust::Language::English) {
en_title
} else if let Some(jp_ro) = manga
.title
.get(&mangadex_api_types_rust::Language::JapaneseRomanized)
{
jp_ro
} else {
manga
let title = match manga.title.get(&mangadex_api_types_rust::Language::English) {
Some(en_title) => en_title,
None => {
match manga
.title
.get(&mangadex_api_types_rust::Language::Japanese)
.unwrap()
};
.get(&mangadex_api_types_rust::Language::JapaneseRomanized)
{
Some(jp_ro) => jp_ro,
None => manga
.title
.get(&mangadex_api_types_rust::Language::Japanese)
.unwrap(),
}
}
};

let chapter_feed = data
.md
Expand All @@ -56,7 +61,10 @@ pub async fn chapter_tracker(http: &Http, webhook: &Webhook, data: &Data) -> Res
.excluded_groups(MD_BLOCKED_LIST.clone())
.limit(1u32)
.send()
.await?;
.await
.inspect_err(|e|
tracing::error!(err = ?e, uuid = %db_manga.manga_dex_id, "an error occurred when fetching chapter feed"),
)?;

let mut db_manga_insert = db_manga.into_active_model();
let now = time::OffsetDateTime::now_utc();
Expand All @@ -70,10 +78,9 @@ pub async fn chapter_tracker(http: &Http, webhook: &Webhook, data: &Data) -> Res

match &chapter_data.chapter {
Some(chap) => {
let mut vol_chap_str = if let Some(vol) = &chapter_data.volume {
format!("Vol. {}, Ch. {}", vol, chap)
} else {
format!("Ch. {}", chap)
let mut vol_chap_str = match &chapter_data.volume {
Some(vol) => format!("Vol. {}, Ch. {}", vol, chap),
None => format!("Ch. {}", chap),
};

if let Some(chapter_title) = &chapter_data.title {
Expand Down
4 changes: 3 additions & 1 deletion src/commands/fluff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{Context, Error};
///
/// you want to listen to squartatrice? here you go.
#[poise::command(prefix_command, slash_command)]
#[tracing::instrument(skip_all)]
pub async fn squartatrice(ctx: Context<'_>) -> Result<(), Error> {
let random_number = rand::random::<u8>();

Expand All @@ -21,7 +22,8 @@ pub async fn squartatrice(ctx: Context<'_>) -> Result<(), Error> {
.allowed_mentions(CreateAllowedMentions::new().replied_user(false))
.content(content),
)
.await?;
.await
.inspect_err(|e| tracing::error!(err = ?e, "an error occurred when sending reply"))?;

Ok(())
}
6 changes: 5 additions & 1 deletion src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{Context, Error};

/// print the list of commands and their usage
#[poise::command(slash_command, prefix_command)]
#[tracing::instrument(skip_all)]
pub async fn help(
ctx: Context<'_>,
#[description = "specific command to show help about"] command: Option<String>,
Expand All @@ -10,7 +11,10 @@ pub async fn help(
extra_text_at_bottom: "Type `s>help command` for more info on a command.",
..Default::default()
};
poise::builtins::help(ctx, command.as_deref(), config).await?;

poise::builtins::help(ctx, command.as_deref(), config)
.await
.inspect_err(|e| tracing::error!(err = ?e, "an error occurred when sending reply"))?;

Ok(())
}
Loading

0 comments on commit 53b26d7

Please sign in to comment.