make it pretty

This commit is contained in:
Sense T 2023-12-26 12:44:12 +08:00
parent a89496676b
commit f34fc54b3a
2 changed files with 26 additions and 19 deletions

View File

@ -95,8 +95,17 @@ impl CommandHandler {
bot: &Bot, bot: &Bot,
message: &Message, message: &Message,
) -> Result<JsonRequest<SendMessage>, Box<dyn Error + Send + Sync>> { ) -> Result<JsonRequest<SendMessage>, Box<dyn Error + Send + Sync>> {
if let Some(reply) = message.reply_to_message() { let reply = match message.reply_to_message() {
if let Some(user) = reply.from() { 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 mut vars = HashMap::new();
let index = OsRng.gen::<usize>() % BOT_TEXT_HANGED.len(); let index = OsRng.gen::<usize>() % BOT_TEXT_HANGED.len();
let text = BOT_TEXT_HANGED[index]; let text = BOT_TEXT_HANGED[index];
@ -110,15 +119,10 @@ impl CommandHandler {
Ok(self Ok(self
.send_text_reply(bot, reply, escape(&text.format(&vars).unwrap())) .send_text_reply(bot, reply, escape(&text.format(&vars).unwrap()))
.await) .await)
} else {
Ok(self
.send_text_reply(bot, message, BOT_TEXT_IS_CHANNEL.to_string())
.await)
} }
} else { None => Ok(self
Ok(self .send_text_reply(bot, message, BOT_TEXT_IS_CHANNEL.to_string())
.send_text_reply(bot, message, BOT_TEXT_NO_TARGET.to_string()) .await),
.await)
} }
} }

View File

@ -38,15 +38,7 @@ async fn main() {
let bot = Bot::new(args.tgbot_token.to_owned()) let bot = Bot::new(args.tgbot_token.to_owned())
.set_api_url(reqwest::Url::parse(&args.api_url.as_str()).unwrap()); .set_api_url(reqwest::Url::parse(&args.api_url.as_str()).unwrap());
match bot.get_me().send().await { get_me(&bot).await;
Ok(result) => log_info_ln!(
"connect succeed: id={}, botname=\"{}\"",
result.id,
result.username()
),
Err(error) => log_panic!("{}", error),
}
register_commands(&bot).await; register_commands(&bot).await;
Commands::repl(bot, move |bot: Bot, message: Message, cmd: Commands| { 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") 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),
}
}