feat(handlers): remove repeated code blocks and create one admin_command and info_command handlers
This commit is contained in:
parent
74e1394fc4
commit
c023b9b764
|
@ -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)
|
|
||||||
}
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
pub mod ban_command;
|
|
||||||
pub mod info_commands;
|
|
||||||
pub mod mute_command;
|
|
||||||
pub mod unmute_command;
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -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),
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod admin_commands;
|
||||||
|
pub mod info_commands;
|
Loading…
Reference in New Issue