Feature: 创建启动通知 #53

Merged
Passthem merged 1 commits from feature/startup-notification into master 2026-02-25 16:13:01 +08:00
Owner

PR Type

Enhancement


Description

  • 新增 Bot 启动通知插件

  • 等待 Bot 上线后广播重启消息

  • 注册"启动通知"频道及其描述信息


Diagram Walkthrough

flowchart LR
  A["driver.on_startup"] -- "等待 bot 上线" --> B["asyncio task 轮询"]
  B -- "bot 就绪" --> C["broadcast 广播通知"]

File Walkthrough

Relevant files
Enhancement
startup_notify.py
新增启动通知插件,广播 Bot 重启消息                                                                         

konabot/plugins/startup_notify.py

  • 新增启动通知插件文件
  • 注册 CHANNEL_STARTUP 频道及 PosterInfo
  • 监听 on_startup 事件,轮询等待至少一个 bot 上线
  • bot 上线后延迟 3 秒通过 broadcast 发送重启通知
+33/-0   

### **PR Type** Enhancement ___ ### **Description** - 新增 Bot 启动通知插件 - 等待 Bot 上线后广播重启消息 - 注册"启动通知"频道及其描述信息 ___ ### Diagram Walkthrough ```mermaid flowchart LR A["driver.on_startup"] -- "等待 bot 上线" --> B["asyncio task 轮询"] B -- "bot 就绪" --> C["broadcast 广播通知"] ``` <details> <summary><h3> File Walkthrough</h3></summary> <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>startup_notify.py</strong><dd><code>新增启动通知插件,广播 Bot 重启消息</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary> <hr> konabot/plugins/startup_notify.py <ul><li>新增启动通知插件文件<br> <li> 注册 <code>CHANNEL_STARTUP</code> 频道及 <code>PosterInfo</code><br> <li> 监听 <code>on_startup</code> 事件,轮询等待至少一个 bot 上线<br> <li> bot 上线后延迟 3 秒通过 <code>broadcast</code> 发送重启通知</ul> </details> </td> <td><a href="https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/startup-notification/konabot/plugins/startup_notify.py">+33/-0</a>&nbsp; &nbsp; </td> </tr> </table></td></tr></tr></tbody></table> </details> ___
Passthem added 1 commit 2026-02-25 15:09:14 +08:00
Collaborator

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵
🧪 No relevant tests
🔒 No security concerns identified
 Recommended focus areas for review

无限循环风险

while True 轮询等待 bot 上线没有设置超时上限。如果 bot 因配置错误等原因永远无法上线,该任务将无限循环且每 15 秒轮询一次,永远不会退出。建议增加最大重试次数或总超时时间,并在超时后记录日志退出。

while True:
    if len(driver.bots) >= 1:
        break
    await asyncio.sleep(15)
任务引用丢失

asyncio.create_task(task()) 的返回值未被保存到任何变量中。Python 文档明确指出,如果不持有对 task 的引用,它可能会被垃圾回收器在完成前回收,导致任务静默消失。应将返回值赋给一个模块级变量或集合以保持强引用。

asyncio.create_task(task())
异常处理缺失

task() 内部的 broadcast 调用如果抛出异常,由于没有 try/except 包裹,异常只会作为 "Task exception was never retrieved" 被静默忽略(尤其在 task 引用丢失的情况下)。建议在 task 内部添加异常捕获和日志记录。

