From 91bea5be5e6550f815b7dd4dae8451a4520aed81 Mon Sep 17 00:00:00 2001 From: Maybe Waffle <waffle.lapkin@gmail.com> Date: Sun, 27 Mar 2022 13:11:59 +0400 Subject: [PATCH] Apply suggestions from the review - remove `username_from_bot` - add `username_from_me` --- examples/dispatching2_features.rs | 49 ++++++++++++++++--------------- src/utils/command.rs | 22 +++++--------- 2 files changed, 32 insertions(+), 39 deletions(-) diff --git a/examples/dispatching2_features.rs b/examples/dispatching2_features.rs index d8e141d9..bc79b956 100644 --- a/examples/dispatching2_features.rs +++ b/examples/dispatching2_features.rs @@ -7,7 +7,7 @@ use rand::Rng; // dispatching system, which will be deprecated in the future. use teloxide::{ prelude2::*, - types::{Dice, Update}, + types::{Dice, Me, Update}, utils::command::BotCommands, }; @@ -26,28 +26,6 @@ async fn main() { let handler = Update::filter_message() // You can use branching to define multiple ways in which an update will be handled. If the // first branch fails, an update will be passed to the second branch, and so on. - .branch( - // Filtering allow you to filter updates by some condition. - dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup()) - // An endpoint is the last update handler. - .endpoint(|msg: Message, bot: AutoSend<Bot>| async move { - log::info!("Received a message from a group chat."); - bot.send_message(msg.chat.id, "This is a group chat.").await?; - respond(()) - }), - ) - .branch( - // There are some extension filtering functions on `Message`. The following filter will - // filter only messages with dices. - Message::filter_dice().endpoint( - |msg: Message, dice: Dice, bot: AutoSend<Bot>| async move { - bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value)) - .reply_to_message_id(msg.id) - .await?; - Ok(()) - }, - ), - ) .branch( dptree::entry() // Filter commands: the next handlers will receive a parsed `SimpleCommand`. @@ -74,6 +52,28 @@ async fn main() { } }, ), + ) + .branch( + // Filtering allow you to filter updates by some condition. + dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup()) + // An endpoint is the last update handler. + .endpoint(|msg: Message, bot: AutoSend<Bot>| async move { + log::info!("Received a message from a group chat."); + bot.send_message(msg.chat.id, "This is a group chat.").await?; + respond(()) + }), + ) + .branch( + // There are some extension filtering functions on `Message`. The following filter will + // filter only messages with dices. + Message::filter_dice().endpoint( + |msg: Message, dice: Dice, bot: AutoSend<Bot>| async move { + bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value)) + .reply_to_message_id(msg.id) + .await?; + Ok(()) + }, + ), ); Dispatcher::builder(bot, handler) @@ -124,6 +124,7 @@ async fn simple_commands_handler( bot: AutoSend<Bot>, cmd: SimpleCommand, cfg: ConfigParameters, + me: Me, ) -> Result<(), teloxide::RequestError> { let text = match cmd { SimpleCommand::Help => { @@ -134,7 +135,7 @@ async fn simple_commands_handler( MaintainerCommands::descriptions() ) } else if msg.chat.is_group() || msg.chat.is_supergroup() { - SimpleCommand::descriptions().username("USERNAME_BOT").to_string() + SimpleCommand::descriptions().username_from_me(&me).to_string() } else { SimpleCommand::descriptions().to_string() } diff --git a/src/utils/command.rs b/src/utils/command.rs index c2d962fc..ee77b11e 100644 --- a/src/utils/command.rs +++ b/src/utils/command.rs @@ -51,16 +51,12 @@ use core::fmt; use std::{ - borrow::Cow, error::Error, fmt::{Display, Formatter, Write}, }; use std::marker::PhantomData; -use teloxide_core::{ - requests::{Request, Requester}, - types::{BotCommand, Me}, -}; +use teloxide_core::types::{BotCommand, Me}; #[cfg(feature = "macros")] pub use teloxide_macros::BotCommands; @@ -282,7 +278,7 @@ pub enum ParseError { pub struct CommandDescriptions<'a> { global_description: Option<&'a str>, descriptions: &'a [CommandDescription<'a>], - bot_username: Option<Cow<'a, str>>, + bot_username: Option<&'a str>, } /// Description of a particular command, used in [`CommandDescriptions`]. @@ -331,21 +327,17 @@ impl<'a> CommandDescriptions<'a> { /// ); /// ``` pub fn username(self, bot_username: &'a str) -> Self { - Self { bot_username: Some(Cow::Borrowed(bot_username)), ..self } + Self { bot_username: Some(bot_username), ..self } } /// Sets the username of the bot. /// - /// This is the same as [`username`], but uses `get_me` method of the bot to - /// get the username. + /// This is the same as [`username`], but uses value returned from `get_me` + /// method to get the username. /// /// [`username`]: self::CommandDescriptions::username - pub async fn username_from_bot(self, bot: &impl Requester) -> CommandDescriptions<'a> { - let Me { user, .. } = bot.get_me().send().await.expect("get_me failed"); - Self { - bot_username: Some(Cow::Owned(user.username.expect("Bots must have usernames"))), - ..self - } + pub fn username_from_me(self, me: &'a Me) -> CommandDescriptions<'a> { + self.username(me.user.username.as_deref().expect("Bots must have usernames")) } }