AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

feat: 统一所有模块获取时间操作, 增加Time模块统一管理时间相关操作, 在StarTools增加TimeTools模块, 提供接口以便使用和Core相同的时间(而不用维护一个时区设置)

Open anka-afk opened this issue 1 year ago • 10 comments

Motivation

  1. 尽管提供了时区配置, 日志和消息的记录等操作没有遵循时区设置
  2. 插件如果需要使用时间相关信息, 需要维护一个时区配置, 应当开放接口统一时间

Modifications

  1. 增加Time模块, 将datetime等模块封装, 简化导入, 并应用时区设置
  2. 为StarTools增加TimeTools模块, 提供时间获取等接口(应用时区设置)
  3. 修改两个命名: updator->updater

Check

  • [ ] 我的 Commit Message 符合良好的规范 提交的时候才发现有这个规范, 这个commit message改起来太难了😭拼尽全力无法战胜
  • [x] 我新增/修复/优化的功能经过良好的测试

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

Sourcery 总结

通过引入一个中心化的 Time 模块,统一各个模块中与时间相关的操作,从而提供一致的时区管理和时间相关实用程序。

新特性:

  • 引入了一个全面的 Time 模块,用于集中管理与时间相关的操作和时区。

增强功能:

  • 在 StarTools 中添加了 TimeTools,以提供一致的与时间相关的接口。
  • 重构了与时间相关的代码,以便在不同的组件中使用新的 Time 模块。

杂项:

  • 在多个位置将 'updator' 重命名为 'updater'。
Original summary in English

Summary by Sourcery

Unify time-related operations across modules by introducing a centralized Time module, providing consistent timezone management and time-related utilities

New Features:

  • Introduced a comprehensive Time module to centralize time-related operations and timezone management

Enhancements:

  • Added TimeTools to StarTools to provide consistent time-related interfaces
  • Refactored time-related code to use the new Time module across different components

Chores:

  • Renamed 'updator' to 'updater' in multiple locations

anka-afk avatar Apr 15 '25 09:04 anka-afk

## Sourcery 提供的审查者指南

此拉取请求引入了一个 `Time` 模块,用于在整个应用程序中进行统一的时间管理,确保一致的时区处理。它还通过将 `astrbot_updator` 重命名为 `astrbot_updater` 和 `PluginUpdator` 重命名为 `PluginUpdater` 来纠正了一个拼写错误。

_未生成图表,因为更改看起来很简单,不需要可视化表示。_

### 文件级别更改

