From c023b9b76448b6f59bf6654a9e788ca4bef1c94d Mon Sep 17 00:00:00 2001 From: doryan Date: Tue, 8 Oct 2024 23:37:22 +0400 Subject: [PATCH] feat(handlers): remove repeated code blocks and create one admin_command and info_command handlers --- src/endpoints/commands/ban_command.rs | 51 ------------ src/endpoints/commands/info_commands.rs | 16 ---- src/endpoints/commands/mod.rs | 4 - src/endpoints/commands/mute_command.rs | 85 -------------------- src/endpoints/commands/unmute_command.rs | 38 --------- src/handlers/commands/admin_commands.rs | 98 ++++++++++++++++++++++++ src/handlers/commands/info_commands.rs | 21 +++++ src/handlers/commands/mod.rs | 2 + 8 files changed, 121 insertions(+), 194 deletions(-) delete mode 100644 src/endpoints/commands/ban_command.rs delete mode 100644 src/endpoints/commands/info_commands.rs delete mode 100644 src/endpoints/commands/mod.rs delete mode 100644 src/endpoints/commands/mute_command.rs delete mode 100644 src/endpoints/commands/unmute_command.rs create mode 100644 src/handlers/commands/admin_commands.rs create mode 100644 src/handlers/commands/info_commands.rs create mode 100644 src/handlers/commands/mod.rs diff --git a/src/endpoints/commands/ban_command.rs b/src/endpoints/commands/ban_command.rs deleted file mode 100644 index e847fc6..0000000 --- a/src/endpoints/commands/ban_command.rs +++ /dev/null @@ -1,51 +0,0 @@ -use telers::{ - event::{ - EventReturn, - telegram::HandlerResult - }, - filters::CommandObject, - types::Message, - Bot -}; - -use crate::{ - assets::files::BAN_COMMAND_HELP, actions::ban::ban_member, - types::{ - enums::target_user::TargetUser, - structs::handler_entity::HandlerEntity - }, - utils::{ - general::cast_boxed_array::cast_boxed, - telegram::{ - args_parsers::get_user, - data_getters::get_chat_data, - senders::send_html - } - } -}; - -pub async fn ban(bot: Bot, message: Message, command: CommandObject) -> HandlerResult { - let args: Vec<&'static str> = cast_boxed(command.args); - - let (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message); - - let target_user: TargetUser = get_user( - handler_entity.clone(), - args.first().copied(), - &mut 0, - ); - - if args.is_empty() && !target_user.exist(){ - send_html(bot, message, BAN_COMMAND_HELP).await?; - - return Ok(EventReturn::Cancel); - } - - handler_entity - .message_sender_builder - .set_text("Нет ID или ответа на сообщение пользователя."); - - ban_member(handler_entity, chat_id, target_user, 0).await?; - - Ok(EventReturn::Finish) -} diff --git a/src/endpoints/commands/info_commands.rs b/src/endpoints/commands/info_commands.rs deleted file mode 100644 index 509d6ae..0000000 --- a/src/endpoints/commands/info_commands.rs +++ /dev/null @@ -1,16 +0,0 @@ -use telers::{event::telegram::HandlerResult, types::Message, Bot}; - -use crate::{ - assets::files::{HELP_COMMAND_TEXT, PRIVACY_COMMAND_TEXT}, - utils::telegram::senders::send_html, -}; - -#[inline] -pub async fn help(bot: Bot, msg: Message) -> HandlerResult { - send_html(bot, msg, HELP_COMMAND_TEXT).await -} - -#[inline] -pub async fn privacy(bot: Bot, msg: Message) -> HandlerResult { - send_html(bot, msg, PRIVACY_COMMAND_TEXT).await -} diff --git a/src/endpoints/commands/mod.rs b/src/endpoints/commands/mod.rs deleted file mode 100644 index 1077e29..0000000 --- a/src/endpoints/commands/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod ban_command; -pub mod info_commands; -pub mod mute_command; -pub mod unmute_command; diff --git a/src/endpoints/commands/mute_command.rs b/src/endpoints/commands/mute_command.rs deleted file mode 100644 index 9804b9e..0000000 --- a/src/endpoints/commands/mute_command.rs +++ /dev/null @@ -1,85 +0,0 @@ -use telers::{ - event::{telegram::HandlerResult, EventReturn}, - filters::CommandObject, - types::Message, - Bot, -}; - -use crate::{ - assets::files::MUTE_COMMAND_HELP, - actions::mute::mute_member, - types::{ - enums::{ - target_user::TargetUser, - time_metrics::TimeMetrics - }, - structs::handler_entity::HandlerEntity, - }, - utils::{ - general::cast_boxed_array::cast_boxed, - telegram::{ - args_parsers::get_user, - data_getters::get_chat_data, - senders::send_html - }, - }, -}; - -pub async fn mute(bot: Bot, message: Message, command: CommandObject) -> HandlerResult { - let (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message); - - let args: Vec<&'static str> = cast_boxed(command.args); - - let mut duration_argument_position = 0usize; - - let target_user: TargetUser = get_user( - handler_entity.clone(), - args.first().copied(), - &mut duration_argument_position, - ); - - if args.is_empty() && !target_user.exist(){ - send_html(bot, message, MUTE_COMMAND_HELP).await?; - - return Ok(EventReturn::Cancel); - } - - handler_entity - .message_sender_builder - .set_text("Нет ID или ответа на сообщение пользователя."); - - match args.get(duration_argument_position).cloned() { - Some(duration_str) => { - let metric = args - .get(duration_argument_position + 1) - .cloned() - .unwrap_or("d"); - - if let Ok(duration) = duration_str.parse::() { - let mute_duration = TimeMetrics::from(metric, duration); - - mute_member(handler_entity, chat_id, target_user, (mute_duration, 0)).await?; - } else { - handler_entity - .message_sender_builder - .build() - .send(&handler_entity.bot_instance) - .await?; - - return Ok(EventReturn::Cancel); - } - } - None => { - handler_entity - .message_sender_builder - .text("Не указана длительность мута.") - .build() - .send(&handler_entity.bot_instance) - .await?; - - return Ok(EventReturn::Cancel); - } - } - - Ok(EventReturn::Finish) -} diff --git a/src/endpoints/commands/unmute_command.rs b/src/endpoints/commands/unmute_command.rs deleted file mode 100644 index d3de11e..0000000 --- a/src/endpoints/commands/unmute_command.rs +++ /dev/null @@ -1,38 +0,0 @@ -use telers::{ - event::{telegram::HandlerResult, EventReturn}, - filters::CommandObject, - types::Message, - Bot, -}; - -use crate::{ - actions::unmute::unmute_member, - assets::files::UNMUTE_COMMAND_HELP, - types::{enums::target_user::TargetUser, structs::handler_entity::HandlerEntity}, - utils::{ - general::cast_boxed_array::cast_boxed, - telegram::{args_parsers::get_user, data_getters::get_chat_data, senders::send_html}, - }, -}; - -pub async fn unmute(bot: Bot, message: Message, command: CommandObject) -> HandlerResult { - let (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message); - - let args: Vec<&'static str> = cast_boxed(command.args); - - let target_user: TargetUser = get_user(handler_entity.clone(), args.first().copied(), &mut 0); - - if args.is_empty() && !target_user.exist() { - send_html(bot, message, UNMUTE_COMMAND_HELP).await?; - - return Ok(EventReturn::Cancel); - } - - handler_entity - .message_sender_builder - .set_text("Нет ID или ответа на сообщение пользователя."); - - unmute_member(handler_entity, chat_id, target_user).await?; - - Ok(EventReturn::Finish) -} diff --git a/src/handlers/commands/admin_commands.rs b/src/handlers/commands/admin_commands.rs new file mode 100644 index 0000000..ea65e51 --- /dev/null +++ b/src/handlers/commands/admin_commands.rs @@ -0,0 +1,98 @@ +use std::ops::Deref; + +use telers::{ + event::{telegram::HandlerResult, EventReturn}, + filters::CommandObject, + types::Message, + Bot, +}; + +use crate::{ + actions::{ban::ban_member, mute::mute_member, unmute::unmute_member}, + assets::files::{BAN_COMMAND_HELP, MUTE_COMMAND_HELP, UNMUTE_COMMAND_HELP}, + types::{ + enums::{target_user::TargetUser, time_metrics::TimeMetrics}, + structs::handler_entity::HandlerEntity, + }, + utils::{ + general::cast_boxed_array::cast_boxed, + telegram::{args_parsers::get_user, data_getters::get_chat_data, senders::send_html}, + }, +}; + +pub async fn admin_command_endpoint( + bot: Bot, + message: Message, + command: CommandObject, +) -> HandlerResult { + let command_type: &str = command.command.deref(); + + let args: Vec<&'static str> = cast_boxed(command.args); + + let (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message); + + let mut duration_argument_position = 0usize; + + let target_user: TargetUser = get_user( + handler_entity.clone(), + args.first().copied(), + &mut duration_argument_position, + ); + + if args.is_empty() && !target_user.exist() { + let help_txt = match command_type { + "ban" => BAN_COMMAND_HELP, + "mute" => MUTE_COMMAND_HELP, + "unmute" => UNMUTE_COMMAND_HELP, + _ => "Такой команды не существует.", + }; + + send_html(bot, message, help_txt).await?; + + return Ok(EventReturn::Cancel); + } + + handler_entity + .message_sender_builder + .set_text("Нет ID или ответа на сообщение пользователя."); + + match command_type { + "ban" => ban_member(handler_entity, chat_id, target_user, 0).await?, + "mute" => match args.get(duration_argument_position).cloned() { + Some(duration_str) => { + let metric = args + .get(duration_argument_position + 1) + .cloned() + .unwrap_or("d"); + + if let Ok(duration) = duration_str.parse::() { + let mute_duration = TimeMetrics::from(metric, duration); + + mute_member(handler_entity, chat_id, target_user, (mute_duration, 0)).await? + } else { + handler_entity + .message_sender_builder + .build() + .send(&handler_entity.bot_instance) + .await?; + + return Ok(EventReturn::Cancel); + } + } + None => { + handler_entity + .message_sender_builder + .text("Не указана длительность мута.") + .build() + .send(&handler_entity.bot_instance) + .await?; + + return Ok(EventReturn::Cancel); + } + }, + "unmute" => unmute_member(handler_entity, chat_id, target_user).await?, + _ => EventReturn::Finish, + }; + + Ok(EventReturn::Finish) +} diff --git a/src/handlers/commands/info_commands.rs b/src/handlers/commands/info_commands.rs new file mode 100644 index 0000000..4cba626 --- /dev/null +++ b/src/handlers/commands/info_commands.rs @@ -0,0 +1,21 @@ +use std::ops::Deref; + +use telers::{event::telegram::HandlerResult, filters::CommandObject, types::Message, Bot}; + +use crate::{ + assets::files::{HELP_COMMAND_TEXT, PRIVACY_COMMAND_TEXT}, + utils::telegram::senders::send_html, +}; + +#[inline] +pub async fn info_commands_endpoint( + bot: Bot, + msg: Message, + command: CommandObject, +) -> HandlerResult { + match command.command.deref() { + "help" => send_html(bot, msg, HELP_COMMAND_TEXT).await, + "privacy" => send_html(bot, msg, PRIVACY_COMMAND_TEXT).await, + _ => Ok(telers::event::EventReturn::Cancel), + } +} diff --git a/src/handlers/commands/mod.rs b/src/handlers/commands/mod.rs new file mode 100644 index 0000000..3ae4fef --- /dev/null +++ b/src/handlers/commands/mod.rs @@ -0,0 +1,2 @@ +pub mod admin_commands; +pub mod info_commands;