refactor(folder name): rename from 'handlers' to 'endpoints'

This commit is contained in:
doryan 2024-07-09 23:01:33 +04:00
parent 7576859907
commit cca1b86772
8 changed files with 268 additions and 0 deletions

View File

@ -0,0 +1,51 @@
use telers::{
event::{
EventReturn,
telegram::HandlerResult
},
filters::CommandObject,
types::Message,
Bot
};
use crate::{
assets::files::BAN_COMMAND_HELP, actions::ban::ban_member,
types::{
enums::target_user::TargetUser,
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 ban(bot: Bot, message: Message, command: CommandObject) -> HandlerResult {
let args: Vec<&'static str> = cast_boxed(command.args);
let (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message);
let target_user: TargetUser = get_user(
handler_entity.clone(),
args.first().copied(),
&mut 0,
);
if args.is_empty() && !target_user.exist(){
send_html(bot, message, BAN_COMMAND_HELP).await?;
return Ok(EventReturn::Cancel);
}
handler_entity
.message_sender_builder
.set_text("Нет ID или ответа на сообщение пользователя.");
ban_member(handler_entity, chat_id, target_user, 0).await?;
Ok(EventReturn::Finish)
}

View File

@ -0,0 +1,19 @@
use telers::{
event::telegram::HandlerResult,
types::Message,
Bot,
};
use crate::{
utils::telegram::senders::send_html,
assets::files::{HELP_COMMAND_TEXT, PRIVACY_COMMAND_TEXT}
};
pub async fn help(bot: Bot, msg: Message) -> HandlerResult {
send_html(bot, msg, HELP_COMMAND_TEXT).await
}
pub async fn privacy(bot: Bot, msg: Message) -> HandlerResult {
send_html(bot, msg, PRIVACY_COMMAND_TEXT).await
}

View File

@ -0,0 +1,4 @@
pub mod ban_command;
pub mod info_commands;
pub mod mute_command;
pub mod unmute_command;

View File

@ -0,0 +1,85 @@
use telers::{
event::{telegram::HandlerResult, EventReturn},
filters::CommandObject,
types::Message,
Bot,
};
use crate::{
assets::files::MUTE_COMMAND_HELP,
actions::mute::mute_member,
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 mute(bot: Bot, message: Message, command: CommandObject) -> HandlerResult {
let (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message);
let args: Vec<&'static str> = cast_boxed(command.args);
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(){
send_html(bot, message, MUTE_COMMAND_HELP).await?;
return Ok(EventReturn::Cancel);
}
handler_entity
.message_sender_builder
.set_text("Нет ID или ответа на сообщение пользователя.");
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);
}
}
Ok(EventReturn::Finish)
}

View File

@ -0,0 +1,53 @@
use telers::{
event::{telegram::HandlerResult, EventReturn},
filters::CommandObject,
types::Message,
Bot,
};
use crate::{
assets::files::UNMUTE_COMMAND_HELP,
actions::unmute::unmute_member,
types::{enums::target_user::TargetUser, 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 unmute(bot: Bot, message: Message, command: CommandObject) -> HandlerResult {
if command.args.len() == 0 {
send_html(bot, message, UNMUTE_COMMAND_HELP).await?;
return Ok(EventReturn::Cancel);
}
let (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message);
let args: Vec<&'static str> = cast_boxed(command.args);
let target_user: TargetUser = get_user(
handler_entity.clone(),
args.first().copied(),
&mut 0,
);
if args.is_empty() && !target_user.exist(){
send_html(bot, message, UNMUTE_COMMAND_HELP).await?;
return Ok(EventReturn::Cancel);
}
handler_entity
.message_sender_builder
.set_text("Нет ID или ответа на сообщение пользователя.");
unmute_member(handler_entity, chat_id, target_user).await?;
Ok(EventReturn::Finish)
}

View File

@ -0,0 +1,53 @@
use telers::{
event::{telegram::HandlerResult, EventReturn},
types::{Dice, Message},
Bot,
};
use crate::{
actions::{ban::ban_member, mute::mute_member},
types::{
enums::{target_user::TargetUser, time_metrics::TimeMetrics},
structs::{handler_entity::HandlerEntity, message_sender::MessageSender},
},
};
const DICE_DELAY_MS: u64 = 4000u64;
const CASINO_DELAY_MS: u64 = 1500u64;
pub async fn dice_handler(bot: Bot, message: Message) -> HandlerResult {
let (chat_id, dice): (i64, Dice) = (message.chat().id(), message.dice().unwrap().clone());
let sender = MessageSender::builder(chat_id);
let mut handler_entity = HandlerEntity::new(bot, message, sender);
let (mute_time, emoji): (TimeMetrics, &str) = (TimeMetrics::Days(dice.value), &dice.emoji);
let target = TargetUser::Reply(handler_entity.message_reciever.clone());
handler_entity
.message_sender_builder
.set_text("Нет ответа на сообщение пользователя.");
match emoji {
"🎲" => {
mute_member(handler_entity, chat_id, target, (mute_time, DICE_DELAY_MS)).await?;
}
"🎰" => {
if dice.value == 64 {
ban_member(handler_entity, chat_id, target, CASINO_DELAY_MS).await?;
} else {
mute_member(
handler_entity,
chat_id,
target,
(mute_time, CASINO_DELAY_MS),
)
.await?;
}
}
_ => ()
}
Ok(EventReturn::Finish)
}

View File

@ -0,0 +1 @@
pub mod dice;

2
src/endpoints/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod commands;
pub mod dice;