132 lines
5.1 KiB
Rust
132 lines
5.1 KiB
Rust
use std::{future::Future, ops::Deref, pin::Pin};
|
||
|
||
use telers::{
|
||
errors::HandlerError,
|
||
event::{service::BoxFuture, telegram::HandlerResult, EventReturn},
|
||
filters::{chat_type, CommandObject},
|
||
methods::{BanChatMember, RestrictChatMember, SendMessage, UnbanChatMember},
|
||
types::{ChatPermissions, Message},
|
||
Bot,
|
||
};
|
||
|
||
use crate::{
|
||
assets::files::{BAN_COMMAND_HELP, MUTE_COMMAND_HELP, UNBAN_COMMAND_HELP, UNMUTE_COMMAND_HELP},
|
||
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,
|
||
},
|
||
},
|
||
};
|
||
|
||
const USER_ID_NOT_FOUND: &str = "Для выполнение команды нужно знать ID пользователя, или ввести команду и выполнить её, ответив на сообщение пользователя";
|
||
const DEMOTE_ERROR: &str = "Невозможно снять привелегий администратора в силу того, что права были выданы одним из администраторов или основателем";
|
||
|
||
pub async fn admin_commands_endpoint(
|
||
bot: Bot,
|
||
message: Message,
|
||
command_object: CommandObject,
|
||
) -> HandlerResult {
|
||
let (command_type, args) = (command_object.command.deref(), command_object.args.deref());
|
||
|
||
if let Some(args) = parse_args(args, &message, command_type) {
|
||
match command_type {
|
||
"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?;
|
||
}
|
||
_ => unreachable!(),
|
||
};
|
||
} else {
|
||
let help_txt = match command_type {
|
||
"ban" => BAN_COMMAND_HELP,
|
||
"mute" => MUTE_COMMAND_HELP,
|
||
"unban" => UNBAN_COMMAND_HELP,
|
||
"unmute" => UNMUTE_COMMAND_HELP,
|
||
_ => unreachable!(),
|
||
};
|
||
|
||
send_html(&bot, &message, help_txt).await?;
|
||
}
|
||
Ok(EventReturn::Finish)
|
||
}
|
||
|
||
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!()
|
||
}
|
||
}
|