Compare commits

..

8 Commits

Author SHA1 Message Date
doryan 20b1e78aa3 uhm... 2024-08-21 20:58:34 +04:00
doryan d45ebcc682 feat(imports): format imports 2024-08-21 20:58:21 +04:00
doryan f41e012f8e refactor: add append_command macro 2024-08-21 20:57:43 +04:00
doryan e333e401f3 feat(fn): all_admin_member is slice 2024-08-21 20:56:45 +04:00
doryan 352514d2b0 refactor: format code 2024-08-21 20:56:11 +04:00
doryan 46183842bd fix(on reply): prevent unmute action with REPLY 2024-08-21 20:55:28 +04:00
doryan 1bcbb3d456 refactor: rename file
dice.rs -> dice_handler.rs
2024-08-21 20:52:08 +04:00
doryan b557bf5832 feat(imports): format imports 2024-08-21 20:50:38 +04:00
9 changed files with 38 additions and 64 deletions

View File

@ -1,5 +1,7 @@
use telers::{ use telers::{
event::{telegram::HandlerResult, EventReturn}, methods::BanChatMember, Bot event::{telegram::HandlerResult, EventReturn},
methods::BanChatMember,
Bot,
}; };
use tokio::time::{sleep, Duration as DurationSleep}; use tokio::time::{sleep, Duration as DurationSleep};
@ -25,6 +27,7 @@ pub async fn ban_member(
let user_id: i64 = user.get_id().unwrap(); let user_id: i64 = user.get_id().unwrap();
let callback = || async { bot.send(BanChatMember::new(chat_id, user_id)).await }; let callback = || async { bot.send(BanChatMember::new(chat_id, user_id)).await };
let demote_args: (&Bot, i64, i64) = (&bot, user_id, chat_id); let demote_args: (&Bot, i64, i64) = (&bot, user_id, chat_id);
sender_builder.set_text("Невозможно забанить пользователя."); sender_builder.set_text("Невозможно забанить пользователя.");

View File

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

View File

@ -6,35 +6,21 @@ use telers::{
}; };
use crate::{ use crate::{
assets::files::UNMUTE_COMMAND_HELP,
actions::unmute::unmute_member, actions::unmute::unmute_member,
assets::files::UNMUTE_COMMAND_HELP,
types::{enums::target_user::TargetUser, structs::handler_entity::HandlerEntity}, types::{enums::target_user::TargetUser, structs::handler_entity::HandlerEntity},
utils::{ utils::{
general::cast_boxed_array::cast_boxed, general::cast_boxed_array::cast_boxed,
telegram::{ telegram::{args_parsers::get_user, data_getters::get_chat_data, senders::send_html},
args_parsers::get_user,
data_getters::get_chat_data,
senders::send_html
},
}, },
}; };
pub async fn unmute(bot: Bot, message: Message, command: CommandObject) -> HandlerResult { 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 (chat_id, mut handler_entity): (i64, HandlerEntity) = get_chat_data(&bot, &message);
let args: Vec<&'static str> = cast_boxed(command.args); let args: Vec<&'static str> = cast_boxed(command.args);
let target_user: TargetUser = get_user( let target_user: TargetUser = get_user(handler_entity.clone(), args.first().copied(), &mut 0);
handler_entity.clone(),
args.first().copied(),
&mut 0,
);
if args.is_empty() && !target_user.exist() { if args.is_empty() && !target_user.exist() {
send_html(bot, message, UNMUTE_COMMAND_HELP).await?; send_html(bot, message, UNMUTE_COMMAND_HELP).await?;
@ -50,4 +36,3 @@ pub async fn unmute(bot: Bot, message: Message, command: CommandObject) -> Handl
Ok(EventReturn::Finish) Ok(EventReturn::Finish)
} }

View File

@ -1 +1 @@
pub mod dice; pub mod dice_handler;

View File

@ -21,9 +21,9 @@ use endpoints::{
ban_command::ban, ban_command::ban,
info_commands::{help, privacy}, info_commands::{help, privacy},
mute_command::mute, mute_command::mute,
unmute_command::unmute unmute_command::unmute,
}, },
dice::dice::dice_handler, dice::dice_handler::dice_handler,
}; };
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -46,6 +46,16 @@ fn logs() {
); );
} }
macro_rules! append_command {
($branch:expr, $($command:expr), *) => {
$($branch
.message
.register($command)
.filter(Command::one(stringify!($command)));
)*
}
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
logs(); logs();
@ -70,30 +80,8 @@ 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");
default_commands append_command!(default_commands, help, privacy);
.message append_command!(admin_commands, unmute, mute, ban);
.register(help)
.filter(Command::one("help"));
default_commands
.message
.register(privacy)
.filter(Command::one("privacy"));
admin_commands
.message
.register(unmute)
.filter(Command::one("unmute"));
admin_commands
.message
.register(mute)
.filter(Command::one("mute"));
admin_commands
.message
.register(ban)
.filter(Command::one("ban"));
admin_commands admin_commands
.message .message
@ -115,7 +103,7 @@ async fn main() {
.unwrap(); .unwrap();
} }
Err(error) => { Err(error) => {
println!("Error text: {:?}", error); eprintln!("Error text: {:?}", error);
} }
} }
} }

View File

@ -27,6 +27,7 @@ impl TimeMetrics {
_ => Self::Days(duration), _ => Self::Days(duration),
} }
} }
pub fn extract(self) -> i64 { pub fn extract(self) -> i64 {
match self { match self {
Self::Minutes(min) => min, Self::Minutes(min) => min,
@ -36,6 +37,7 @@ impl TimeMetrics {
Self::Mounths(mon) => mon, Self::Mounths(mon) => mon,
} }
} }
pub fn get_word_declensions(self) -> (impl Into<String>, impl Into<String>, impl Into<String>) { pub fn get_word_declensions(self) -> (impl Into<String>, impl Into<String>, impl Into<String>) {
match self { match self {
Self::Minutes(_) => ("минута", "минуты", "минут"), Self::Minutes(_) => ("минута", "минуты", "минут"),

View File

@ -1,7 +1,6 @@
use telers::types::chat_member::ChatMember; use telers::types::chat_member::ChatMember;
pub fn is_admin(all_admin_members: &Vec<ChatMember>, user_id: i64) -> bool { pub fn is_admin(all_admin_members: &[ChatMember], user_id: i64) -> bool {
//fix: moderators with non full rights can use admin commands now.
all_admin_members all_admin_members
.iter() .iter()
.any(|admin: &ChatMember| match admin { .any(|admin: &ChatMember| match admin {

View File

@ -3,7 +3,7 @@ use telers::{errors::SessionErrorKind as ErrorKind, event::EventReturn, types::C
use crate::types::structs::message_sender::MessageSender; use crate::types::structs::message_sender::MessageSender;
use std::future::Future; use std::future::Future;
use super::{admin_check::is_admin, rights_control::demote_user, data_getters::get_all_admins}; use super::{admin_check::is_admin, data_getters::get_all_admins, rights_control::demote_user};
const DEMOTE_FAILURE_MESSAGE: &str = "Команда не может быть выполнена: \ const DEMOTE_FAILURE_MESSAGE: &str = "Команда не может быть выполнена: \
не удалось удалить административные привилегии пользователя."; не удалось удалить административные привилегии пользователя.";