Compare commits

..

6 Commits

15 changed files with 144 additions and 214 deletions

View File

@ -1,51 +0,0 @@
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

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

View File

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

View File

@ -1,85 +0,0 @@
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

@ -1,38 +0,0 @@
use telers::{
event::{telegram::HandlerResult, EventReturn},
filters::CommandObject,
types::Message,
Bot,
};
use crate::{
actions::unmute::unmute_member,
assets::files::UNMUTE_COMMAND_HELP,
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 {
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,98 @@
use std::ops::Deref;
use telers::{
event::{telegram::HandlerResult, EventReturn},
filters::CommandObject,
types::Message,
Bot,
};
use crate::{
actions::{ban::ban_member, mute::mute_member, unmute::unmute_member},
assets::files::{BAN_COMMAND_HELP, MUTE_COMMAND_HELP, UNMUTE_COMMAND_HELP},
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 admin_command_endpoint(
bot: Bot,
message: Message,
command: CommandObject,
) -> HandlerResult {
let command_type: &str = command.command.deref();
let args: Vec<&'static str> = cast_boxed(command.args);
let (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message);
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() {
let help_txt = match command_type {
"ban" => BAN_COMMAND_HELP,
"mute" => MUTE_COMMAND_HELP,
"unmute" => UNMUTE_COMMAND_HELP,
_ => "Такой команды не существует.",
};
send_html(bot, message, help_txt).await?;
return Ok(EventReturn::Cancel);
}
handler_entity
.message_sender_builder
.set_text("Нет ID или ответа на сообщение пользователя.");
match command_type {
"ban" => ban_member(handler_entity, chat_id, target_user, 0).await?,
"mute" => 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);
}
},
"unmute" => unmute_member(handler_entity, chat_id, target_user).await?,
_ => EventReturn::Finish,
};
Ok(EventReturn::Finish)
}

View File

@ -0,0 +1,21 @@
use std::ops::Deref;
use telers::{event::telegram::HandlerResult, filters::CommandObject, types::Message, Bot};
use crate::{
assets::files::{HELP_COMMAND_TEXT, PRIVACY_COMMAND_TEXT},
utils::telegram::senders::send_html,
};
#[inline]
pub async fn info_commands_endpoint(
bot: Bot,
msg: Message,
command: CommandObject,
) -> HandlerResult {
match command.command.deref() {
"help" => send_html(bot, msg, HELP_COMMAND_TEXT).await,
"privacy" => send_html(bot, msg, PRIVACY_COMMAND_TEXT).await,
_ => Ok(telers::event::EventReturn::Cancel),
}
}

View File

@ -0,0 +1,2 @@
pub mod admin_commands;
pub mod info_commands;

View File

@ -9,20 +9,15 @@ use telers::{
mod actions;
mod assets;
mod handlers;
mod middlewares;
mod types;
mod utils;
use middlewares::admin_check_middleware::AdminCheck;
mod endpoints;
use endpoints::{
commands::{
ban_command::ban,
info_commands::{help, privacy},
mute_command::mute,
unmute_command::unmute,
},
use handlers::{
commands::{admin_commands::admin_command_endpoint, info_commands::info_commands_endpoint},
dice::dice_handler::dice_handler,
};
@ -46,16 +41,6 @@ fn logs() {
);
}
macro_rules! append_command {
($branch:expr, $($command:expr), *) => {
$($branch
.message
.register($command)
.filter(Command::one(stringify!($command)));
)*
}
}
#[tokio::main]
async fn main() {
logs();
@ -80,8 +65,8 @@ async fn main() {
let mut admin_commands = Router::new("admin_commands");
let mut default_commands = Router::new("default_commands");
append_command!(default_commands, help, privacy);
append_command!(admin_commands, unmute, mute, ban);
create_handler!(default_commands, info_commands_endpoint, help, privacy);
create_handler!(admin_commands, admin_command_endpoint, mute, unmute, ban);
admin_commands
.message

View File

@ -0,0 +1,16 @@
#[macro_export]
macro_rules! create_handler {
($branch:expr, $command:expr) => {
$($branch
.message
.register($command)
.filter(Command::one(stringify!($command)));
)*
};
($branch:expr, $command:expr, $($endpoint:expr), *) => {
$branch
.message
.register($command)
.filter(Command::many([$(stringify!($endpoint)),*]));
};
}

View File

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

View File

@ -1,2 +1,3 @@
pub mod general;
pub mod macro_rules;
pub mod telegram;