99 lines
3.1 KiB
Rust
99 lines
3.1 KiB
Rust
|
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::<i64>() {
|
|||
|
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)
|
|||
|
}
|