From ff4ba90e2129c997f2fc4e90a70eac9060df9ff7 Mon Sep 17 00:00:00 2001 From: Sokhibjon Orzikulov Date: Sat, 18 Nov 2023 01:44:33 +0500 Subject: [PATCH] implemented channel user cleaners --- delta/channel.ts | 15 --------------- delta/mod.ts | 2 -- src/functions/groups.rs | 7 ++----- src/functions/mod.rs | 26 ++++++++++++++++++++++++-- src/lib.rs | 6 +++++- src/main.rs | 8 ++++---- 6 files changed, 35 insertions(+), 29 deletions(-) delete mode 100644 delta/channel.ts diff --git a/delta/channel.ts b/delta/channel.ts deleted file mode 100644 index 19e1991..0000000 --- a/delta/channel.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Composer, Context } from "../deps.ts"; - -const composer = new Composer(); - -composer.on("message:text", async (ctx: Context): Promise => { - if (ctx?.message?.from?.username) { - if ( - ctx?.message?.from?.username === "Channel_Bot" - ) { - await ctx.deleteMessage(); - } - } -}); - -export default composer; diff --git a/delta/mod.ts b/delta/mod.ts index d760e89..397aa2e 100644 --- a/delta/mod.ts +++ b/delta/mod.ts @@ -1,6 +1,5 @@ import which from "./which.ts"; import { Bot } from "../deps.ts"; -import channel from "./channel.ts"; import trigger from "./trigger.ts"; import useful from "./useful.ts"; @@ -9,5 +8,4 @@ export default async (bot: Bot) => { .use(which) .use(useful) .use(trigger) - .use(channel); }; diff --git a/src/functions/groups.rs b/src/functions/groups.rs index fc89106..a023dd7 100644 --- a/src/functions/groups.rs +++ b/src/functions/groups.rs @@ -10,8 +10,7 @@ use teloxide::{ static TEXT: &str = "Telegramdagi Rust Hamjamiyatlari yoki Guruhlari:\nAgar o'zingizni guruhingizni qo'shmoqchi bo'lsangiz, bizni community.json ni yangilang!"; -pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> { - let groups: Groups = Groups::new(); +pub async fn command(bot: &Bot, msg: &Message, groups: &Groups) -> ResponseResult<()> { bot.send_message(msg.chat.id, TEXT) .parse_mode(ParseMode::Html) @@ -21,9 +20,7 @@ pub async fn command(bot: &Bot, msg: &Message) -> ResponseResult<()> { Ok(()) } -pub async fn callback_list(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>) -> ResponseResult<()> { - let groups: Groups = Groups::new(); - +pub async fn callback_list(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>, groups: &Groups) -> ResponseResult<()> { if !args.is_empty() { if let Some(Message { id, chat, .. }) = q.message.clone() { bot.edit_message_text(chat.id, id, TEXT) diff --git a/src/functions/mod.rs b/src/functions/mod.rs index 3990967..e0928c7 100644 --- a/src/functions/mod.rs +++ b/src/functions/mod.rs @@ -11,6 +11,7 @@ pub use inline::inline; use crate::utils::github::GitHub; use crate::Command; +use crate::utils::group_manager::Groups; use std::error::Error; use teloxide::prelude::*; @@ -20,13 +21,14 @@ pub async fn commands( msg: Message, cmd: Command, github: GitHub, + groups: Groups, ) -> Result<(), Box> { let _ = match cmd { Command::Start => crate::functions::start::command(&bot, &msg).await, Command::Help => crate::functions::help::command(&bot, &msg, &cmd).await, Command::Rules => crate::functions::rules::command(&bot, &msg).await, Command::About => crate::functions::about::command(&bot, &msg).await, - Command::Groups => crate::functions::groups::command(&bot, &msg).await, + Command::Groups => crate::functions::groups::command(&bot, &msg, &groups).await, Command::Latest => crate::functions::latest::command(&bot, github, &msg).await, Command::Version => crate::functions::version::command(&bot, github, &msg).await, }; @@ -38,6 +40,7 @@ pub async fn callback( bot: Bot, q: CallbackQuery, github: GitHub, + groups: Groups, ) -> Result<(), Box> { let mut args: Vec<&str> = Vec::new(); @@ -49,7 +52,7 @@ pub async fn callback( } let _ = match args.remove(0) { - "group" => crate::functions::groups::callback_list(&bot, &q, &args).await, + "group" => crate::functions::groups::callback_list(&bot, &q, &args, &groups).await, "detail" => crate::functions::groups::callback_detail(&bot, &q, &args).await, "version" => crate::functions::version::callback_list(&bot, &q, &args, github).await, "changelog" => crate::functions::version::callback_detail(&bot, &q, &args, github).await, @@ -59,3 +62,22 @@ pub async fn callback( Ok(()) } + +pub async fn triggers( + bot: Bot, + msg: Message, +) -> Result<(), Box> { + if let Some(user) = msg.from() { + if let Some(username) = user.username.clone() { + if username == "Channel_Bot" { + // try to delete message and ignore error + match bot.delete_message(msg.chat.id, msg.id).await { + Ok(_) => {}, + Err(_) => {}, + } + } + } + } + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index f45474b..a08609d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ pub mod functions; pub mod hooks; pub mod utils; -use teloxide::{dispatching::UpdateHandler, prelude::*, utils::command::BotCommands}; +use teloxide::{dispatching::{UpdateHandler, UpdateFilterExt}, prelude::*, utils::command::BotCommands}; #[derive(BotCommands, Clone, Debug)] #[command(rename_rule = "lowercase", parse_with = "split")] @@ -42,4 +42,8 @@ pub fn handler() -> UpdateHandler() .endpoint(functions::commands), ) + .branch( + Update::filter_message() + .endpoint(functions::triggers), + ) } diff --git a/src/main.rs b/src/main.rs index 864af7e..b81bfa4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use crates_io_api::AsyncClient; -use rustina::{handler, utils::github::GitHub}; +use rustina::{handler, utils::{github::GitHub, group_manager::Groups}}; use std::error::Error; use teloxide::prelude::*; @@ -10,16 +10,16 @@ async fn main() -> Result<(), Box> { let bot = Bot::from_env(); + let groups: Groups = Groups::new(); + let github = GitHub::new(); let crates_client = AsyncClient::new( "Rustina Assistant (rust@maid.uz)", std::time::Duration::from_millis(100), ) .unwrap(); - let github = GitHub::new(); - Dispatcher::builder(bot, handler()) - .dependencies(dptree::deps![crates_client, github]) + .dependencies(dptree::deps![crates_client, github, groups]) // If no handler succeeded to handle an update, this closure will be called .default_handler(|upd| async move { log::warn!("Unhandled update: {:?}", upd);