98 lines
3.1 KiB
Rust
98 lines
3.1 KiB
Rust
use telers::{
|
||
enums::ParseMode,
|
||
event::{telegram::HandlerResult, EventReturn},
|
||
filters::CommandObject,
|
||
types::Message,
|
||
Bot,
|
||
};
|
||
|
||
use crate::{
|
||
assets::files::MUTE_COMMAND_HELP,
|
||
handlers::actions::mute::mute_member,
|
||
types::{
|
||
enums::{target_user::TargetUser, time_metrics::TimeMetrics},
|
||
structs::{handler_entity::HandlerEntity, message_sender::MessageSender},
|
||
},
|
||
utils::general::parse_boxed_array::parse_boxed,
|
||
};
|
||
|
||
pub async fn mute(bot: Bot, message: Message, command: CommandObject) -> HandlerResult {
|
||
let args: Vec<&'static str> = parse_boxed(command.args);
|
||
|
||
let (message_id, chat_id): (i64, i64) = (message.id(), message.chat().id());
|
||
let sender = MessageSender::builder(chat_id).reply_to(message_id);
|
||
let mut handler_entity: HandlerEntity = HandlerEntity::new(bot, message, sender);
|
||
|
||
let mut duration_argument_position: usize = 0;
|
||
|
||
let target_user: TargetUser = match (
|
||
handler_entity.message_reciever.reply_to_message(),
|
||
args.first(),
|
||
) {
|
||
(Some(msg), _) => TargetUser::CurrentMessage(msg.clone()),
|
||
(None, Some(raw_id)) => {
|
||
duration_argument_position += 1;
|
||
if let Ok(id) = raw_id.parse::<i64>() {
|
||
TargetUser::Id(id)
|
||
} else {
|
||
handler_entity
|
||
.message_sender_builder
|
||
.text("Нет ID или ответа на сообщение пользователя.")
|
||
.build()
|
||
.send(&handler_entity.bot_instance)
|
||
.await?;
|
||
|
||
return Ok(EventReturn::Cancel);
|
||
}
|
||
}
|
||
(None, None) => {
|
||
handler_entity
|
||
.message_sender_builder
|
||
.text(MUTE_COMMAND_HELP)
|
||
.parse_mode(ParseMode::HTML)
|
||
.build()
|
||
.send(&handler_entity.bot_instance)
|
||
.await?;
|
||
|
||
return Ok(EventReturn::Cancel);
|
||
}
|
||
};
|
||
|
||
handler_entity
|
||
.message_sender_builder
|
||
.set_text("Не указана длительность мута.");
|
||
|
||
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
|
||
.build()
|
||
.send(&handler_entity.bot_instance)
|
||
.await?;
|
||
|
||
return Ok(EventReturn::Cancel);
|
||
}
|
||
}
|
||
|
||
Ok(EventReturn::Finish)
|
||
}
|