Skip to content

refactor(bot): 命令分家#22

Open
linhemin wants to merge 9 commits intoembyplus:mainfrom
linhemin:main
Open

refactor(bot): 命令分家#22
linhemin wants to merge 9 commits intoembyplus:mainfrom
linhemin:main

Conversation

@linhemin
Copy link
Contributor

@linhemin linhemin commented Feb 12, 2025

  • 新增 AdminCommandHandler 和 UserCommandHandler 类,分离管理员和用户命令处理逻辑
  • 创建 command_router.py 文件,统一管理命令路由
  • 移除 commands.py 中冗余的命令处理逻辑

好的,这是翻译成中文的 pull request 总结:

Sourcery 总结

重构命令处理逻辑,将管理命令和用户命令分离到专用类中,并引入集中式命令路由器。

新特性:

  • 为管理命令和用户命令实现单独的命令处理程序。

增强功能:

  • 引入命令路由器来管理命令路由逻辑。
Original summary in English

Summary by Sourcery

Refactor command handling logic by separating admin and user commands into dedicated classes and introducing a centralized command router.

New Features:

  • Implement separate command handlers for admin and user commands.

Enhancements:

  • Introduce a command router to manage command routing logic.

- 新增 AdminCommandHandler 和 UserCommandHandler 类,分离管理员和用户命令处理逻辑
- 创建 command_router.py 文件,统一管理命令路由
- 移除 commands.py 中冗余的命令处理逻辑
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 12, 2025

## Sourcery 审查者指南

此 PR 通过将用户和管理员命令分离到不同的处理程序中,集中命令路由,以及模块化实用程序函数,来重构了机器人命令处理。此外,它还删除了冗余代码,并更新了在用户服务中访问某些属性的方式。

#### AdminCommandHandler & UserCommandHandler 的类图

```mermaid
classDiagram
    class UserCommandHandler {
      - bot_client: BotClient
      - user_service: UserService
      - code_to_message_id: Dict
      + count(message: Message)
      + info(message: Message)
      + use_code(message: Message)
      + select_line(message: Message)
      + create_user(message: Message)
      + reset_emby_password(message: Message)
      + help_command(message: Message)
      + handle_callback_query(client, CallbackQuery)
    }

    class AdminCommandHandler {
      - bot_client: BotClient
      - user_service: UserService
      - code_to_message_id: Dict
      + new_code(message: Message)
      + new_whitelist_code(message: Message)
      + ban_emby(message: Message)
      + unban_emby(message: Message)
      + register_until(message: Message)
      + register_amount(message: Message)
      + send_code(code_list, message: Message, whitelist: bool)
      + group_member_change_handler(client, message: Message)
    }

    UserCommandHandler <|-- AdminCommandHandler : similar structure
    note for UserCommandHandler "处理所有用户发起的命令"
    note for AdminCommandHandler "处理所有管理员相关的命令"

文件级别变更

变更 详情 文件
将命令处理逻辑分离到 AdminCommandHandler 和 UserCommandHandler 中。
  • 在 bot/admin_command.py 中创建了新的 AdminCommandHandler 类来处理管理员特定的命令。
  • 在 bot/user_command.py 中创建了新的 UserCommandHandler 类来管理用户相关的命令。
  • 从 bot/commands.py 中删除了冗余的命令逻辑,并引入了新处理程序的初始化。
  • 更新了 bot/commands.py 以初始化新的处理程序,并使用命令路由器设置命令路由。
bot/commands.py
bot/admin_command.py
bot/user_command.py
使用专用模块集中命令路由。
  • 添加了一个新的文件 bot/command_router.py 来定义和绑定命令路由处理程序。
  • 用新模块中的集中路由替换了 bot/commands.py 中的内联命令注册。
bot/command_router.py
模块化和扩展实用程序函数。
  • 使用新的辅助函数增强了 bot/utils.py:reply_html、parse_args、ensure_args 和 send_error,用于统一的消息处理。
bot/utils.py
用户服务配置处理中的小更新。
  • 更新了 services/user_service.py 以通过 username 属性而不是字典访问来引用 telegram_name。
services/user_service.py
调整了应用程序引导以进行命令路由。
  • 删除了 app.py 中对 setup_commands 的调用,将命令注册完全转移到命令路由器。
app.py

提示和命令

与 Sourcery 互动

  • 触发新的审查: 在 pull request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 通过回复审查评论,要求 Sourcery 从审查评论创建一个 issue。您也可以回复审查评论并使用 @sourcery-ai issue 从中创建一个 issue。
  • 生成 pull request 标题: 在 pull request 标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在 pull request 上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在 pull request 上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成审查者指南: 在 pull request 上评论 @sourcery-ai guide 以随时(重新)生成审查者指南。
  • 解决所有 Sourcery 评论: 在 pull request 上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经处理了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在 pull request 上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想从新的审查开始,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!
  • 为 issue 生成行动计划: 在 issue 上评论 @sourcery-ai plan 以为其生成行动计划。

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的 pull request 摘要、审查者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获得帮助

```
Original review guide in English

