refactor: minor changes

This commit is contained in:
2025-11-19 00:51:50 +02:00
parent c5d073d29b
commit 800509c34d
3 changed files with 18 additions and 14 deletions

View File

@@ -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");
}
}