refactor(bot actions): move bot actions from src/handlers to src/

This commit is contained in:
doryan 2024-07-09 23:00:01 +04:00
parent ec2ff44e36
commit 7576859907
4 changed files with 170 additions and 0 deletions

49
src/actions/ban.rs Normal file
View File

@ -0,0 +1,49 @@
use telers::{
event::{telegram::HandlerResult, EventReturn}, methods::BanChatMember, Bot
};
use tokio::time::{sleep, Duration as DurationSleep};
use crate::{
types::{
enums::target_user::TargetUser,
structs::handler_entity::{ExtractedEntityData, HandlerEntity},
},
utils::telegram::try_do::try_restrict,
};
pub async fn ban_member(
handler_entity: HandlerEntity,
chat_id: i64,
user: TargetUser,
delay: u64,
) -> HandlerResult {
let (bot, message, mut sender_builder): ExtractedEntityData = handler_entity.extract();
sleep(DurationSleep::from_millis(delay)).await;
let user_id: i64 = user.get_id().unwrap();
let callback = || async { bot.send(BanChatMember::new(chat_id, user_id)).await };
let demote_args: (&Bot, i64, i64) = (&bot, user_id, chat_id);
sender_builder.set_text("Невозможно забанить пользователя.");
if try_restrict(callback, demote_args, sender_builder.clone().build())
.await
.is_err()
{
Ok(EventReturn::Cancel)
} else {
let banned_user_name: String = user.get_user_name(&bot, &message).await.unwrap();
sender_builder
.reply_to(message.id())
.text(format!("Пользователь {banned_user_name} забанен."))
.build()
.send(&bot)
.await?;
Ok(EventReturn::Finish)
}
}

3
src/actions/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod ban;
pub mod mute;
pub mod unmute;

58
src/actions/mute.rs Normal file
View File

@ -0,0 +1,58 @@
use telers::{
event::{telegram::HandlerResult, EventReturn},
Bot,
};
use tokio::time::{sleep, Duration};
use crate::{
types::{
enums::target_user::TargetUser,
structs::handler_entity::{ExtractedEntityData, HandlerEntity},
TimeValues,
},
utils::{
general::get_expiration_time::{get_expiration_time, ExtractedDuration},
telegram::{rights_control::restrict, try_do::try_restrict},
},
};
pub async fn mute_member(
handler_entity: HandlerEntity,
chat_id: i64,
user: TargetUser,
time: TimeValues,
) -> HandlerResult {
let (bot, message, mut sender_builder): ExtractedEntityData = handler_entity.extract();
let user_id: i64 = user.get_id().unwrap();
sleep(Duration::from_millis(time.1)).await;
let (unmute_date, postfix, mute_duration): ExtractedDuration = get_expiration_time(time);
let demote_args: (&Bot, i64, i64) = (&bot, user_id, chat_id);
let callback = || async { restrict(&bot, user_id, unmute_date, chat_id).await };
sender_builder.set_text("Невозможно выдать мут.");
if try_restrict(callback, demote_args, sender_builder.clone().build())
.await
.is_err()
{
Ok(EventReturn::Cancel)
} else {
let muted_user_name: String = user.get_user_name(&bot, &message).await.unwrap();
sender_builder
.reply_to(message.id())
.text(format!(
"Пользователю {muted_user_name} выдан мут на {mute_duration} {postfix}."
))
.build()
.send(&bot)
.await?;
Ok(EventReturn::Finish)
}
}

60
src/actions/unmute.rs Normal file
View File

@ -0,0 +1,60 @@
use telers::{
event::{telegram::HandlerResult, EventReturn},
methods::RestrictChatMember,
types::ChatPermissions,
};
use crate::types::{
enums::target_user::TargetUser,
structs::handler_entity::{ExtractedEntityData, HandlerEntity},
};
pub async fn unmute_member(
handler_entity: HandlerEntity,
chat_id: i64,
user: TargetUser,
) -> HandlerResult {
let (bot, message, sender_builder): ExtractedEntityData = handler_entity.extract();
let user_id: i64;
if let Some(id) = user.get_id() {
user_id = id;
} else {
sender_builder
.build()
.send(&bot)
.await?;
return Ok(EventReturn::Cancel);
}
let default_member_permissions = ChatPermissions::all()
.can_change_info(false)
.can_manage_topics(false)
.can_invite_users(false)
.can_pin_messages(false);
let bot_action = RestrictChatMember::new(chat_id, user_id, default_member_permissions);
if let Err(error) = bot.send(bot_action).await {
sender_builder
.text(format!("Невозможно снять мут с пользователя: {error:?}."))
.build()
.send(&bot)
.await?;
Ok(EventReturn::Cancel)
} else {
let muted_user_name: String = user.get_user_name(&bot, &message).await.unwrap();
sender_builder
.reply_to(message.id())
.text(format!("С пользователя {muted_user_name} снят мут."))
.build()
.send(&bot)
.await?;
Ok(EventReturn::Finish)
}
}