await broadcast(CHANNEL_STARTUP, UniMessage.text("此方 BOT 重启好了"))
## PR Reviewer Guide 🔍 Here are some key observations to aid the review process: <table> <tr><td>⏱️&nbsp;<strong>Estimated effort to review</strong>: 2 🔵🔵⚪⚪⚪</td></tr> <tr><td>🧪&nbsp;<strong>No relevant tests</strong></td></tr> <tr><td>🔒&nbsp;<strong>No security concerns identified</strong></td></tr> <tr><td>⚡&nbsp;<strong>Recommended focus areas for review</strong><br><br> <details><summary><a href='https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/startup-notification/konabot/plugins/startup_notify.py#L23-L26'><strong>无限循环风险</strong></a> `while True` 轮询等待 bot 上线没有设置超时上限。如果 bot 因配置错误等原因永远无法上线,该任务将无限循环且每 15 秒轮询一次,永远不会退出。建议增加最大重试次数或总超时时间,并在超时后记录日志退出。 </summary> ```python while True: if len(driver.bots) >= 1: break await asyncio.sleep(15) ``` </details> <details><summary><a href='https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/startup-notification/konabot/plugins/startup_notify.py#L32-L32'><strong>任务引用丢失</strong></a> `asyncio.create_task(task())` 的返回值未被保存到任何变量中。Python 文档明确指出,如果不持有对 task 的引用,它可能会被垃圾回收器在完成前回收,导致任务静默消失。应将返回值赋给一个模块级变量或集合以保持强引用。 </summary> ```python asyncio.create_task(task()) ``` </details> <details><summary><a href='https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/startup-notification/konabot/plugins/startup_notify.py#L30-L30'><strong>异常处理缺失</strong></a> `task()` 内部的 `broadcast` 调用如果抛出异常,由于没有 try/except 包裹,异常只会作为 "Task exception was never retrieved" 被静默忽略(尤其在 task 引用丢失的情况下)。建议在 task 内部添加异常捕获和日志记录。 </summary> ```python await broadcast(CHANNEL_STARTUP, UniMessage.text("此方 BOT 重启好了")) ``` </details> </td></tr> </table>
Collaborator

PR Code Suggestions

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
无限等待循环缺少超时机制

这个 while True 循环没有超时机制。如果 bot 永远无法连接(例如配置错误),此任务将无限循环等待,造成资源泄漏。建议添加最大重试次数或超时限制。

konabot/plugins/startup_notify.py [23-26]

-while True:
+max_retries = 20
+for _ in range(max_retries):
     if len(driver.bots) >= 1:
         break
     await asyncio.sleep(15)
+else:
+    return
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies that the while True loop at lines 23-26 has no timeout or retry limit, which could lead to an infinite loop if the bot never connects. Adding a max retry count is a reasonable defensive improvement. However, this is a startup notification feature — if the bot never connects, the loop just wastes minimal resources (sleeping 15s each iteration), and the broader system likely has bigger problems. It's a valid but moderate-impact improvement.

Low
## PR Code Suggestions ✨ <!-- 7026337 --> Explore these optional code suggestions: <table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=1>Possible issue</td> <td> <details><summary>无限等待循环缺少超时机制</summary> ___ **这个 `while True` 循环没有超时机制。如果 bot 永远无法连接(例如配置错误),此任务将无限循环等待,造成资源泄漏。建议添加最大重试次数或超时限制。** [konabot/plugins/startup_notify.py [23-26]](https://gitea.service.jazzwhom.top/mttu-developers/konabot/src/branch/feature/startup-notification/konabot/plugins/startup_notify.py#L23-L26) ```diff -while True: +max_retries = 20 +for _ in range(max_retries): if len(driver.bots) >= 1: break await asyncio.sleep(15) +else: + return ``` <details><summary>Suggestion importance[1-10]: 5</summary> __ Why: The suggestion correctly identifies that the `while True` loop at lines 23-26 has no timeout or retry limit, which could lead to an infinite loop if the bot never connects. Adding a max retry count is a reasonable defensive improvement. However, this is a startup notification feature — if the bot never connects, the loop just wastes minimal resources (sleeping 15s each iteration), and the broader system likely has bigger problems. It's a valid but moderate-impact improvement. </details></details></td><td align=center>Low </td></tr></tr></tbody></table>
Passthem merged commit c66576e12b into master 2026-02-25 16:13:01 +08:00
Passthem deleted branch feature/startup-notification 2026-02-25 16:13:02 +08:00
Sign in to join this conversation.
No description provided.