Skip to content

Commit 75926c0

Browse files
committed
feat: 多实例模式增强:支持 ctx.pickBot(id) 获取指定机器人实例,支持 ctx.actions.xxx 调用需要手动传入 bot 实例的方法
1 parent e0387cd commit 75926c0

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

packages/mioki/src/plugin.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,23 @@ export const deduplicator = new Deduplicator()
215215
* Mioki 上下文对象,包含 Mioki 运行时的信息和方法
216216
*/
217217
export interface MiokiContext extends Services, Configs, Utils, RemoveBotParam<Actions> {
218-
/** 当前处理事件的机器人实例 */
218+
/**
219+
* 单实例模式下:机器人实例
220+
* 多实例模式下:第一个机器人实例
221+
*
222+
* 如果要获取指定机器人实例,请使用 ctx.getBot(id) 方法,id 通常可以从 event.self_id 获取
223+
*/
219224
bot: NapCat
220-
/** 所有已连接的机器人实例列表 */
221-
bots: ExtendedNapCat[]
222225
/** 当前机器人 QQ 号 */
223226
self_id: number
227+
228+
/** 多实例模式下:所有已连接的机器人实例列表 */
229+
bots: ExtendedNapCat[]
230+
/** 多实例模式下:通过 QQ 号获取机器人实例 */
231+
pickBot: (id: number) => ExtendedNapCat
232+
/** 多实例模式下:需要手动传入 bot 实例的操作方法集合*/
233+
actions: Actions
234+
224235
/** 消息构造器 */
225236
segment: NapCat['segment']
226237
/** 通过域名获取 Cookies */
@@ -398,13 +409,15 @@ export async function enablePlugin(
398409
const createContext = (bot: ExtendedNapCat): MiokiContext => {
399410
return {
400411
bot,
401-
bots,
402412
self_id: bot.bot_id,
413+
bots,
414+
pickBot: (id: number) => bots.find((b) => b.bot_id === id)!,
403415
segment: bot.segment,
404416
getCookie: bot.getCookie.bind(bot),
405417
...utilsExports,
406418
...configExports,
407419
...buildRemovedActions(bot),
420+
actions: actionsExports,
408421
logger,
409422
services: servicesExports.services,
410423
clears: userClears,

packages/mioki/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "../../tsconfig.json"
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node"]
5+
}
36
}

packages/napcat-sdk/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "../../tsconfig.json"
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node"]
5+
}
36
}

0 commit comments

Comments
 (0)