diff --git a/src/comments.rs b/src/comments.rs index c6bca6b..c55ca6e 100644 --- a/src/comments.rs +++ b/src/comments.rs @@ -91,6 +91,7 @@ impl Comments { /// Get a reference to the underlying lines for debugging or testing. #[cfg(test)] + #[must_use] pub fn lines(&self) -> &[String] { &self.lines } @@ -156,7 +157,7 @@ mod tests { let caption = comments.build_caption(); assert_eq!(caption.chars().count(), TELEGRAM_CAPTION_LIMIT); - assert!(caption.ends_with("...")) + assert!(caption.ends_with("...")); } #[test] diff --git a/src/config.rs b/src/config.rs index 89e689a..defa888 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,8 @@ use crate::error::{Error, Result}; use std::{env, fmt::Debug, path::PathBuf, sync::OnceLock}; use teloxide::types::ChatId; +pub const FAILED_FETCH_MEDIA_MESSAGE: &str = "Failed to fetch media, you foking donkey."; + static GLOBAL_CONFIG: OnceLock = OnceLock::new(); #[derive(Debug, Clone, Default)] diff --git a/src/main.rs b/src/main.rs index e9f6ecc..39ca7d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use teloxide::{prelude::*, respond, utils::command::BotCommands}; use tg_relay_rs::{ commands::{Command, answer}, comments::Comments, - config::{Config, global_config}, + config::{Config, FAILED_FETCH_MEDIA_MESSAGE, global_config}, handler::{Handler, create_handlers}, telemetry::setup_logger, }; @@ -18,26 +18,26 @@ async fn main() -> color_eyre::Result<()> { Comments::load_from_file("comments.txt") .await .map_err(|e| { - warn!("failed to laod comments.txt: {e}; using dummy comments"); + warn!("failed to load comments.txt: {e}; using dummy comments"); e }) .unwrap_or_else(|_| Comments::dummy()) - .init() - .expect("failed to initialize comments"); + .init()?; - Config::from_env() - .init() - .expect("failed to initialize config"); + Config::from_env().init()?; let bot = Bot::from_env(); - info!("bot starting"); + let bot_name = bot.get_me().await?.username().to_owned(); + + info!(name = bot_name, "bot starting"); let handlers = create_handlers(); teloxide::repl(bot.clone(), move |bot: Bot, msg: Message| { let handlers = handlers.clone(); + let bot_name_cloned = bot_name.clone(); async move { - process_cmd(&bot, &msg).await; + process_cmd(&bot, &msg, &bot_name_cloned).await; process_message(&bot, &msg, &handlers).await; respond(()) } @@ -57,7 +57,7 @@ async fn process_message(bot: &Bot, msg: &Message, handlers: &[Handler]) { if let Err(err) = handler.handle(bot, msg.chat.id, url).await { error!(%err, "handler failed"); let _ = bot - .send_message(msg.chat.id, "Failed to fetch media, you foking donkey.") + .send_message(msg.chat.id, FAILED_FETCH_MEDIA_MESSAGE) .await; if let Some(chat_id) = global_config().chat_id { let _ = bot.send_message(chat_id, format!("{err}")).await; @@ -68,10 +68,11 @@ async fn process_message(bot: &Bot, msg: &Message, handlers: &[Handler]) { } } -async fn process_cmd(bot: &Bot, msg: &Message) { +async fn process_cmd(bot: &Bot, msg: &Message, bot_name: &str) { if let Some(text) = msg.text() - && let Ok(cmd) = Command::parse(text, "guenter") + && let Ok(cmd) = Command::parse(text, bot_name) + && let Err(e) = answer(bot, msg, cmd).await { - let _ = answer(bot, msg, cmd).await; + error!(%e, "failed to answer command"); } }