refactor(bot): dice and command endpoints; feat(ban): add ban endpoint; and etc.
This commit is contained in:
parent
3fda2620e3
commit
311187b6eb
|
@ -1,61 +0,0 @@
|
||||||
use telers::{
|
|
||||||
event::{telegram::HandlerResult, EventReturn},
|
|
||||||
Bot,
|
|
||||||
};
|
|
||||||
|
|
||||||
use tokio::time::{sleep, Duration as DurationSleep};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
types::{
|
|
||||||
enums::target_user::TargetUser,
|
|
||||||
structs::handler_entity::{ExtractedEntityData, HandlerEntity},
|
|
||||||
},
|
|
||||||
utils::telegram::{ban_member::ban_chat_member, 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();
|
|
||||||
|
|
||||||
let user_id: i64 = match user.get_id() {
|
|
||||||
Some(id) => id,
|
|
||||||
None => {
|
|
||||||
sender_builder
|
|
||||||
.text("Нет ID или ответа на сообщение пользователя.")
|
|
||||||
.reply_to(message.id())
|
|
||||||
.build()
|
|
||||||
.send(&bot)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
return Ok(EventReturn::Cancel);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
sleep(DurationSleep::from_millis(delay)).await;
|
|
||||||
|
|
||||||
let callback = || async { ban_chat_member(&bot, user_id, chat_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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,3 +0,0 @@
|
||||||
pub mod ban;
|
|
||||||
pub mod mute;
|
|
||||||
pub mod unmute;
|
|
|
@ -1,58 +0,0 @@
|
||||||
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_duration::{get_duration, ExtractedDuration},
|
|
||||||
telegram::{restrict::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_duration(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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,48 +0,0 @@
|
||||||
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 = user.get_id().unwrap();
|
|
||||||
|
|
||||||
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
|
|
||||||
.unwrap();
|
|
||||||
Ok(EventReturn::Finish)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,67 +0,0 @@
|
||||||
use telers::{
|
|
||||||
event::{telegram::HandlerResult, EventReturn},
|
|
||||||
types::{Dice, Message},
|
|
||||||
Bot,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
handlers::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 handler_entity: HandlerEntity = HandlerEntity::new(bot, message, sender);
|
|
||||||
|
|
||||||
let (mute_time, emoji): (TimeMetrics, &str) = (TimeMetrics::Days(dice.value), &dice.emoji);
|
|
||||||
|
|
||||||
let target: TargetUser = TargetUser::Reply(handler_entity.message_reciever.clone());
|
|
||||||
|
|
||||||
if !target.exist() {
|
|
||||||
handler_entity
|
|
||||||
.message_sender_builder
|
|
||||||
.text("Нет ответа на сообщение пользователя.")
|
|
||||||
.build()
|
|
||||||
.send(&handler_entity.bot_instance)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
return Ok(EventReturn::Cancel);
|
|
||||||
}
|
|
||||||
|
|
||||||
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?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
handler_entity
|
|
||||||
.message_sender_builder
|
|
||||||
.text("Эмодзи не имеет привязанных действий.")
|
|
||||||
.build()
|
|
||||||
.send(&handler_entity.bot_instance)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(EventReturn::Finish)
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
pub mod dice;
|
|
|
@ -1,3 +0,0 @@
|
||||||
pub mod actions;
|
|
||||||
pub mod command_handlers;
|
|
||||||
pub mod dice_handler;
|
|
28
src/main.rs
28
src/main.rs
|
@ -4,10 +4,10 @@ use telers::{
|
||||||
enums::ContentType,
|
enums::ContentType,
|
||||||
event::ToServiceProvider,
|
event::ToServiceProvider,
|
||||||
filters::{content_type::ContentType as CT, Command},
|
filters::{content_type::ContentType as CT, Command},
|
||||||
types::Message,
|
|
||||||
Bot, Dispatcher, Router,
|
Bot, Dispatcher, Router,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod actions;
|
||||||
mod assets;
|
mod assets;
|
||||||
mod middlewares;
|
mod middlewares;
|
||||||
mod types;
|
mod types;
|
||||||
|
@ -15,14 +15,15 @@ mod utils;
|
||||||
|
|
||||||
use middlewares::admin_check_middleware::AdminCheck;
|
use middlewares::admin_check_middleware::AdminCheck;
|
||||||
|
|
||||||
use assets::files::{HELP_COMMAND_TEXT, PRIVACY_COMMAND_TEXT};
|
mod endpoints;
|
||||||
|
use endpoints::{
|
||||||
mod handlers;
|
commands::{
|
||||||
use handlers::{
|
ban_command::ban,
|
||||||
command_handlers::{
|
info_commands::{help, privacy},
|
||||||
info_commands_template::send_info, mute_command::mute, unmute_command::unmute,
|
mute_command::mute,
|
||||||
|
unmute_command::unmute
|
||||||
},
|
},
|
||||||
dice_handler::dice::dice_handler,
|
dice::dice::dice_handler,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
@ -69,12 +70,6 @@ async fn main() {
|
||||||
let mut admin_commands = Router::new("admin_commands");
|
let mut admin_commands = Router::new("admin_commands");
|
||||||
let mut default_commands = Router::new("default_commands");
|
let mut default_commands = Router::new("default_commands");
|
||||||
|
|
||||||
let help =
|
|
||||||
|bot: Bot, msg: Message| async { send_info(bot, msg, HELP_COMMAND_TEXT).await };
|
|
||||||
|
|
||||||
let privacy =
|
|
||||||
|bot: Bot, msg: Message| async { send_info(bot, msg, PRIVACY_COMMAND_TEXT).await };
|
|
||||||
|
|
||||||
default_commands
|
default_commands
|
||||||
.message
|
.message
|
||||||
.register(help)
|
.register(help)
|
||||||
|
@ -95,6 +90,11 @@ async fn main() {
|
||||||
.register(mute)
|
.register(mute)
|
||||||
.filter(Command::one("mute"));
|
.filter(Command::one("mute"));
|
||||||
|
|
||||||
|
admin_commands
|
||||||
|
.message
|
||||||
|
.register(ban)
|
||||||
|
.filter(Command::one("ban"));
|
||||||
|
|
||||||
admin_commands
|
admin_commands
|
||||||
.message
|
.message
|
||||||
.inner_middlewares
|
.inner_middlewares
|
||||||
|
|
|
@ -9,7 +9,7 @@ use anyhow::Error as Reject;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
types::structs::message_sender::MessageSender,
|
types::structs::message_sender::MessageSender,
|
||||||
utils::telegram::{admin_check::is_admin, get_all_admins::get_all_admins},
|
utils::telegram::{admin_check::is_admin, data_getters::get_all_admins},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
Loading…
Reference in New Issue