Skip to content

Commit

Permalink
implemented channel user cleaners
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Nov 17, 2023
1 parent 4b0f62b commit ff4ba90
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
15 changes: 0 additions & 15 deletions delta/channel.ts

This file was deleted.

2 changes: 0 additions & 2 deletions delta/mod.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -9,5 +8,4 @@ export default async (bot: Bot) => {
.use(which)
.use(useful)
.use(trigger)
.use(channel);
};
7 changes: 2 additions & 5 deletions src/functions/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use teloxide::{

static TEXT: &str = "<b>Telegramdagi Rust Hamjamiyatlari yoki Guruhlari:</b>\nAgar o'zingizni guruhingizni qo'shmoqchi bo'lsangiz, bizni <a href='https://github.com/rust-lang-uz/rustina/blob/main/communities.json'>community.json</a> 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)
Expand All @@ -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)
Expand Down
26 changes: 24 additions & 2 deletions src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand All @@ -20,13 +21,14 @@ pub async fn commands(
msg: Message,
cmd: Command,
github: GitHub,
groups: Groups,
) -> Result<(), Box<dyn Error + Send + Sync>> {
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,
};
Expand All @@ -38,6 +40,7 @@ pub async fn callback(
bot: Bot,
q: CallbackQuery,
github: GitHub,
groups: Groups,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut args: Vec<&str> = Vec::new();

Expand All @@ -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,
Expand All @@ -59,3 +62,22 @@ pub async fn callback(

Ok(())
}

pub async fn triggers(
bot: Bot,
msg: Message,
) -> Result<(), Box<dyn Error + Send + Sync>> {
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(())
}
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -42,4 +42,8 @@ pub fn handler() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'sta
.filter_command::<Command>()
.endpoint(functions::commands),
)
.branch(
Update::filter_message()
.endpoint(functions::triggers),
)
}
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand All @@ -10,16 +10,16 @@ async fn main() -> Result<(), Box<dyn Error>> {

let bot = Bot::from_env();

let groups: Groups = Groups::new();
let github = GitHub::new();
let crates_client = AsyncClient::new(
"Rustina Assistant ([email protected])",
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);
Expand Down

0 comments on commit ff4ba90

Please sign in to comment.