2025-04-11 11:10:21 +03:00
use std ::{ future ::Future , ops ::Deref , pin ::Pin } ;
2024-10-08 22:37:22 +03:00
use telers ::{
2025-04-11 11:10:21 +03:00
errors ::HandlerError ,
event ::{ service ::BoxFuture , telegram ::HandlerResult , EventReturn } ,
filters ::{ chat_type , CommandObject } ,
methods ::{ BanChatMember , RestrictChatMember , SendMessage , UnbanChatMember } ,
types ::{ ChatPermissions , Message } ,
2024-10-08 22:37:22 +03:00
Bot ,
} ;
use crate ::{
2025-04-09 23:46:50 +03:00
assets ::files ::{ BAN_COMMAND_HELP , MUTE_COMMAND_HELP , UNBAN_COMMAND_HELP , UNMUTE_COMMAND_HELP } ,
2025-04-11 11:10:21 +03:00
types ::enums ::{ target_user ::TargetUser , time_metrics ::TimeDuration } ,
utils ::{
general ::get_expiration_time ::get_expiration_date ,
telegram ::{
args_parser ::{ parse_args , Argument } ,
member_rights ::{ demote_user , restrict } ,
senders ::send_html ,
} ,
} ,
2024-10-08 22:37:22 +03:00
} ;
2025-04-09 23:46:50 +03:00
const USER_ID_NOT_FOUND : & str = " Для выполнение команды нужно знать ID пользователя, или ввести команду и выполнить её, ответив на сообщение пользователя " ;
2025-04-11 11:10:21 +03:00
const DEMOTE_ERROR : & str = " Невозможно снять привелегий администратора в силу того, что права были выданы одним из администраторов или основателем " ;
2025-04-09 23:46:50 +03:00
pub async fn admin_commands_endpoint (
2024-10-08 22:37:22 +03:00
bot : Bot ,
message : Message ,
2025-04-09 23:46:50 +03:00
command_object : CommandObject ,
2024-10-08 22:37:22 +03:00
) -> HandlerResult {
2025-04-09 23:46:50 +03:00
let ( command_type , args ) = ( command_object . command . deref ( ) , command_object . args . deref ( ) ) ;
2024-10-08 22:37:22 +03:00
2025-04-11 11:10:21 +03:00
if let Some ( args ) = parse_args ( args , & message , command_type ) {
2025-04-09 23:46:50 +03:00
match command_type {
2025-04-11 11:10:21 +03:00
" ban " = > {
let ban_callback = async | chat_id : i64 , user_id : i64 | {
bot . send ( BanChatMember ::new ( chat_id , user_id ) ) . await ? ;
Ok ( EventReturn ::Finish )
} ;
execute_command ( & bot , & message , ban_callback , args ) . await ? ;
}
" unban " = > {
let unban_callback = async | chat_id : i64 , user_id : i64 | {
bot . send ( UnbanChatMember ::new ( chat_id , user_id ) ) . await ? ;
Ok ( EventReturn ::Finish )
} ;
execute_command ( & bot , & message , unban_callback , args ) . await ? ;
}
" mute " = > {
let duration = if let Some ( Argument ::Time ( duration ) ) = args . get ( 1 ) {
get_expiration_date ( * duration )
} else {
bot . send ( SendMessage ::new (
message . chat ( ) . id ( ) ,
" Н е указана длительность мута." ,
) )
. await ? ;
return Err ( HandlerError ::from_display ( " Mute duration is not defined " ) ) ;
} ;
let mute_callback = async | chat_id : i64 , user_id : i64 | {
restrict ( & bot , user_id , duration , chat_id ) . await ? ;
Ok ( EventReturn ::Finish )
} ;
execute_command ( & bot , & message , mute_callback , args ) . await ? ;
}
" unmute " = > {
let unmute_callback = async | chat_id : i64 , user_id : i64 | {
bot . send ( RestrictChatMember ::new (
chat_id ,
user_id ,
ChatPermissions ::new ( ) ,
) )
. await ? ;
Ok ( EventReturn ::Finish )
} ;
execute_command ( & bot , & message , unmute_callback , args ) . await ? ;
}
2025-04-09 23:46:50 +03:00
_ = > unreachable! ( ) ,
} ;
} else {
2024-10-08 22:37:22 +03:00
let help_txt = match command_type {
" ban " = > BAN_COMMAND_HELP ,
" mute " = > MUTE_COMMAND_HELP ,
2025-04-09 23:46:50 +03:00
" unban " = > UNBAN_COMMAND_HELP ,
2024-10-08 22:37:22 +03:00
" unmute " = > UNMUTE_COMMAND_HELP ,
2025-04-09 23:46:50 +03:00
_ = > unreachable! ( ) ,
2024-10-08 22:37:22 +03:00
} ;
2025-04-11 11:10:21 +03:00
send_html ( & bot , & message , help_txt ) . await ? ;
2024-10-08 22:37:22 +03:00
}
Ok ( EventReturn ::Finish )
}
2025-04-11 11:10:21 +03:00
pub async fn execute_command (
bot : & Bot ,
message : & Message ,
command : impl AsyncFnOnce ( i64 , i64 ) -> HandlerResult + Copy ,
command_args : Vec < Argument > ,
) -> HandlerResult {
let chat_id = message . chat ( ) . id ( ) ;
if let Some ( Argument ::User ( user ) ) = command_args . first ( ) {
if let TargetUser ::None = user {
bot . send ( SendMessage ::new ( chat_id , USER_ID_NOT_FOUND ) )
. await ? ;
return Err ( HandlerError ::from_display ( " Can't find user ID " ) ) ;
}
let ( user_name , user_id ) = (
user . get_user_name ( bot , message ) . await . unwrap ( ) ,
user . get_id ( ) . unwrap ( ) ,
) ;
if command ( user_id , chat_id ) . await . is_err ( ) {
if demote_user ( bot , user_id , chat_id ) . await . is_err ( ) {
bot . send ( SendMessage ::new ( chat_id , DEMOTE_ERROR ) ) . await ? ;
return Err ( HandlerError ::from_display ( " Can't demote chat member because he has permission that he got from another admin/chat owner " ) ) ;
}
command ( user_id , chat_id ) . await ? ;
} else {
bot . send ( SendMessage ::new ( chat_id , " Command executed successful! " ) )
. await ? ;
}
Ok ( EventReturn ::Finish )
} else {
unreachable! ( )
}
}