gluon/src/handlers/actions/mute.rs

71 lines
2.1 KiB
Rust
Raw Normal View History

2024-05-30 23:37:47 +03:00
use telers::{
event::{telegram::HandlerResult, EventReturn},
Bot,
};
2024-06-02 01:30:52 +03:00
use tokio::time::{sleep, Duration};
2024-05-30 23:37:47 +03:00
use crate::{
types::{
2024-06-02 02:34:56 +03:00
enums::target_user::TargetUser,
structs::handler_entity::{ExtractedEntityData, HandlerEntity},
TimeValues,
2024-05-30 23:37:47 +03:00
},
2024-06-02 01:30:52 +03:00
utils::{
2024-06-02 02:34:56 +03:00
general::get_duration::{get_duration, ExtractedDuration},
2024-06-02 01:30:52 +03:00
telegram::{restrict::restrict, try_do::try_restrict},
},
2024-05-30 23:37:47 +03:00
};
2024-06-02 01:30:52 +03:00
pub async fn mute(
handler_entity: HandlerEntity,
chat_id: i64,
2024-06-02 02:34:56 +03:00
user: TargetUser,
time: TimeValues,
2024-06-02 01:30:52 +03:00
) -> HandlerResult {
2024-06-02 02:34:56 +03:00
let (bot, message, mut sender_builder): ExtractedEntityData = handler_entity.extract();
2024-06-02 01:30:52 +03:00
2024-06-02 02:34:56 +03:00
let user_id: i64 = match user.get_id() {
Some(id) => id,
None => {
sender_builder
.text("Ответьте на сообщение, чтобы замьютить участника чата.")
.reply_to(message.id())
.build()
.send(&bot)
.await
.unwrap();
return Ok(EventReturn::Cancel);
}
};
2024-05-30 23:37:47 +03:00
2024-06-02 02:34:56 +03:00
sleep(Duration::from_millis(time.1)).await;
2024-06-02 01:30:52 +03:00
2024-06-02 02:34:56 +03:00
let (unmute_date, postfix, mute_duration): ExtractedDuration = get_duration(time);
let demote_args: (&Bot, i64, i64) = (&bot, user_id, chat_id);
let callback = || async { restrict(&bot, user_id, unmute_date, chat_id).await };
2024-05-30 23:37:47 +03:00
2024-06-02 02:34:56 +03:00
sender_builder.set_text("Невозможно замьютить участника чата, демотните и попробуйте снова");
2024-05-30 23:37:47 +03:00
2024-06-02 02:34:56 +03:00
if try_restrict(callback, demote_args, sender_builder.clone().build())
2024-05-30 23:37:47 +03:00
.await
.is_err()
{
Ok(EventReturn::Cancel)
} else {
2024-06-02 02:34:56 +03:00
let muted_user_name = message.from().unwrap().clone().username.unwrap();
2024-05-30 23:37:47 +03:00
2024-06-02 01:30:52 +03:00
sender_builder
2024-05-30 23:37:47 +03:00
.reply_to(message.id())
.text(format!(
"Пользователь {} замьючен на {:?} {}.",
2024-06-02 02:34:56 +03:00
muted_user_name, mute_duration, postfix
2024-05-30 23:37:47 +03:00
))
2024-06-02 01:30:52 +03:00
.build()
2024-05-30 23:37:47 +03:00
.send(&bot)
.await?;
Ok(EventReturn::Finish)
}
}