| 变更 | 详情 | 文件 |
| ------ | ------- | ----- |
| 引入了一个 `Time` 模块来封装与时间相关的操作,提供了一个统一的接口来访问和操作时间,并确保与时区设置的一致性。 | <ul><li>创建了一个 `Time` 类,其中包含用于获取当前时间、时间戳、格式化日期时间、解析日期时间字符串和测量时间差的方法。</li><li>`Time` 类在内部使用 `datetime` 和 `zoneinfo` 模块。</li><li>添加了以不同格式(时间戳、datetime 对象、格式化字符串)获取当前时间的方法。</li><li>添加了解析时间字符串和格式化 datetime 对象的方法。</li><li>添加了测量两个点之间的时间差的方法。</li><li>添加了用于同步和异步休眠的方法。</li></ul> | `astrbot/core/__init__.py`<br/>`astrbot/core/time.py` |
| 通过添加 `TimeTools` 类将 `Time` 模块集成到 `StarTools` 中,为插件提供了一种使用配置的时区访问与时间相关的信息的一致方式。 | <ul><li>在 `StarTools` 中添加了一个 `TimeTools` 类。</li><li>`TimeTools` 类提供了用于获取当前时间、时间戳、格式化日期时间、解析日期时间字符串和测量时间差的方法,所有这些都使用 `Time` 模块。</li><li>`TimeTools` 方法委托给 `Time` 模块中的相应方法。</li></ul> | `astrbot/core/star/star_tools.py` |
| 在整个代码库中,用新的 `Time` 模块替换了对 `datetime` 和 `time` 模块的直接调用,以确保一致的时区处理。 | <ul><li>将 `datetime.datetime.now()` 替换为 `Time.now()`。</li><li>将 `time.time()` 替换为 `Time.time()` 或 `Time.timestamp()`。</li><li>将 `datetime.datetime.fromtimestamp()` 替换为 `Time.fromtimestamp()`。</li><li>将 `datetime.datetime.strptime()` 替换为 `Time.parse_datetime()`。</li><li>将 `datetime.timedelta()` 替换为 `Time.timedelta()`。</li><li>将 `asyncio.sleep()` 替换为 `Time.async_sleep()`。</li></ul> | `packages/astrbot/main.py`<br/>`packages/reminder/main.py`<br/>`astrbot/core/db/sqlite.py`<br/>`astrbot/core/pipeline/rate_limit_check/stage.py`<br/>`astrbot/core/platform/sources/telegram/tg_event.py`<br/>`astrbot/core/utils/io.py`<br/>`astrbot/core/core_lifecycle.py`<br/>`astrbot/core/log.py`<br/>`astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py`<br/>`astrbot/core/platform/sources/gewechat/client.py`<br/>`astrbot/core/updater.py`<br/>`astrbot/dashboard/routes/stat.py`<br/>`astrbot/core/platform/astrbot_message.py`<br/>`astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py`<br/>`astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py`<br/>`astrbot/core/platform/sources/webchat/webchat_adapter.py`<br/>`astrbot/core/provider/sources/sensevoice_selfhosted_source.py`<br/>`astrbot/core/utils/session_waiter.py`<br/>`astrbot/dashboard/routes/auth.py` |
| 通过将 `astrbot_updator` 重命名为 `astrbot_updater` 来纠正了一个拼写错误。 | <ul><li>在 `astrbot/core/core_lifecycle.py` 中将 `astrbot_updator` 属性重命名为 `astrbot_updater`。</li><li>在 `astrbot/dashboard/routes/update.py` 中将 `astrbot_updator` 属性重命名为 `astrbot_updater`。</li><li>在 `astrbot/dashboard/server.py` 中将 `astrbot_updator` 属性重命名为 `astrbot_updater`。</li><li>在 `tests/test_dashboard.py` 中将 `astrbot_updator` 属性重命名为 `astrbot_updater`。</li><li>更新了变更日志以反映重命名。</li></ul> | `astrbot/core/core_lifecycle.py`<br/>`astrbot/dashboard/routes/update.py`<br/>`astrbot/dashboard/server.py`<br/>`tests/test_dashboard.py`<br/>`changelogs/v3.4.13.md` |
| 将 `PluginUpdator` 重命名为 `PluginUpdater`。 | <ul><li>在 `astrbot/core/star/star_manager.py` 中将 `PluginUpdator` 类重命名为 `PluginUpdater`。</li><li>在 `astrbot/core/star/updater.py` 中将 `PluginUpdator` 类重命名为 `PluginUpdater`。</li></ul> | `astrbot/core/star/star_manager.py`<br/>`astrbot/core/star/updater.py` |

---

<details>
<summary>提示和命令</summary>

#### 与 Sourcery 互动

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

#### 自定义您的体验

