From 2d50440bf1e8b84ed4c5a5dd3cad2b67b789c608 Mon Sep 17 00:00:00 2001 From: YoursFunny Date: Mon, 21 Sep 2026 17:33:45 +0800 Subject: [PATCH] refactor(commands): one require_admin gate for the admin-only commands /bot_dict and /clear_cache each resolved the sender id and replied "Admin only." on their own; the helper returns the id or answers the refusal, so both arms read as one line. --- crates/xmedia-bot/src/handlers/commands.rs | 33 ++++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/crates/xmedia-bot/src/handlers/commands.rs b/crates/xmedia-bot/src/handlers/commands.rs index 82aa109..0e8c549 100644 --- a/crates/xmedia-bot/src/handlers/commands.rs +++ b/crates/xmedia-bot/src/handlers/commands.rs @@ -396,13 +396,7 @@ pub(crate) async fn execute_command( Command::BotDict => { // Debug dump of the chat's persisted state: admin only (it echoes // forward-channel ids and templates to whoever asks). - let sender_id = message - .from - .as_ref() - .map(|user| user.id.0 as i64) - .unwrap_or(-1); - if !CONFIG.admin_ids.contains(&sender_id) { - reply(bot, message.chat.id.0, message.id, "Admin only.").await?; + if require_admin(bot, message).await?.is_none() { return Ok(()); } let chat_data = CHAT_STORE.get(message.chat.id.0).await; @@ -492,15 +486,9 @@ pub(crate) async fn execute_command( .await?; } Command::ClearCache(arg) => { - let sender_id = message - .from - .as_ref() - .map(|user| user.id.0 as i64) - .unwrap_or(-1); - if !CONFIG.admin_ids.contains(&sender_id) { - reply(bot, message.chat.id.0, message.id, "Admin only.").await?; + let Some(sender_id) = require_admin(bot, message).await? else { return Ok(()); - } + }; let arg = arg.trim(); if arg.is_empty() { let removed = LINK_CACHE.clear(None).await; @@ -650,6 +638,21 @@ pub(crate) async fn execute_command( Ok(()) } +/// The gate the admin-only commands share: `Some(sender_id)` for an admin, +/// `None` after the refusal has been sent (the command then returns). +async fn require_admin(bot: &Bot, message: &Message) -> Result, RequestError> { + let sender_id = message + .from + .as_ref() + .map(|user| user.id.0 as i64) + .unwrap_or(-1); + if CONFIG.admin_ids.contains(&sender_id) { + return Ok(Some(sender_id)); + } + reply(bot, message.chat.id.0, message.id, "Admin only.").await?; + Ok(None) +} + /// `"y"` for one, `"ies"` for anything else — "1 entry" / "2 entries". fn plural(n: usize) -> &'static str { if n == 1 { "y" } else { "ies" }