From d28bc12a9dc8b0e4c5fde3cd8e6f0f3d07c8d47e Mon Sep 17 00:00:00 2001 From: Sokhibjon Orzikulov Date: Sat, 18 Nov 2023 02:03:49 +0500 Subject: [PATCH] offtopic implemented --- delta/mod.ts | 2 - delta/trigger.ts | 128 --------------------------------------- delta/which.ts | 21 ------- src/functions/groups.rs | 8 ++- src/functions/mod.rs | 20 +++--- src/functions/offtop.rs | 47 ++++++++++++++ src/functions/version.rs | 19 +++--- src/lib.rs | 14 +++-- src/main.rs | 5 +- 9 files changed, 88 insertions(+), 176 deletions(-) delete mode 100644 delta/trigger.ts delete mode 100644 delta/which.ts create mode 100644 src/functions/offtop.rs diff --git a/delta/mod.ts b/delta/mod.ts index 397aa2e..88ae65f 100644 --- a/delta/mod.ts +++ b/delta/mod.ts @@ -1,11 +1,9 @@ -import which from "./which.ts"; import { Bot } from "../deps.ts"; import trigger from "./trigger.ts"; import useful from "./useful.ts"; export default async (bot: Bot) => { await bot - .use(which) .use(useful) .use(trigger) }; diff --git a/delta/trigger.ts b/delta/trigger.ts deleted file mode 100644 index 704de4c..0000000 --- a/delta/trigger.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { Composer, Context, InlineKeyboard } from "../deps.ts"; -import isReply from "../hooks/isReply.ts"; -import { reply } from "../utils/sender.ts"; - -const composer = new Composer(); - -composer.command("off", isReply, async (ctx: Context): Promise => { - if (ctx?.message?.reply_to_message?.from?.id === ctx.me.id) { - await ctx.reply(`Ha-ha... yaxshi urinish!`); - await ctx.reply(`Ha-ha... yaxshi urinish!`, { - parse_mode: "HTML", - }); - } else { - await ctx.api.deleteMessage( - ctx.message!.chat!.id, - ctx.message!.reply_to_message!.message_id, - ); - await ctx.api.deleteMessage( - ctx.message!.chat!.id, - ctx.message!.message_id, - ); - - const message = - `Hurmatli ${ctx?.message?.reply_to_message?.from?.first_name},` + - `\n` + - `\n` + - `Tushunishim bo'yicha siz mavzudan chetlayashayabsiz. Iltimos, ` + - `quyidagi tugmachani bosish orqali bizning offtop guruhga o'tib oling! ` + - `Offtopic guruhimizda istalgan mavzuda suhbatlashish ruxsat etiladi. Boshqalarga halaqit qilmayliga 😉` + - `\n` + - `\n` + - `Hurmat ila, Rustina (Rastina)`; - - const keyboard = new InlineKeyboard().url( - `Offtop Chat`, - `https://t.me/rustlanguz_offtopic`, - ); - - await reply(ctx, message, keyboard); - } -}); - -composer.command("nometa", isReply, async (ctx: Context): Promise => { - if (ctx?.message?.reply_to_message?.from?.id === ctx.me.id) { - await ctx.reply(`Ha-ha... yaxshi urinish!`); - } else { - await ctx.api.deleteMessage( - ctx.message!.chat!.id, - ctx.message!.reply_to_message!.message_id, - ); - - await ctx.api.deleteMessage( - ctx.message!.chat!.id, - ctx.message!.message_id, - ); - - const message = - `Hurmatli ${ctx?.message?.reply_to_message?.from?.first_name},` + - `\n` + - `\n` + - `Tushunishim bo'yicha siz boshqalarni vaqtini isrof qilayabsiz. Iltimos, ` + - `quyidagi tugmachani bosish orqali to'g'ri savol berishni o'rganing! ` + - `Boshqalarni ham sizni kutgani ko'p vaqti yo'q. Hammaning vaqtini hurmat qilayliga 😉` + - `\n` + - `\n` + - `Hurmat ila, Rustina (Rastina)`; - - const keyboard = new InlineKeyboard().url( - `Nometa Qo'llanmasi`, - `https://nometa.uz/`, - ); - - await reply(ctx, message, keyboard); - } -}); - -composer.command("nonoff", isReply, async (ctx: Context): Promise => { - if (ctx?.message?.reply_to_message?.from?.id === ctx.me.id) { - await ctx.reply(`Ha-ha... yaxshi urinish!`); - } else { - await ctx.api.deleteMessage( - ctx.message!.chat!.id, - ctx.message!.reply_to_message!.message_id, - ); - - await ctx.api.deleteMessage( - ctx.message!.chat!.id, - ctx.message!.message_id, - ); - - const message = - `Hurmatli ${ctx?.message?.reply_to_message?.from?.first_name},` + - `\n` + - `\n` + - `Chunishim bo'yicha siz mavzuga kirib ketayabsiz. Iltimos, ` + - `quyidagi tugmachani bosish orqali bizning asosiy guruhga o'tib oling! ` + - `Linux haqida iloji boricha asosiy guruhimizda suhbatlashish tavsiya etiladi. Offtopchilarga halaqit qilmayliga 😉` + - `\n` + - `\n` + - `Hurmat ila, Rustina (Rastina)`; - - const keyboard = new InlineKeyboard().url( - `Asosiy Chat`, - `https://t.me/rustlanguz`, - ); - - await reply(ctx, message, keyboard); - } -}); - -composer.command("doc", isReply, async (ctx: Context): Promise => { - if (ctx?.message?.reply_to_message?.from?.id === ctx.me.id) { - await reply(ctx, `Ha-ha... yaxshi urinish!`); - } else { - const message = - `Demak, ${ctx?.message?.reply_to_message?.from?.first_name},` + - `\n` + - `\n` + - `Bir bor ekan, bir yo'q ekan... Qadim o'tgan zamonlarda dokumentatsiya ` + - `bo'lgan ekan. Aytishlariga qaraganda, undan deyarli hamma savollarga ` + - `javob olsa bo'larkanda. Yanachi, unga avtorlar shunchalik ko'p vaqt ajratishar ` + - `ekanu, lekin uni sanoqligina odam o'qisharkan. Attang...`; - - await reply(ctx, message); - } -}); - -export default composer; diff --git a/delta/which.ts b/delta/which.ts deleted file mode 100644 index 180e968..0000000 --- a/delta/which.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Composer, Context } from "../deps.ts"; -import isGroup from "../hooks/isGroup.ts"; -import { reply } from "../utils/sender.ts"; - -const composer = new Composer(); - -composer.command("which", isGroup, async (ctx: Context): Promise => { - const status = (await ctx.getChatMember(ctx!.from!.id)).status; - - const message = `Rustacean ${ctx!.from!.first_name} metrikasi:` + - `\n` + - `\n` + - `Ismi: ${ctx!.from!.first_name} ` + `\n` + - (ctx?.from?.username && "Username: @" + ctx.from.username + `\n`) + - (ctx?.chat?.id && `Chat ID: ${ctx.chat.id}` + `\n`) + - `Status: ${status}`; - - await reply(ctx, message); -}); - -export default composer; diff --git a/src/functions/groups.rs b/src/functions/groups.rs index a023dd7..104e004 100644 --- a/src/functions/groups.rs +++ b/src/functions/groups.rs @@ -11,7 +11,6 @@ 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, groups: &Groups) -> ResponseResult<()> { - bot.send_message(msg.chat.id, TEXT) .parse_mode(ParseMode::Html) .reply_markup(keyboard_list(&groups, 1)) @@ -20,7 +19,12 @@ pub async fn command(bot: &Bot, msg: &Message, groups: &Groups) -> ResponseResul Ok(()) } -pub async fn callback_list(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>, groups: &Groups) -> ResponseResult<()> { +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 e0928c7..abb09c2 100644 --- a/src/functions/mod.rs +++ b/src/functions/mod.rs @@ -3,6 +3,7 @@ pub mod groups; pub mod help; pub mod inline; pub mod latest; +pub mod offtop; pub mod rules; pub mod start; pub mod version; @@ -10,14 +11,15 @@ pub mod version; pub use inline::inline; use crate::utils::github::GitHub; -use crate::Command; use crate::utils::group_manager::Groups; +use crate::Command; use std::error::Error; use teloxide::prelude::*; +use teloxide::types::*; pub async fn commands( bot: Bot, - _me: teloxide::types::Me, + me: Me, msg: Message, cmd: Command, github: GitHub, @@ -31,6 +33,7 @@ pub async fn commands( 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, + Command::Off => crate::functions::offtop::command(&bot, &msg, &me).await, }; Ok(()) @@ -55,7 +58,9 @@ pub async fn callback( "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, + "changelog" => { + crate::functions::version::callback_detail(&bot, &q, &args, github).await + } _ => Ok(()), }; } @@ -63,17 +68,14 @@ pub async fn callback( Ok(()) } -pub async fn triggers( - bot: Bot, - msg: Message, -) -> Result<(), Box> { +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(_) => {} + Err(_) => {} } } } diff --git a/src/functions/offtop.rs b/src/functions/offtop.rs new file mode 100644 index 0000000..014d496 --- /dev/null +++ b/src/functions/offtop.rs @@ -0,0 +1,47 @@ +use teloxide::{prelude::*, types::*}; + +static TEXT_FAIL: &str = "Ha-ha... yaxshi urinish!"; +static TEXT_NON_REPLY: &str = "↪ Reply bilan ko'rsatingchi habarni!"; + +pub async fn command(bot: &Bot, msg: &Message, me: &Me) -> ResponseResult<()> { + if msg.reply_to_message().is_none() { + return { + bot.send_message(msg.chat.id, TEXT_NON_REPLY).await?; + Ok(()) + }; + } + + // if replied person is bot itself, send fail message + if let Some(user) = msg.reply_to_message().as_ref().unwrap().from() { + if user.username.clone().unwrap() == me.username() { + return { + bot.send_message(msg.chat.id, TEXT_FAIL).await?; + Ok(()) + }; + } + } + + bot.delete_message(msg.chat.id, msg.id).await?; + bot.delete_message(msg.chat.id, msg.reply_to_message().unwrap().id) + .await?; + + bot.send_message(msg.chat.id, view(msg.reply_to_message().unwrap())) + .parse_mode(ParseMode::Html) + .await?; + + Ok(()) +} + +pub fn view(msg: &Message) -> String { + format!( + "Hurmatli {},\ + \n\n\ + Tushunishim bo'yicha siz mavzudan chetlayashayabsiz. Iltimos, \ + quyidagi tugmachani bosish orqali bizning offtop guruhga o'tib oling! \ + Offtopic guruhimizda istalgan mavzuda suhbatlashish ruxsat etiladi. Boshqalarga halaqit qilmayliga 😉\ + \n\n\ + Hurmat ila, Rustina (Rastina)", + msg.from().unwrap().id, + msg.from().unwrap().first_name + ) +} diff --git a/src/functions/version.rs b/src/functions/version.rs index db4c6bd..1ea36c3 100644 --- a/src/functions/version.rs +++ b/src/functions/version.rs @@ -45,8 +45,12 @@ pub async fn callback_list( Ok(()) } - -pub async fn callback_detail(bot: &Bot, q: &CallbackQuery, args: &Vec<&str>, github: GitHub) -> ResponseResult<()> { +pub async fn callback_detail( + bot: &Bot, + q: &CallbackQuery, + args: &Vec<&str>, + github: GitHub, +) -> ResponseResult<()> { let page = args[0].parse::().unwrap(); let version: Release = github.get_detail(args[1]).await.unwrap(); @@ -88,7 +92,10 @@ pub fn keyboard_list( let mut keyboard = Keyboard::new(); for release in releases { - keyboard.text(&release.tag_name, &format!("changelog_{}_{}", page, release.tag_name)); + keyboard.text( + &release.tag_name, + &format!("changelog_{}_{}", page, release.tag_name), + ); keyboard.row(); } @@ -103,10 +110,7 @@ pub fn keyboard_list( keyboard.get() } -pub fn keyboard_detail( - page: u32, - release: Release, -) -> InlineKeyboardMarkup { +pub fn keyboard_detail(page: u32, release: Release) -> InlineKeyboardMarkup { let mut keyboard = Keyboard::new(); keyboard.url("📝 GitHub da o'qish", release.html_url.as_str()); @@ -115,4 +119,3 @@ pub fn keyboard_detail( keyboard.get() } - diff --git a/src/lib.rs b/src/lib.rs index a08609d..a8a35f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,11 @@ pub mod functions; pub mod hooks; pub mod utils; -use teloxide::{dispatching::{UpdateHandler, UpdateFilterExt}, prelude::*, utils::command::BotCommands}; +use teloxide::{ + dispatching::{UpdateFilterExt, UpdateHandler}, + prelude::*, + utils::command::BotCommands, +}; #[derive(BotCommands, Clone, Debug)] #[command(rename_rule = "lowercase", parse_with = "split")] @@ -28,6 +32,9 @@ pub enum Command { /// Specific version Version, + + /// Report offtopic + Off, } pub fn handler() -> UpdateHandler> { @@ -42,8 +49,5 @@ pub fn handler() -> UpdateHandler() .endpoint(functions::commands), ) - .branch( - Update::filter_message() - .endpoint(functions::triggers), - ) + .branch(Update::filter_message().endpoint(functions::triggers)) } diff --git a/src/main.rs b/src/main.rs index b81bfa4..1797ff2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ use crates_io_api::AsyncClient; -use rustina::{handler, utils::{github::GitHub, group_manager::Groups}}; +use rustina::{ + handler, + utils::{github::GitHub, group_manager::Groups}, +}; use std::error::Error; use teloxide::prelude::*;