From df99275455d690ce15bfc68ac10265fb50d9e633 Mon Sep 17 00:00:00 2001 From: Sense T Date: Thu, 18 Jan 2024 17:19:37 +0800 Subject: [PATCH] group blacklist --- deploy/k8s/kustomization.yaml | 1 + flake.nix | 9 ++++++++- src/config.rs | 4 ++++ src/main.rs | 18 +++++++++++++----- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/deploy/k8s/kustomization.yaml b/deploy/k8s/kustomization.yaml index 673401d..9cfa4d1 100644 --- a/deploy/k8s/kustomization.yaml +++ b/deploy/k8s/kustomization.yaml @@ -7,6 +7,7 @@ secretGenerator: files: - config/DATABASE_URI - config/TGBOT_TOKEN + - config/GROUP_BANNED images: - name: newName: ghcr.io/senseab/hangitbot diff --git a/flake.nix b/flake.nix index 55dd20b..d01907a 100644 --- a/flake.nix +++ b/flake.nix @@ -75,14 +75,21 @@ description = lib.mdDoc "Custom telegram api URI"; }; + groupBanned = mkOption { + type = types.listOf types.int; + default = []; + description = lib.mdDoc "GroupID blacklisted"; + }; + extraOptions = mkOption { type = types.str; description = lib.mdDoc "Extra option for bot."; + default = ""; }; }; config = let - args = "${cfg.extraOptions} ${if isString cfg.tgUri then "--api-uri ${escapeShellArg cfg.tgUri}" else ""}"; + args = "${cfg.extraOptions} ${if cfg?tgUri then "--api-uri ${escapeShellArg cfg.tgUri}" else ""} ${if cfg?groupBanned then concatStringsSep " " (lists.concatMap (group: ["-b ${group}"]) cfg.groupBanned) else ""}"; in mkIf cfg.enable { systemd.services.hangitbot = { wantedBy = ["multi-uesr.target"]; diff --git a/src/config.rs b/src/config.rs index 833e998..d37e88d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,4 +24,8 @@ pub struct Args { /// Api Server URL #[clap(long, value_parser, env = "API_URL", default_value=DEFAULT_API_URL)] pub api_url: String, + + /// GroupID blacklisted + #[clap(short = 'b', long, value_parser, env = "GROUP_BANNED", value_delimiter = ',', num_args = 1..)] + pub group_banned: Vec } diff --git a/src/main.rs b/src/main.rs index 92a0003..314e78c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use teloxide::{ }; use utils::message_handler; use wd_log::{ - log_debug_ln, log_error_ln, log_info_ln, log_panic, set_level, set_prefix, DEBUG, INFO, + log_debug_ln, log_error_ln, log_info_ln, log_panic, set_level, set_prefix, DEBUG, INFO, log_warn_ln, }; #[tokio::main] @@ -56,7 +56,11 @@ async fn main() { .branch( Update::filter_message() .branch(dptree::entry().filter_command::().endpoint( - |db: Controller, bot: Bot, message: Message, cmd: Commands| async move { + |db: Controller, black_list: Vec, bot: Bot, message: Message, cmd: Commands| async move { + if black_list.contains(&message.chat.id.0) { + log_warn_ln!("banned group dectected: {:?}", message.chat.id); + return Ok(()); + } let r = match cmd { Commands::Help => help_handler(&bot, &message).await, Commands::About => about_handler(&bot, &message).await, @@ -75,8 +79,12 @@ async fn main() { )) .branch( dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup()) - .endpoint(|db: Controller, msg: Message, me: Me| async move { - let r = message_handler(&db, msg, &me).await; + .endpoint(|db: Controller, message: Message, me: Me, black_list: Vec| async move { + if black_list.contains(&message.chat.id.0) { + log_warn_ln!("banned group dectected: {:?}", message.chat.id); + return Ok(()); + } + let r = message_handler(&db, message, &me).await; match r { Ok(_) => Ok(()), Err(err) => { @@ -99,7 +107,7 @@ async fn main() { )); Dispatcher::builder(bot, handler) - .dependencies(dptree::deps![db_controller, me]) + .dependencies(dptree::deps![db_controller, me, args.group_banned]) .default_handler(|upd| async move { log_debug_ln!("unhandled update: {:?}", upd) }) .enable_ctrlc_handler() .build()