Reviewer's Guide by Sourcery

This PR refactors the bot command processing by separating user and admin commands into distinct handlers, centralizing command routing, and modularizing utility functions. Additionally, it removes redundant code and updates how certain properties are accessed in user services.

Class diagram for AdminCommandHandler & UserCommandHandler

classDiagram
    class UserCommandHandler {
      - bot_client: BotClient
      - user_service: UserService
      - code_to_message_id: Dict
      + count(message: Message)
      + info(message: Message)
      + use_code(message: Message)
      + select_line(message: Message)
      + create_user(message: Message)
      + reset_emby_password(message: Message)
      + help_command(message: Message)
      + handle_callback_query(client, CallbackQuery)
    }

    class AdminCommandHandler {
      - bot_client: BotClient
      - user_service: UserService
      - code_to_message_id: Dict
      + new_code(message: Message)
      + new_whitelist_code(message: Message)
      + ban_emby(message: Message)
      + unban_emby(message: Message)
      + register_until(message: Message)
      + register_amount(message: Message)
      + send_code(code_list, message: Message, whitelist: bool)
      + group_member_change_handler(client, message: Message)
    }

    UserCommandHandler <|-- AdminCommandHandler : similar structure
    note for UserCommandHandler "Handles all user-initiated commands"
    note for AdminCommandHandler "Handles all admin-related commands"
Loading

File-Level Changes

Change Details Files
Separated command handling logic into AdminCommandHandler and UserCommandHandler.
  • Created new AdminCommandHandler class in bot/admin_command.py to handle admin-specific commands.
  • Created new UserCommandHandler class in bot/user_command.py to manage user-related commands.
  • Removed the redundant command logic from bot/commands.py and introduced initialization of new handlers.
  • Updated bot/commands.py to initialize the new handlers and set up command routes using the command router.
bot/commands.py
bot/admin_command.py
bot/user_command.py
Centralized command routing using a dedicated module.
  • Added a new file bot/command_router.py to define and bind command route handlers.
  • Replaced inline command registrations in bot/commands.py with centralized routing in the new module.
bot/command_router.py
Modularized and extended utility functions.
  • Enhanced bot/utils.py with new helper functions: reply_html, parse_args, ensure_args, and send_error for uniform message handling.
bot/utils.py
Minor update in user service configuration handling.
  • Updated services/user_service.py to reference telegram_name via the username attribute instead of dictionary access.
services/user_service.py
Adjusted application bootstrap for command routing.
  • Removed the call to setup_commands in app.py, shifting command registration entirely to the command router.
app.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @linhemin - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Recursive call to group_member_change_handler causes unintentional recursion. (link)

Overall Comments:

  • Consider adding a base class or interface for AdminCommandHandler and UserCommandHandler to enforce a contract for command handlers.
  • The code_to_message_id dictionary is used in both AdminCommandHandler and UserCommandHandler; consider moving it to a shared base class or a separate utility.
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 1 other issue
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

- 新增 EventHandler 类统一处理回调查询和群组成员变更事件
- 从 AdminCommandHandler 和 UserCommandHandler 中移除相关事件处理代码
- 更新命令路由注册逻辑,使用新的 EventHandler 实例
- 优化日志记录配置,支持文件和控制台输出
- 将 admin_command、event_command 和 user_command 模块的导入路径修改为 bot.command包下的路径
- 这个改动统一了命令模块的导入路径格式,提高了代码的可维护性和可读性
- 将命令路由定义从单独的函数封装为统一的列表结构
- 通过循环动态注册消息处理器,简化了代码结构- 新增命令支持:new_code, new_whitelist_code, ban_emby, unban_emby, register_until, register_amount
- 保留了现有的回调查询和群组成员变动处理器
linhemin and others added 5 commits February 13, 2025 10:19
- 更新多个命令处理函数,使用新的参数解析装饰器- 优化代码结构,提高可读性和可维护性
- 移除了原有的 ensure_args 函数
- 新增了 with_ensure_args 装饰器,用于参数数量检查
- 在 admin_command.py 和 user_command.py 中使用新装饰器替换原有的参数检查逻辑- 更新了 with_parsed_args装饰器的注释说明
- 将 CommandHandler 类从 commands.py 移动到 command/__init__.py
- 创建新的 utils/__init__.py 文件,集中通用工具函数
- 更新模块导入路径,使用相对路径导入
- 优化代码格式和结构,提高可读性
- 调整代码缩进和换行,提高可读性
- 统一使用 f-string 格式化字符串
- 优化部分变量和函数命名
- 移除冗余的空行和导入
- 消除警告
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants