refactor: minor changes

This commit is contained in:
Kristofers Solo 2025-11-19 00:51:50 +02:00
parent c5d073d29b
commit 800509c34d
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
3 changed files with 18 additions and 14 deletions

View File

@ -91,6 +91,7 @@ impl Comments {
/// Get a reference to the underlying lines for debugging or testing. /// Get a reference to the underlying lines for debugging or testing.
#[cfg(test)] #[cfg(test)]
#[must_use]
pub fn lines(&self) -> &[String] { pub fn lines(&self) -> &[String] {
&self.lines &self.lines
} }
@ -156,7 +157,7 @@ mod tests {
let caption = comments.build_caption(); let caption = comments.build_caption();
assert_eq!(caption.chars().count(), TELEGRAM_CAPTION_LIMIT); assert_eq!(caption.chars().count(), TELEGRAM_CAPTION_LIMIT);
assert!(caption.ends_with("...")) assert!(caption.ends_with("..."));
} }
#[test] #[test]

View File

@ -2,6 +2,8 @@ use crate::error::{Error, Result};
use std::{env, fmt::Debug, path::PathBuf, sync::OnceLock}; use std::{env, fmt::Debug, path::PathBuf, sync::OnceLock};
use teloxide::types::ChatId; use teloxide::types::ChatId;
pub const FAILED_FETCH_MEDIA_MESSAGE: &str = "Failed to fetch media, you foking donkey.";
static GLOBAL_CONFIG: OnceLock<Config> = OnceLock::new(); static GLOBAL_CONFIG: OnceLock<Config> = OnceLock::new();
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]

View File

@ -3,7 +3,7 @@ use teloxide::{prelude::*, respond, utils::command::BotCommands};
use tg_relay_rs::{ use tg_relay_rs::{
commands::{Command, answer}, commands::{Command, answer},
comments::Comments, comments::Comments,
config::{Config, global_config}, config::{Config, FAILED_FETCH_MEDIA_MESSAGE, global_config},
handler::{Handler, create_handlers}, handler::{Handler, create_handlers},
telemetry::setup_logger, telemetry::setup_logger,
}; };
@ -18,26 +18,26 @@ async fn main() -> color_eyre::Result<()> {
Comments::load_from_file("comments.txt") Comments::load_from_file("comments.txt")
.await .await
.map_err(|e| { .map_err(|e| {
warn!("failed to laod comments.txt: {e}; using dummy comments"); warn!("failed to load comments.txt: {e}; using dummy comments");
e e
}) })
.unwrap_or_else(|_| Comments::dummy()) .unwrap_or_else(|_| Comments::dummy())
.init() .init()?;
.expect("failed to initialize comments");
Config::from_env() Config::from_env().init()?;
.init()
.expect("failed to initialize config");
let bot = Bot::from_env(); 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(); let handlers = create_handlers();
teloxide::repl(bot.clone(), move |bot: Bot, msg: Message| { teloxide::repl(bot.clone(), move |bot: Bot, msg: Message| {
let handlers = handlers.clone(); let handlers = handlers.clone();
let bot_name_cloned = bot_name.clone();
async move { async move {
process_cmd(&bot, &msg).await; process_cmd(&bot, &msg, &bot_name_cloned).await;
process_message(&bot, &msg, &handlers).await; process_message(&bot, &msg, &handlers).await;
respond(()) 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 { if let Err(err) = handler.handle(bot, msg.chat.id, url).await {
error!(%err, "handler failed"); error!(%err, "handler failed");
let _ = bot let _ = bot
.send_message(msg.chat.id, "Failed to fetch media, you foking donkey.") .send_message(msg.chat.id, FAILED_FETCH_MEDIA_MESSAGE)
.await; .await;
if let Some(chat_id) = global_config().chat_id { if let Some(chat_id) = global_config().chat_id {
let _ = bot.send_message(chat_id, format!("{err}")).await; 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() 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");
} }
} }