From f34fc54b3a543f07a26488ed9b0ef11148cb8ff7 Mon Sep 17 00:00:00 2001 From: Sense T Date: Tue, 26 Dec 2023 12:44:12 +0800 Subject: [PATCH] make it pretty --- src/commands.rs | 24 ++++++++++++++---------- src/main.rs | 21 ++++++++++++--------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 43896d1..ce15286 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -95,8 +95,17 @@ impl CommandHandler { bot: &Bot, message: &Message, ) -> Result, Box> { - if let Some(reply) = message.reply_to_message() { - if let Some(user) = reply.from() { + let reply = match message.reply_to_message() { + Some(reply) => reply, + None => { + return Ok(self + .send_text_reply(bot, message, BOT_TEXT_NO_TARGET.to_string()) + .await) + } + }; + + match reply.from() { + Some(user) => { let mut vars = HashMap::new(); let index = OsRng.gen::() % BOT_TEXT_HANGED.len(); let text = BOT_TEXT_HANGED[index]; @@ -110,15 +119,10 @@ impl CommandHandler { Ok(self .send_text_reply(bot, reply, escape(&text.format(&vars).unwrap())) .await) - } else { - Ok(self - .send_text_reply(bot, message, BOT_TEXT_IS_CHANNEL.to_string()) - .await) } - } else { - Ok(self - .send_text_reply(bot, message, BOT_TEXT_NO_TARGET.to_string()) - .await) + None => Ok(self + .send_text_reply(bot, message, BOT_TEXT_IS_CHANNEL.to_string()) + .await), } } diff --git a/src/main.rs b/src/main.rs index 4e41771..729f3b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,15 +38,7 @@ async fn main() { let bot = Bot::new(args.tgbot_token.to_owned()) .set_api_url(reqwest::Url::parse(&args.api_url.as_str()).unwrap()); - match bot.get_me().send().await { - Ok(result) => log_info_ln!( - "connect succeed: id={}, botname=\"{}\"", - result.id, - result.username() - ), - Err(error) => log_panic!("{}", error), - } - + get_me(&bot).await; register_commands(&bot).await; Commands::repl(bot, move |bot: Bot, message: Message, cmd: Commands| { @@ -71,3 +63,14 @@ async fn register_commands(bot: &Bot) { log_info_ln!("commands registered") } } + +async fn get_me(bot: &Bot) { + match bot.get_me().send().await { + Ok(result) => log_info_ln!( + "connect succeed: id={}, botname=\"{}\"", + result.id, + result.username() + ), + Err(error) => log_panic!("{}", error), + } +} \ No newline at end of file