start the refactor
This commit is contained in:
158
src/lib.rs
Normal file
158
src/lib.rs
Normal file
@@ -0,0 +1,158 @@
|
||||
use chrono::Local;
|
||||
use std::sync::Arc;
|
||||
use teloxide::{prelude::*, utils::command::BotCommands};
|
||||
pub mod ban_stiker;
|
||||
pub mod check_permissions;
|
||||
mod database;
|
||||
mod filter_files;
|
||||
mod pole_dialogue;
|
||||
mod rewrite_links;
|
||||
mod spoiler_mangas;
|
||||
mod telegram_utils;
|
||||
|
||||
#[derive(BotCommands, Clone)]
|
||||
#[command(
|
||||
rename_rule = "lowercase",
|
||||
description = "These commands are supported:"
|
||||
)]
|
||||
enum Command {
|
||||
#[command(description = "ban a stiker")]
|
||||
Torquemada,
|
||||
#[command(description = "display this text.")]
|
||||
Help,
|
||||
#[command(description = "list pole points")]
|
||||
Top,
|
||||
#[command(description = "get the server time")]
|
||||
Time,
|
||||
}
|
||||
|
||||
/*#[tokio::main]
|
||||
async fn main() {
|
||||
run().await;
|
||||
}*/
|
||||
|
||||
pub async fn run() {
|
||||
//teloxide::enable_logging!();
|
||||
pretty_env_logger::init();
|
||||
log::info!("Starting bot");
|
||||
let bot = Bot::from_env();
|
||||
let permissions = Arc::new(check_permissions::GroupPermissions::new());
|
||||
let p1 = Arc::clone(&permissions);
|
||||
let p2 = Arc::clone(&permissions);
|
||||
//Command::repl(bot.clone(), answer).await;
|
||||
let handler = Update::filter_message()
|
||||
.branch(
|
||||
dptree::filter(move |msg: Message| !p1.compare(&msg.chat.id.to_string())).endpoint(
|
||||
|msg: Message, bot: Bot| async move {
|
||||
if !msg.chat.is_private() {
|
||||
println!("{}", msg.chat.id.0);
|
||||
bot.leave_chat(msg.chat.id).await?;
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
),
|
||||
)
|
||||
.branch(
|
||||
dptree::filter(|msg: Message| is_channel_user(msg)).endpoint(
|
||||
|msg: Message, bot: Bot| async move {
|
||||
bot.delete_message(msg.chat.id, msg.id).await?;
|
||||
Ok(())
|
||||
},
|
||||
),
|
||||
)
|
||||
.branch(
|
||||
Update::filter_message()
|
||||
.filter_command::<Command>()
|
||||
.endpoint(|msg: Message, bot: Bot, command: Command| answer(bot, msg, command)),
|
||||
)
|
||||
.branch(
|
||||
dptree::filter(|msg: Message| is_media(msg.clone()) && ban_stiker::check_media(msg))
|
||||
.endpoint(|msg: Message, bot: Bot| async move {
|
||||
bot.delete_message(msg.chat.id, msg.id).await?;
|
||||
Ok(())
|
||||
}),
|
||||
)
|
||||
/*
|
||||
Now is useless because the group doesn't make any spoiler
|
||||
.branch(
|
||||
dptree::filter(move |msg: Message| {
|
||||
(is_photo(msg.clone()) && p2.compar_party(&msg.chat.id.to_string()))
|
||||
})
|
||||
.endpoint(|msg: Message, bot: Bot| spoiler_mangas::check_image(msg, bot)),
|
||||
)*/
|
||||
.branch(
|
||||
dptree::filter(move |msg: Message| filter_files::analyce_name::check_file(msg.clone()))
|
||||
.endpoint(|msg: Message, bot: Bot| filter_files::action::take_actions(msg, bot)),
|
||||
)
|
||||
.branch(
|
||||
dptree::filter(move |msg: Message| {
|
||||
rewrite_links::check_contain_links::contain_links(msg.clone())
|
||||
})
|
||||
.endpoint(|msg: Message, bot: Bot| {
|
||||
rewrite_links::check_contain_links::fix_links(msg, bot)
|
||||
}),
|
||||
)
|
||||
.branch(
|
||||
dptree::filter(move |msg: Message| permissions.compar_party(&msg.chat.id.to_string()))
|
||||
.endpoint(|msg: Message, bot: Bot| pole_dialogue::exe_pole(msg, bot)),
|
||||
);
|
||||
Dispatcher::builder(bot, handler).build().dispatch().await;
|
||||
}
|
||||
|
||||
async fn answer(bot: Bot, msg: Message, command: Command) -> anyhow::Result<()> {
|
||||
match command {
|
||||
Command::Torquemada => match ban_stiker::ban_media(msg.clone(), bot.clone()).await {
|
||||
Ok(_o) => {
|
||||
bot.send_message(msg.chat.id, "Otro fichero que se va a la hoguera")
|
||||
.await?
|
||||
}
|
||||
Err(e) => bot.send_message(msg.chat.id, e.to_string()).await?,
|
||||
},
|
||||
Command::Help => {
|
||||
bot.send_message(msg.chat.id, Command::descriptions().to_string())
|
||||
.await?
|
||||
}
|
||||
Command::Top => pole_dialogue::get_top(msg, bot).await?,
|
||||
Command::Time => {
|
||||
bot.send_message(
|
||||
msg.chat.id,
|
||||
Local::now().format("%Y-%m-%d:%H:%M:%S").to_string(),
|
||||
)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_media(msg: Message) -> bool {
|
||||
is_stiker(msg.clone()) || is_gif(msg.clone()) || is_photo(msg.clone())
|
||||
}
|
||||
|
||||
fn is_stiker(msg: Message) -> bool {
|
||||
match msg.sticker() {
|
||||
Some(s) => true,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_gif(msg: Message) -> bool {
|
||||
match msg.animation() {
|
||||
Some(s) => true,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_photo(msg: Message) -> bool {
|
||||
match msg.photo() {
|
||||
Some(s) => true,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_channel_user(msg: Message) -> bool {
|
||||
match msg.from() {
|
||||
Some(u) => u.is_channel(),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user