Skip to content

为缺少session_id字段的aiocqhttp消息提供一个默认值,防止报错 #1328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Zhalslar
Copy link

@Zhalslar Zhalslar commented Apr 19, 2025

修复了aiocqhttp消息缺少session_id字段时导致报错

Motivation

aiocqhttp的群邀请事件和加好友事件在构造成AiocqhttpMessageEvent时缺少session_id字段,导致报错,影响我写的插件

Modifications

在构造AiocqhttpMessageEvent时,若session_id不存在则提供一个默认值来使用:
async def handle_msg(self, message: AstrBotMessage):
message_event = AiocqhttpMessageEvent(
message_str=message.message_str,
message_obj=message,
platform_meta=self.meta(),
session_id=getattr(message, "session_id", "114514"),
bot=self.bot,
)

Check

  • 我的 Commit Message 符合良好的规范
  • 我新增/修复/优化的功能经过良好的测试

好的,这是翻译成中文的 pull request 摘要:

Sourcery 总结

Bug 修复:

  • 修复了 aiocqhttp 中由于缺少 session_id 字段而导致群邀请和好友请求事件出错的问题,通过添加默认值 '114514' 解决。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Fixed an issue where group invite and friend request events in aiocqhttp were causing errors due to missing session_id field by adding a default value of '114514'

Copy link

sourcery-ai bot commented Apr 19, 2025

## Sourcery 评审者指南

此 pull request 解决了一个问题,即从 aiocqhttp 群邀请和好友请求事件构建 `AiocqhttpMessageEvent` 实例时,缺少 `session_id` 字段,从而导致错误。 为了解决这个问题,现在为 `session_id` 提供了一个默认值,如果消息对象中缺少该值。

#### `AiocqhttpMessageEvent` 的更新类图

```mermaid
classDiagram
  class AiocqhttpMessageEvent{
    message_str: str
    message_obj: AstrBotMessage
    platform_meta: dict
    session_id: str
    bot: Any
    + __init__(message_str: str, message_obj: AstrBotMessage, platform_meta: dict, session_id: str, bot: Any)
  }
  class AstrBotMessage{
  }
  AiocqhttpMessageEvent -- AstrBotMessage : message_obj

文件级别变更

变更 详情 文件
实现了 AiocqhttpMessageEventsession_id 字段的默认值,当消息中缺少该字段时。
  • 使用 getattr 安全地访问消息对象的 session_id 属性,如果未找到该属性,则提供默认值 "114514"。
astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.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 pull request addresses an issue where AiocqhttpMessageEvent instances were missing the session_id field when constructed from aiocqhttp group invite and friend request events, leading to errors. To resolve this, a default value is now provided for session_id if it's missing from the message object.

Updated class diagram for AiocqhttpMessageEvent

classDiagram
  class AiocqhttpMessageEvent{
    message_str: str
    message_obj: AstrBotMessage
    platform_meta: dict
    session_id: str
    bot: Any
    + __init__(message_str: str, message_obj: AstrBotMessage, platform_meta: dict, session_id: str, bot: Any)
  }
  class AstrBotMessage{
  }
  AiocqhttpMessageEvent -- AstrBotMessage : message_obj
Loading

File-Level Changes

Change Details Files
Implemented a default value for the session_id field in AiocqhttpMessageEvent when it is missing from the message.
  • Used getattr to safely access the session_id attribute of the message object, providing a default value of "114514" if the attribute is not found.
astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.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.

@Zhalslar - 我已经查看了你的更改 - 这里有一些反馈:

总体评论

  • 考虑使用 message.session_id if hasattr(message, 'session_id') else '114514' 以获得更好的可读性。
以下是我在审查期间查看的内容
  • 🟢 一般问题:一切看起来都很好
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery 对于开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @Zhalslar - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using message.session_id if hasattr(message, 'session_id') else '114514' for better readability.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 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.

@@ -320,7 +320,7 @@ async def handle_msg(self, message: AstrBotMessage):
message_str=message.message_str,
message_obj=message,
platform_meta=self.meta(),
session_id=message.session_id,
session_id=getattr(message, "session_id", "114514"),
Copy link
Member

Choose a reason for hiding this comment

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

不建议在这里进行 workaround 处理。可以试试直接修改 _convert_handle_request_event()。看了下是由于这个方法导致的所提到的报错。

@Soulter
Copy link
Member

Soulter commented Apr 22, 2025

@Zhalslar

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