diff --git a/examples/admin.rs b/examples/admin.rs index 194b8b54..09754a3d 100644 --- a/examples/admin.rs +++ b/examples/admin.rs @@ -53,17 +53,35 @@ impl FromStr for UnitOfTime { } } -// Calculates time of user restriction. -fn calc_restrict_time(time: u64, unit: UnitOfTime) -> Duration { - match unit { - UnitOfTime::Hours => Duration::hours(time as i64), - UnitOfTime::Minutes => Duration::minutes(time as i64), - UnitOfTime::Seconds => Duration::seconds(time as i64), - } +#[tokio::main] +async fn main() { + pretty_env_logger::init(); + log::info!("Starting admin_bot..."); + + let bot = teloxide::Bot::from_env().auto_send(); + + teloxide::commands_repl(bot, action, Command::ty()).await; } type Bot = AutoSend; +async fn action( + bot: Bot, + msg: Message, + command: Command, +) -> Result<(), Box> { + match command { + Command::Help => { + bot.send_message(msg.chat.id, Command::descriptions()).await?; + } + Command::Kick => kick_user(bot, msg).await?, + Command::Ban { time, unit } => ban_user(bot, msg, calc_restrict_time(time, unit)).await?, + Command::Mute { time, unit } => mute_user(bot, msg, calc_restrict_time(time, unit)).await?, + }; + + Ok(()) +} + // Kick a user with a replied message. async fn kick_user(bot: Bot, msg: Message) -> Result<(), Box> { match msg.reply_to_message() { @@ -78,6 +96,29 @@ async fn kick_user(bot: Bot, msg: Message) -> Result<(), Box Result<(), Box> { + match msg.reply_to_message() { + Some(replied) => { + bot.kick_chat_member( + msg.chat.id, + replied.from().expect("Must be MessageKind::Common").id, + ) + .until_date(msg.date + time) + .await?; + } + None => { + bot.send_message(msg.chat.id, "Use this command in a reply to another message!") + .await?; + } + } + Ok(()) +} + // Mute a user with a replied message. async fn mute_user( bot: Bot, @@ -102,52 +143,11 @@ async fn mute_user( Ok(()) } -// Ban a user with replied message. -async fn ban_user( - bot: Bot, - msg: Message, - time: Duration, -) -> Result<(), Box> { - match msg.reply_to_message() { - Some(replied) => { - bot.kick_chat_member( - msg.chat.id, - replied.from().expect("Must be MessageKind::Common").id, - ) - .until_date(msg.date + time) - .await?; - } - None => { - bot.send_message(msg.chat.id, "Use this command in a reply to another message!") - .await?; - } +// Calculates time of user restriction. +fn calc_restrict_time(time: u64, unit: UnitOfTime) -> Duration { + match unit { + UnitOfTime::Hours => Duration::hours(time as i64), + UnitOfTime::Minutes => Duration::minutes(time as i64), + UnitOfTime::Seconds => Duration::seconds(time as i64), } - Ok(()) -} - -async fn action( - bot: Bot, - msg: Message, - command: Command, -) -> Result<(), Box> { - match command { - Command::Help => { - bot.send_message(msg.chat.id, Command::descriptions()).await?; - } - Command::Kick => kick_user(bot, msg).await?, - Command::Ban { time, unit } => ban_user(bot, msg, calc_restrict_time(time, unit)).await?, - Command::Mute { time, unit } => mute_user(bot, msg, calc_restrict_time(time, unit)).await?, - }; - - Ok(()) -} - -#[tokio::main] -async fn main() { - pretty_env_logger::init(); - log::info!("Starting admin_bot..."); - - let bot = teloxide::Bot::from_env().auto_send(); - - teloxide::commands_repl(bot, action, Command::ty()).await; } diff --git a/examples/buttons.rs b/examples/buttons.rs index fa49e1de..ce8bfb1a 100644 --- a/examples/buttons.rs +++ b/examples/buttons.rs @@ -18,6 +18,25 @@ enum Command { Start, } +#[tokio::main] +async fn main() -> Result<(), Box> { + pretty_env_logger::init(); + log::info!("Starting bot..."); + + let bot = Bot::from_env().auto_send(); + + let handler = dptree::entry() + .branch(Update::filter_message().endpoint(message_handler)) + .branch(Update::filter_callback_query().endpoint(callback_handler)) + .branch(Update::filter_inline_query().endpoint(inline_query_handler)); + + Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await; + + log::info!("Closing bot... Goodbye!"); + + Ok(()) +} + /// Creates a keyboard made by buttons in a big column. fn make_keyboard() -> InlineKeyboardMarkup { let mut keyboard: Vec> = vec![]; @@ -111,22 +130,3 @@ async fn callback_handler( Ok(()) } - -#[tokio::main] -async fn main() -> Result<(), Box> { - pretty_env_logger::init(); - log::info!("Starting bot..."); - - let bot = Bot::from_env().auto_send(); - - let handler = dptree::entry() - .branch(Update::filter_message().endpoint(message_handler)) - .branch(Update::filter_callback_query().endpoint(callback_handler)) - .branch(Update::filter_inline_query().endpoint(inline_query_handler)); - - Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await; - - log::info!("Closing bot... Goodbye!"); - - Ok(()) -}