访问您的 [仪表板](https://app.sourcery.ai) 以:
- 启用或禁用审查功能,例如 Sourcery 生成的拉取请求摘要、审查者指南等。
- 更改审查语言。
- 添加、删除或编辑自定义审查说明。
- 调整其他审查设置。

#### 获得帮助

- [联系我们的支持团队](mailto:[email protected]) 提出问题或反馈。
- 访问我们的 [文档](https://docs.sourcery.ai) 获取详细指南和信息。
- 通过在 [X/Twitter](https://x.com/SourceryAI)、[LinkedIn](https://www.linkedin.com/company/sourcery-ai/) 或 [GitHub](https://github.com/sourcery-ai) 上关注我们,与 Sourcery 团队保持联系。

</details>
Original review guide in English

Reviewer's Guide by Sourcery

This pull request introduces a Time module for unified time management across the application, ensuring consistent timezone handling. It also corrects a typo by renaming astrbot_updator to astrbot_updater and PluginUpdator to PluginUpdater.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Introduced a Time module to encapsulate time-related operations, providing a unified interface for accessing and manipulating time, and ensuring consistency with timezone settings.
  • Created a Time class with methods for getting the current time, timestamp, formatting datetimes, parsing datetime strings, and measuring time differences.
  • The Time class uses the datetime and zoneinfo modules internally.
  • Added methods to get the current time in different formats (timestamp, datetime object, formatted string).
  • Added methods to parse time strings and format datetime objects.
  • Added methods to measure time differences between two points.
  • Added methods for sleeping synchronously and asynchronously.
astrbot/core/__init__.py
astrbot/core/time.py
Integrated the Time module into StarTools by adding a TimeTools class, providing plugins with a consistent way to access time-related information using the configured timezone.
  • Added a TimeTools class within StarTools.
  • The TimeTools class provides methods for getting the current time, timestamp, formatting datetimes, parsing datetime strings, and measuring time differences, all using the Time module.
  • The TimeTools methods delegate to the corresponding methods in the Time module.
astrbot/core/star/star_tools.py
Replaced direct calls to datetime and time modules with the new Time module throughout the codebase to ensure consistent timezone handling.
  • Replaced datetime.datetime.now() with Time.now().
  • Replaced time.time() with Time.time() or Time.timestamp().
  • Replaced datetime.datetime.fromtimestamp() with Time.fromtimestamp().
  • Replaced datetime.datetime.strptime() with Time.parse_datetime().
  • Replaced datetime.timedelta() with Time.timedelta().
  • Replaced asyncio.sleep() with Time.async_sleep().
packages/astrbot/main.py
packages/reminder/main.py
astrbot/core/db/sqlite.py
astrbot/core/pipeline/rate_limit_check/stage.py
astrbot/core/platform/sources/telegram/tg_event.py
astrbot/core/utils/io.py
astrbot/core/core_lifecycle.py
astrbot/core/log.py
astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py
astrbot/core/platform/sources/gewechat/client.py
astrbot/core/updater.py
astrbot/dashboard/routes/stat.py
astrbot/core/platform/astrbot_message.py
astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py
astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py
astrbot/core/platform/sources/webchat/webchat_adapter.py
astrbot/core/provider/sources/sensevoice_selfhosted_source.py
astrbot/core/utils/session_waiter.py
astrbot/dashboard/routes/auth.py
Corrected a typo by renaming astrbot_updator to astrbot_updater.
  • Renamed the astrbot_updator attribute to astrbot_updater in astrbot/core/core_lifecycle.py.
  • Renamed the astrbot_updator attribute to astrbot_updater in astrbot/dashboard/routes/update.py.
  • Renamed the astrbot_updator attribute to astrbot_updater in astrbot/dashboard/server.py.
  • Renamed the astrbot_updator attribute to astrbot_updater in tests/test_dashboard.py.
  • Updated the changelog to reflect the renaming.
astrbot/core/core_lifecycle.py
astrbot/dashboard/routes/update.py
astrbot/dashboard/server.py
tests/test_dashboard.py
changelogs/v3.4.13.md
Renamed PluginUpdator to PluginUpdater.
  • Renamed the PluginUpdator class to PluginUpdater in astrbot/core/star/star_manager.py.
  • Renamed the PluginUpdator class to PluginUpdater in astrbot/core/star/updater.py.
astrbot/core/star/star_manager.py
astrbot/core/star/updater.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

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Apr 15 '25 09:04 sourcery-ai[bot]

另外感觉StarTools和Time做的有点太多了(?)

至少Time模块做的个人感觉功能有点多,个人感觉只让这个模块返回一个datetime对象会更好一点,不然这两个模块的重复度感觉就有点高。

以上仅为个人建议,仅供参考,现在的代码其实也能用。

Raven95676 avatar Apr 15 '25 13:04 Raven95676

另外感觉StarTools和Time做的有点太多了(?)

至少Time模块做的个人感觉功能有点多,个人感觉只让这个模块返回一个datetime对象会更好一点,不然这两个模块的重复度感觉就有点高。

以上仅为个人建议,仅供参考,现在的代码其实也能用。

我想的是给的Tools越详细越好的, 这样上手难度会比较低, 然后不需要让插件去调用AstrBot内部的一些模块, 把所有插件开发用到的相关模块全部塞进去, 然后AstrBot本体不会去使用这个模块里面的东西(里面的东西全部都是专门给插件开发使用的)

anka-afk avatar Apr 17 '25 14:04 anka-afk

另外感觉StarTools和Time做的有点太多了(?) 至少Time模块做的个人感觉功能有点多,个人感觉只让这个模块返回一个datetime对象会更好一点,不然这两个模块的重复度感觉就有点高。 以上仅为个人建议,仅供参考,现在的代码其实也能用。

我想的是给的Tools越详细越好的, 这样上手难度会比较低, 然后不需要让插件去调用AstrBot内部的一些模块, 把所有插件开发用到的相关模块全部塞进去, 然后AstrBot本体不会去使用这个模块里面的东西(里面的东西全部都是专门给插件开发使用的)

有一说一在Time模块中加入更多功能确实能简化框架的部分调用。但以我个人风格来讲,更倾向于将更多功能封装到StarTools中,方便插件作者使用,而框架本身只调用一个简单返回datetime对象的Time模块,这样就可以避免框架里面出现功能/代码重复的两个不同用途的模块。或者把功能全放在Time模块,StarTools的TimeTools仅作为代理。

另外,感觉把Time更名为UnionTime(或者短一点,UniTime)表意更加清晰些?只改动一个大小写有的时候还是有一种在调用time的错觉(

不过刨除这些在风格上的意见不一致,剩下的就大概没什么问题了。

Raven95676 avatar Apr 17 '25 16:04 Raven95676

@Soulter 有时间来看看吗😿

anka-afk avatar Apr 20 '25 14:04 anka-afk

我认为时间戳不应该加上时区偏移,它本质上代表从 Unix 纪元到现在的(毫)秒数。如果要加上时区也应该在被解释成人类可读时间时做转换。

Soulter avatar Apr 22 '25 16:04 Soulter

我认为时间戳不应该加上时区偏移,它本质上代表从 Unix 纪元到现在的(毫)秒数。如果要加上时区也应该在被解释成人类可读时间时做转换。

并且现在存储层已经统一使用 UTC 时间戳,如果现在更换时间戳的定义,会引入破坏性更新。

Soulter avatar Apr 22 '25 16:04 Soulter

此外,我认为统一成一个 Time 类的设计非常好

然后,为了保证前向兼容,建议保留 updator 包以及 AstrBotUpdator 类。(也就是,对于包来说,可以保留 updator.py 但是内部引用都引用 updater.py 的,然后 AstrBotUpdator 类作为 AstrBotUpdater 类的别名,参考 CommandResult)

Soulter avatar Apr 22 '25 16:04 Soulter

我认为时间戳不应该加上时区偏移,它本质上代表从 Unix 纪元到现在的(毫)秒数。如果要加上时区也应该在被解释成人类可读时间时做转换。

并且现在存储层已经统一使用 UTC 时间戳,如果现在更换时间戳的定义,会引入破坏性更新。

嗯, 所有涉及时间戳的地方都统一使用了UTC时间戳, 在需要format成字符串的地方才进行时区转换

anka-afk avatar Apr 23 '25 02:04 anka-afk

此外,我认为统一成一个 Time 类的设计非常好

然后,为了保证前向兼容,建议保留 updator 包以及 AstrBotUpdator 类。(也就是,对于包来说,可以保留 updator.py 但是内部引用都引用 updater.py 的,然后 AstrBotUpdator 类作为 AstrBotUpdater 类的别名,参考 CommandResult)

这里也修改了, 但是感觉有点不太美观(

anka-afk avatar Apr 23 '25 02:04 anka-afk