feat: handle bot_offline notice event in OneBot adapter#623
Conversation
Co-authored-by: sj817 <74231782+sj817@users.noreply.github.com> Agent-Logs-Url: https://github.com/KarinJS/Karin/sessions/dd7fb81d-28c8-46f9-8fac-d8a1a815d910
审阅者指南(Reviewer's Guide)为 OneBot 适配器、核心事件类型和事件流水线增加对 OneBot 处理 OneBot bot_offline 通知的时序图sequenceDiagram
actor OneBotServer
participant AdapterOneBot
participant createNotice
participant createBotOfflineNotice
participant friendNoticeHandler
participant PluginSystem
OneBotServer->>AdapterOneBot: BotOfflineNoticeEvent (notice_type = bot_offline)
AdapterOneBot->>createNotice: createNotice(event, bot)
createNotice->>createNotice: time = new Date(event.time * 1000)
createNotice->>createBotOfflineNotice: BotOfflineOptions
activate createBotOfflineNotice
createBotOfflineNotice->>createBotOfflineNotice: new BotOfflineNotice(options)
createBotOfflineNotice->>friendNoticeHandler: friendNoticeHandler(BotOfflineNotice)
deactivate createBotOfflineNotice
friendNoticeHandler->>PluginSystem: emit notice.botOffline
PluginSystem->>PluginSystem: invoke handlers(ctx)
PluginSystem-->>PluginSystem: ctx.tips = Bot下线: ctx.content.tag
PluginSystem-->>PluginSystem: logger.warn(Bot offline info)
BotOffline 通知流水线的更新类图classDiagram
class NoticeBase {
<<abstract>>
}
class BotOfflineNotice {
- "botOffline" #subEvent
- Contact_friend_ #contact
- Sender_friend_ #sender
+ BotOfflineType content
+ BotOfflineNotice(options BotOfflineOptions)
+ get subEvent() string
+ get contact() Contact_friend_
+ get sender() Sender_friend_
+ get isPrivate() boolean
+ get isFriend() boolean
+ get isGroup() boolean
+ get isGuild() boolean
+ get isDirect() boolean
+ get isGroupTemp() boolean
}
class BotOfflineType {
+ string tag
+ string message
}
class BotOfflineOptions {
+ Contact_friend_ contact
+ Sender_friend_ sender
+ BotOfflineType content
}
class FriendNoticeEventMap {
+ BotOfflineNotice notice_botOffline
}
class Notice {
+ BotOfflineNotice botOfflineVariant
}
NoticeBase <|-- BotOfflineNotice
BotOfflineOptions o--> BotOfflineType
BotOfflineNotice o--> BotOfflineOptions
FriendNoticeEventMap --> BotOfflineNotice
Notice --> BotOfflineNotice
文件级改动
与关联 issue 的对照评估
可能关联的 issues
Tips and commands与 Sourcery 交互
自定义你的体验前往你的 dashboard 可以:
获取帮助Original review guide in EnglishReviewer's GuideAdds first-class support for OneBot Sequence diagram for handling OneBot bot_offline noticesequenceDiagram
actor OneBotServer
participant AdapterOneBot
participant createNotice
participant createBotOfflineNotice
participant friendNoticeHandler
participant PluginSystem
OneBotServer->>AdapterOneBot: BotOfflineNoticeEvent (notice_type = bot_offline)
AdapterOneBot->>createNotice: createNotice(event, bot)
createNotice->>createNotice: time = new Date(event.time * 1000)
createNotice->>createBotOfflineNotice: BotOfflineOptions
activate createBotOfflineNotice
createBotOfflineNotice->>createBotOfflineNotice: new BotOfflineNotice(options)
createBotOfflineNotice->>friendNoticeHandler: friendNoticeHandler(BotOfflineNotice)
deactivate createBotOfflineNotice
friendNoticeHandler->>PluginSystem: emit notice.botOffline
PluginSystem->>PluginSystem: invoke handlers(ctx)
PluginSystem-->>PluginSystem: ctx.tips = Bot下线: ctx.content.tag
PluginSystem-->>PluginSystem: logger.warn(Bot offline info)
Updated class diagram for BotOffline notice pipelineclassDiagram
class NoticeBase {
<<abstract>>
}
class BotOfflineNotice {
- "botOffline" #subEvent
- Contact_friend_ #contact
- Sender_friend_ #sender
+ BotOfflineType content
+ BotOfflineNotice(options BotOfflineOptions)
+ get subEvent() string
+ get contact() Contact_friend_
+ get sender() Sender_friend_
+ get isPrivate() boolean
+ get isFriend() boolean
+ get isGroup() boolean
+ get isGuild() boolean
+ get isDirect() boolean
+ get isGroupTemp() boolean
}
class BotOfflineType {
+ string tag
+ string message
}
class BotOfflineOptions {
+ Contact_friend_ contact
+ Sender_friend_ sender
+ BotOfflineType content
}
class FriendNoticeEventMap {
+ BotOfflineNotice notice_botOffline
}
class Notice {
+ BotOfflineNotice botOfflineVariant
}
NoticeBase <|-- BotOfflineNotice
BotOfflineOptions o--> BotOfflineType
BotOfflineNotice o--> BotOfflineOptions
FriendNoticeEventMap --> BotOfflineNotice
Notice --> BotOfflineNotice
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - 我发现了 1 个问题
给 AI 代理的提示
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="packages/core/src/event/notice.ts" line_range="1278-1279" />
<code_context>
+ #sender: BotOfflineOptions['sender']
+ content: BotOfflineOptions['content']
+
+ constructor (options: BotOfflineOptions) {
+ super(Object.assign(options, { subEvent: 'botOffline' as const }))
+
+ this.#subEvent = 'botOffline'
</code_context>
<issue_to_address>
**issue (bug_risk):** 在调用 `super` 之前设置 `subEvent` 时,应避免对 `options` 对象进行修改。
`Object.assign(options, { subEvent: 'botOffline' as const })` 会改变原始的 `options` 对象,如果该实例在其他地方被复用或检查,可能会导致一些隐蔽的 bug。应改为向 `super` 传入一个新对象,例如 `super({ ...options, subEvent: 'botOffline' as const })` 或 `super(Object.assign({}, options, { subEvent: 'botOffline' as const }))`,从而保证调用方传入的 `options` 保持不变。
</issue_to_address>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的代码评审。
Original comment in English
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="packages/core/src/event/notice.ts" line_range="1278-1279" />
<code_context>
+ #sender: BotOfflineOptions['sender']
+ content: BotOfflineOptions['content']
+
+ constructor (options: BotOfflineOptions) {
+ super(Object.assign(options, { subEvent: 'botOffline' as const }))
+
+ this.#subEvent = 'botOffline'
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid mutating the `options` object when setting `subEvent` before calling `super`.
`Object.assign(options, { subEvent: 'botOffline' as const })` mutates the original `options` object, which can cause subtle bugs if that instance is reused or inspected elsewhere. Instead, pass a new object to `super`, e.g. `super({ ...options, subEvent: 'botOffline' as const })` or `super(Object.assign({}, options, { subEvent: 'botOffline' as const }))`, so the caller’s `options` remains unchanged.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
Adds first-class handling for OneBot bot_offline notice events end-to-end (OneBot adapter typings → core event types → event creation/dispatch → adapter routing), eliminating the “unknown event” warning and enabling plugins to subscribe to notice.botOffline.
Changes:
- Added
NoticeType.BotOffline = 'bot_offline'andBotOfflineNoticeEventto the OneBot notice event union. - Added
BotOfflineType,botOfflinesub-event typing,BotOfflineOptions, and wirednotice.botOfflineinto the core notice event map/union. - Implemented
BotOfflineNotice+ factory + handler routing, plus tips initialization and OneBot adapter branch to create the event.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/onebot/src/event/notice.ts | Adds OneBot notice enum + event interface for bot_offline. |
| packages/core/src/types/event/content.ts | Introduces BotOfflineType payload (tag, message). |
| packages/core/src/types/event/options.ts | Adds botOffline to NoticeEventSub and defines BotOfflineOptions. |
| packages/core/src/types/event/event.ts | Wires notice.botOffline into the event map and Notice union. |
| packages/core/src/event/notice.ts | Implements BotOfflineNotice event class. |
| packages/core/src/event/create/index.ts | Adds createBotOfflineNotice factory routed via friendNoticeHandler. |
| packages/core/src/event/handler/other/other.ts | Adds initTips formatting for botOffline. |
| packages/core/src/adapter/onebot/create/notice.ts | Routes OneBot bot_offline to createBotOfflineNotice. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…in bot_offline handler Co-authored-by: sj817 <74231782+sj817@users.noreply.github.com> Agent-Logs-Url: https://github.com/KarinJS/Karin/sessions/34c587bf-3892-4a64-8b9f-60637af418ec
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
你可以通过以下命令安装该版本: |
The OneBot adapter was logging
[AdapterOneBot] 收到未知事件whenever it received abot_offlinenotice (e.g., account forcibly logged out). This adds full first-class support for the event across the type system, event pipeline, and adapter.Changes
packages/onebotNoticeType.BotOffline = 'bot_offline'enum entry +BotOfflineNoticeEventinterface added toOneBotNoticeEventunionpackages/core— typesBotOfflineTypecontent interface (tag,message)'botOffline'added toNoticeEventSubunion;BotOfflineOptionstype added (usesContact<'friend'>/Sender<'friend'>— no group context exists for this event)BotOfflineNoticeadded toFriendNoticeEventMapas'notice.botOffline'and to theNoticeunionpackages/core— event pipelineBotOfflineNoticeclass extendingNoticeBasecreateBotOfflineNoticefactory routing throughfriendNoticeHandlerinitTipscase:Bot下线: <tag>packages/core— adapter handlerNoticeType.BotOfflinebranch increateNoticeusingbot.selfIdas contact/senderPlugins can now subscribe to
'notice.botOffline'events:Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
registry.npmmirror.com/home/REDACTED/work/_temp/ghcca-node/node/bin/node node /home/REDACTED/.npm/_npx/4876f32048baf8ac/node_modules/.bin/prebuild-install -r node --pkg_version=0.13.1 --pkg_name=node-pty-prebuilt-multiarch ame git(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Summary by Sourcery
在整个类型系统和事件管道中,为 OneBot 机器人离线通知事件添加一等支持。
新功能:
notice.botOffline事件。改进:
initTips处理逻辑,为机器人离线事件提供可读性良好的人类提示信息。bot_offline事件视为未知事件。Original summary in English
Summary by Sourcery
Add first-class support for OneBot bot offline notice events across the type system and event pipeline.
New Features:
Enhancements: