From 155a7dcd1f1e8c2784f6747c03fd11f579b60d2d Mon Sep 17 00:00:00 2001 From: nurikk <1525421+nurikk@users.noreply.github.com> Date: Sun, 22 Mar 2026 23:45:04 +0000 Subject: [PATCH 1/2] refactor: remove notify_user duplicate, move local imports to module level - Remove notify_user() which was identical to send_message(), update all callers in cli.py to use send_message instead - Move local imports to module level in bot.py and cli.py to comply with project code style rule ("No local imports") - Add .ccremote-attachments to .gitignore --- .gitignore | 1 + src/ccremote/bot.py | 16 +--------------- src/ccremote/cli.py | 18 ++++++++---------- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index bfa8e6a..12755b7 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ htmlcov/ .env .env.* .ccremote +.ccremote-attachments diff --git a/src/ccremote/bot.py b/src/ccremote/bot.py index 707bb4b..0d2da7b 100644 --- a/src/ccremote/bot.py +++ b/src/ccremote/bot.py @@ -7,6 +7,7 @@ from aiogram import Bot, Dispatcher, F from aiogram.enums import ParseMode +from aiogram.types import BotCommand, BotCommandScopeChat from ccremote.markdown import md_to_tg_html @@ -54,8 +55,6 @@ async def register_commands(bot: Bot, chat_id: int, commands: list[tuple[str, st Fetches current commands first and only calls set_my_commands if they differ. """ - from aiogram.types import BotCommand, BotCommandScopeChat - if not commands: await unregister_commands(bot, chat_id) return @@ -82,22 +81,9 @@ async def register_commands(bot: Bot, chat_id: int, commands: list[tuple[str, st async def unregister_commands(bot: Bot, chat_id: int) -> None: """Remove all bot commands for this chat.""" - from aiogram.types import BotCommandScopeChat - try: await bot.delete_my_commands(scope=BotCommandScopeChat(chat_id=chat_id)) logger.info("Unregistered commands for chat %s", chat_id) except Exception: logger.exception("Failed to unregister commands for chat %s", chat_id) - -async def notify_user(bot: Bot, chat_id: int, text: str) -> None: - """Send a notification DM.""" - try: - await bot.send_message( - chat_id=chat_id, - text=md_to_tg_html(text), - parse_mode=ParseMode.HTML, - ) - except Exception: - logger.debug("Could not notify user %s", chat_id) diff --git a/src/ccremote/cli.py b/src/ccremote/cli.py index 9862909..652445d 100644 --- a/src/ccremote/cli.py +++ b/src/ccremote/cli.py @@ -7,9 +7,15 @@ import contextlib import logging import sys +import uuid from pathlib import Path +from aiogram import Bot + +from ccremote.bot import create_dispatcher, send_message from ccremote.config import SETUP_INSTRUCTIONS, ConfigError, config_file_exists, load_config +from ccremote.models import Session +from ccremote.relay import setup_relay_handlers logger = logging.getLogger(__name__) @@ -42,14 +48,6 @@ def main() -> None: ) async def _run() -> None: - import uuid - - from aiogram import Bot - - from ccremote.bot import create_dispatcher, notify_user - from ccremote.models import Session - from ccremote.relay import setup_relay_handlers - dp = create_dispatcher(config) async with Bot(token=config.bot_token) as bot: @@ -62,7 +60,7 @@ async def _run() -> None: ) msg = f"🟢 **ccremote active**\n`{cwd}`" - await notify_user(bot, config.allowed_user, msg) + await send_message(bot, config.allowed_user, msg) logger.info("ccremote active in %s — send messages to @%s", cwd, me.username) setup_relay_handlers(dp, bot, session, config) @@ -73,7 +71,7 @@ async def _run() -> None: logger.info("Stopping ccremote...") session.terminate() with contextlib.suppress(Exception): - await notify_user(bot, config.allowed_user, "🔴 **ccremote stopped**") + await send_message(bot, config.allowed_user, "🔴 **ccremote stopped**") logger.info("Stopped.") with contextlib.suppress(KeyboardInterrupt): From 2e096c7d7a27035ff10d251d216089f609db4ad0 Mon Sep 17 00:00:00 2001 From: nurikk <1525421+nurikk@users.noreply.github.com> Date: Sun, 22 Mar 2026 23:46:12 +0000 Subject: [PATCH 2/2] style: ruff format bot.py --- src/ccremote/bot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ccremote/bot.py b/src/ccremote/bot.py index 0d2da7b..8149435 100644 --- a/src/ccremote/bot.py +++ b/src/ccremote/bot.py @@ -86,4 +86,3 @@ async def unregister_commands(bot: Bot, chat_id: int) -> None: logger.info("Unregistered commands for chat %s", chat_id) except Exception: logger.exception("Failed to unregister commands for chat %s", chat_id) -