diff --git a/src/utils/general/cast_boxed_array.rs b/src/utils/general/cast_boxed_array.rs new file mode 100644 index 0000000..e681d31 --- /dev/null +++ b/src/utils/general/cast_boxed_array.rs @@ -0,0 +1,11 @@ +pub fn cast_box<'a>(boxed: Box) -> &'a str { + unsafe { &*Box::into_raw(boxed) } +} + +#[allow(clippy::boxed_local)] +pub fn cast_boxed<'a>(boxed_array: Box<[Box]>) -> Vec<&'a str> { + boxed_array + .iter() + .map(|arg| cast_box(arg.to_owned())) + .collect() +} diff --git a/src/utils/general/unrestrict_date.rs b/src/utils/general/expiration_date.rs similarity index 88% rename from src/utils/general/unrestrict_date.rs rename to src/utils/general/expiration_date.rs index 0f66171..036e741 100644 --- a/src/utils/general/unrestrict_date.rs +++ b/src/utils/general/expiration_date.rs @@ -3,7 +3,7 @@ use chrono::{Duration, Local, NaiveDateTime}; use crate::types::{enums::time_metrics::TimeMetrics, TimeValues}; #[inline] -pub fn unrestrict_date(duration: TimeValues) -> NaiveDateTime { +pub fn expiration_date(duration: TimeValues) -> NaiveDateTime { let mute_duration = match duration.0 { TimeMetrics::Minutes(min) => Duration::minutes(min), TimeMetrics::Hours(hrs) => Duration::hours(hrs), diff --git a/src/utils/general/get_duration.rs b/src/utils/general/get_expiration_time.rs similarity index 71% rename from src/utils/general/get_duration.rs rename to src/utils/general/get_expiration_time.rs index d098b6c..4044e45 100644 --- a/src/utils/general/get_duration.rs +++ b/src/utils/general/get_expiration_time.rs @@ -4,13 +4,13 @@ use crate::types::{ structs::countable_time::CountableTime, traits::countable_interface::ICountable, TimeValues, }; -use super::unrestrict_date::unrestrict_date; +use super::expiration_date::expiration_date; pub type ExtractedDuration = (NaiveDateTime, String, i64); -pub fn get_duration(time: TimeValues) -> ExtractedDuration { +pub fn get_expiration_time(time: TimeValues) -> ExtractedDuration { let time_duration = time.0.extract(); - let unmute_date = unrestrict_date(time); + let unmute_date = expiration_date(time); let postfix = CountableTime::from_value(time_duration) .get_postfix(time.0) .unwrap(); diff --git a/src/utils/general/mod.rs b/src/utils/general/mod.rs index cbe6b73..efcc1a6 100644 --- a/src/utils/general/mod.rs +++ b/src/utils/general/mod.rs @@ -1,3 +1,3 @@ -pub mod get_duration; -pub mod parse_boxed_array; -pub mod unrestrict_date; +pub mod expiration_date; +pub mod get_expiration_time; +pub mod cast_boxed_array; diff --git a/src/utils/general/parse_boxed_array.rs b/src/utils/general/parse_boxed_array.rs deleted file mode 100644 index 4d97eb4..0000000 --- a/src/utils/general/parse_boxed_array.rs +++ /dev/null @@ -1,11 +0,0 @@ -pub fn parse_box<'a>(boxed: Box) -> &'a str { - unsafe { &*Box::into_raw(boxed) } -} - -#[allow(clippy::boxed_local)] -pub fn parse_boxed<'a>(boxed_array: Box<[Box]>) -> Vec<&'a str> { - boxed_array - .iter() - .map(|arg| parse_box(arg.to_owned())) - .collect() -} diff --git a/src/utils/telegram/admin_check.rs b/src/utils/telegram/admin_check.rs index 894c0ca..c088174 100644 --- a/src/utils/telegram/admin_check.rs +++ b/src/utils/telegram/admin_check.rs @@ -1,6 +1,7 @@ use telers::types::chat_member::ChatMember; pub fn is_admin(all_admin_members: &Vec, user_id: i64) -> bool { + //fix: moderators with non full rights can use admin commands now. all_admin_members .iter() .any(|admin: &ChatMember| match admin { @@ -9,10 +10,7 @@ pub fn is_admin(all_admin_members: &Vec, user_id: i64) -> bool { && admin.can_change_info && admin.can_delete_messages && admin.can_promote_members - && admin.can_manage_chat && admin.can_restrict_members - && admin.can_invite_users - && admin.can_manage_topics.unwrap() } ChatMember::Owner(owner) => owner.user.id == user_id, _ => false, diff --git a/src/utils/telegram/args_parsers.rs b/src/utils/telegram/args_parsers.rs new file mode 100644 index 0000000..6b490b6 --- /dev/null +++ b/src/utils/telegram/args_parsers.rs @@ -0,0 +1,28 @@ +use crate::types::{ + enums::target_user::TargetUser, + structs::handler_entity::HandlerEntity +}; + +pub fn get_user( + handler_entity: HandlerEntity, + arg: Option<&str>, + arg_pos: &mut usize, +) -> TargetUser { + match ( + handler_entity.message_reciever.reply_to_message(), + arg, + ) { + (Some(msg), _) => TargetUser::CurrentMessage(msg.clone()), + (None, Some(raw_id)) => { + *arg_pos += 1; + if let Ok(id) = raw_id.parse::() { + TargetUser::Id(id) + } else { + TargetUser::None +} + } + (None, None) => { + TargetUser::None + } + } +} diff --git a/src/utils/telegram/ban_member.rs b/src/utils/telegram/ban_member.rs deleted file mode 100644 index 6c8141a..0000000 --- a/src/utils/telegram/ban_member.rs +++ /dev/null @@ -1,6 +0,0 @@ -use telers::{errors::session::ErrorKind, methods::BanChatMember, Bot}; - -#[inline] -pub async fn ban_chat_member(bot: &Bot, chat_id: i64, user_id: i64) -> Result { - bot.send(BanChatMember::new(chat_id, user_id)).await -} diff --git a/src/utils/telegram/data_getters.rs b/src/utils/telegram/data_getters.rs new file mode 100644 index 0000000..5938f38 --- /dev/null +++ b/src/utils/telegram/data_getters.rs @@ -0,0 +1,22 @@ +use telers::{ + methods::GetChatAdministrators, + types::{ChatMember, Message}, + Bot, + errors::SessionErrorKind +}; + +use crate::types::structs::{handler_entity::HandlerEntity, message_sender::MessageSender}; + +pub fn get_chat_data(bot: &Bot, message: &Message) -> (i64, HandlerEntity) { + let (message_id, chat_id): (i64, i64) = (message.id(), message.chat().id()); + let sender = MessageSender::builder(chat_id).reply_to(message_id); + let handler_entity: HandlerEntity = HandlerEntity::new(bot.clone(), message.clone(), sender); + + (chat_id, handler_entity) +} + +#[inline] +pub async fn get_all_admins(bot: &Bot, chat_id: i64) -> Result, SessionErrorKind> { + bot.send(GetChatAdministrators::new(chat_id)) + .await +} diff --git a/src/utils/telegram/demote.rs b/src/utils/telegram/demote.rs deleted file mode 100644 index 140e3ae..0000000 --- a/src/utils/telegram/demote.rs +++ /dev/null @@ -1,21 +0,0 @@ -use telers::{errors::session::ErrorKind, methods::promote_chat_member::PromoteChatMember, Bot}; - -#[inline] -pub async fn demote_user(bot: &Bot, user_id: i64, chat_id: i64) -> Result { - bot.send( - PromoteChatMember::new(chat_id, user_id) - .can_manage_topics(false) - .can_pin_messages(false) - .can_invite_users(false) - .can_change_info(false) - .can_promote_members(false) - .can_restrict_members(false) - .can_manage_voice_chats(false) - .can_delete_messages(false) - .can_edit_messages(false) - .can_post_messages(false) - .can_manage_chat(false) - .is_anonymous(false), - ) - .await -} diff --git a/src/utils/telegram/get_all_admins.rs b/src/utils/telegram/get_all_admins.rs deleted file mode 100644 index 2293b86..0000000 --- a/src/utils/telegram/get_all_admins.rs +++ /dev/null @@ -1,7 +0,0 @@ -use telers::{errors::SessionErrorKind, methods::get_chat_administrators, types::ChatMember, Bot}; - -#[inline] -pub async fn get_all_admins(bot: &Bot, chat_id: i64) -> Result, SessionErrorKind> { - bot.send(get_chat_administrators::GetChatAdministrators::new(chat_id)) - .await -} diff --git a/src/utils/telegram/mod.rs b/src/utils/telegram/mod.rs index ff0e142..2b27ebd 100644 --- a/src/utils/telegram/mod.rs +++ b/src/utils/telegram/mod.rs @@ -1,6 +1,6 @@ pub mod admin_check; -pub mod ban_member; -pub mod demote; -pub mod get_all_admins; -pub mod restrict; +pub mod args_parsers; +pub mod data_getters; +pub mod rights_control; +pub mod senders; pub mod try_do; diff --git a/src/utils/telegram/restrict.rs b/src/utils/telegram/rights_control.rs similarity index 61% rename from src/utils/telegram/restrict.rs rename to src/utils/telegram/rights_control.rs index 827385f..759fb51 100644 --- a/src/utils/telegram/restrict.rs +++ b/src/utils/telegram/rights_control.rs @@ -31,3 +31,23 @@ pub async fn restrict( ) .await } + +#[inline] +pub async fn demote_user(bot: &Bot, user_id: i64, chat_id: i64) -> Result { + bot.send( + PromoteChatMember::new(chat_id, user_id) + .can_manage_topics(false) + .can_pin_messages(false) + .can_invite_users(false) + .can_change_info(false) + .can_promote_members(false) + .can_restrict_members(false) + .can_manage_voice_chats(false) + .can_delete_messages(false) + .can_edit_messages(false) + .can_post_messages(false) + .can_manage_chat(false) + .is_anonymous(false), + ) + .await +} diff --git a/src/utils/telegram/senders.rs b/src/utils/telegram/senders.rs new file mode 100644 index 0000000..ce2b138 --- /dev/null +++ b/src/utils/telegram/senders.rs @@ -0,0 +1,22 @@ +use telers::{ + event::{ + telegram::HandlerResult, + EventReturn + }, + enums::ParseMode, + types::Message, + Bot +}; + +use crate::types::structs::message_sender::MessageSender; + +pub async fn send_html(bot: Bot, message: Message, info_text: &str) -> HandlerResult { + MessageSender::builder(message.chat().id()) + .text(info_text) + .parse_mode(ParseMode::HTML) + .build() + .send(&bot) + .await?; + + Ok(EventReturn::Finish) +} diff --git a/src/utils/telegram/try_do.rs b/src/utils/telegram/try_do.rs index 89d91e6..c1ebc50 100644 --- a/src/utils/telegram/try_do.rs +++ b/src/utils/telegram/try_do.rs @@ -3,7 +3,7 @@ use telers::{errors::SessionErrorKind as ErrorKind, event::EventReturn, types::C use crate::types::structs::message_sender::MessageSender; use std::future::Future; -use super::{admin_check::is_admin, demote::demote_user, get_all_admins::get_all_admins}; +use super::{admin_check::is_admin, rights_control::demote_user, data_getters::get_all_admins}; const DEMOTE_FAILURE_MESSAGE: &str = "Команда не может быть выполнена: \ не удалось удалить административные привилегии пользователя.";