feat(attributes): add #[allow(dead_code)] for target user and #[derive(Clone)] for handler_entity

This commit is contained in:
doryan 2024-07-09 23:03:49 +04:00
parent cca1b86772
commit 3fda2620e3
6 changed files with 3 additions and 181 deletions

View File

@ -1,19 +0,0 @@
use telers::{
enums::parse_mode::ParseMode,
event::{telegram::HandlerResult, EventReturn},
types::Message,
Bot,
};
use crate::types::structs::message_sender::MessageSender;
pub async fn send_info(bot: Bot, message: Message, info_text: &'static str) -> HandlerResult {
MessageSender::builder(message.chat().id())
.text(info_text)
.parse_mode(ParseMode::HTML)
.build()
.send(&bot)
.await
.unwrap();
Ok(EventReturn::Finish)
}

View File

@ -1,3 +0,0 @@
pub mod info_commands_template;
pub mod mute_command;
pub mod unmute_command;

View File

@ -1,97 +0,0 @@
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)
}

View File

@ -1,62 +0,0 @@
use telers::{
enums::ParseMode,
event::{telegram::HandlerResult, EventReturn},
filters::CommandObject,
types::Message,
Bot,
};
use crate::{
assets::files::UNMUTE_COMMAND_HELP,
handlers::actions::unmute::unmute_member,
types::{
enums::target_user::TargetUser,
structs::{handler_entity::HandlerEntity, message_sender::MessageSender},
},
utils::general::parse_boxed_array::parse_boxed,
};
pub async fn unmute(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);
match args.first().cloned() {
Some(raw_id) => {
handler_entity
.message_sender_builder
.set_text("Нет ID или ответа на сообщение пользователя.");
if let Ok(parsed_id) = raw_id.parse::<i64>() {
let on_id: TargetUser = TargetUser::Id(parsed_id);
unmute_member(handler_entity, chat_id, on_id).await?;
} else {
handler_entity
.message_sender_builder
.build()
.send(&handler_entity.bot_instance)
.await
.unwrap();
}
}
None => {
if handler_entity.message_reciever.reply_to_message().is_none() {
handler_entity
.message_sender_builder
.text(UNMUTE_COMMAND_HELP)
.parse_mode(ParseMode::HTML)
.build()
.send(&handler_entity.bot_instance)
.await
.unwrap();
} else {
let on_reply = TargetUser::Reply(handler_entity.message_reciever.clone());
unmute_member(handler_entity, chat_id, on_reply).await?;
}
}
}
Ok(EventReturn::Finish)
}

View File

@ -15,6 +15,7 @@ pub enum TargetUser {
} }
impl TargetUser { impl TargetUser {
#[allow(dead_code)]
pub fn exist(&self) -> bool { pub fn exist(&self) -> bool {
match self { match self {
Self::Id(_id) => true, Self::Id(_id) => true,
@ -44,6 +45,7 @@ impl TargetUser {
Self::None => None, Self::None => None,
} }
} }
pub async fn get_user_name(&self, bot: &Bot, msg: &Message) -> Option<String> { pub async fn get_user_name(&self, bot: &Bot, msg: &Message) -> Option<String> {
match self { match self {
Self::Id(id) => { Self::Id(id) => {

View File

@ -3,6 +3,7 @@ use telers::{types::Message, Bot};
pub type ExtractedEntityData = (Bot, Message, MessageSenderBuilder); pub type ExtractedEntityData = (Bot, Message, MessageSenderBuilder);
#[derive(Clone)]
pub struct HandlerEntity { pub struct HandlerEntity {
pub bot_instance: Bot, pub bot_instance: Bot,
pub message_reciever: Message, pub message_reciever: Message,