node-karin 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +57 -0
- package/config/defSet/App.yaml +37 -0
- package/config/defSet/config.yaml +43 -0
- package/config/defSet/group.yaml +18 -0
- package/config/defSet/pm2.yaml +21 -0
- package/config/defSet/redis.yaml +18 -0
- package/config/defSet/server.yaml +42 -0
- package/config/view/App.yaml +74 -0
- package/config/view/config.yaml +100 -0
- package/config/view/group.yaml +62 -0
- package/config/view/pm2.yaml +41 -0
- package/config/view/redis.yaml +25 -0
- package/config/view/server.yaml +93 -0
- package/lib/adapter/onebot/onebot11.d.ts +430 -0
- package/lib/adapter/onebot/onebot11.js +1302 -0
- package/lib/core/init.d.ts +0 -0
- package/lib/core/init.js +4 -0
- package/lib/core/karin.d.ts +72 -0
- package/lib/core/karin.js +51 -0
- package/lib/core/listener.d.ts +121 -0
- package/lib/core/listener.js +188 -0
- package/lib/core/plugin.app.d.ts +15 -0
- package/lib/core/plugin.app.js +18 -0
- package/lib/core/plugin.d.ts +182 -0
- package/lib/core/plugin.js +138 -0
- package/lib/core/plugin.loader.d.ts +149 -0
- package/lib/core/plugin.loader.js +462 -0
- package/lib/core/server.d.ts +26 -0
- package/lib/core/server.js +213 -0
- package/lib/db/level.d.ts +20 -0
- package/lib/db/level.js +38 -0
- package/lib/db/redis.d.ts +41 -0
- package/lib/db/redis.js +137 -0
- package/lib/db/redis_level.d.ts +113 -0
- package/lib/db/redis_level.js +290 -0
- package/lib/event/event.d.ts +138 -0
- package/lib/event/event.handler.d.ts +29 -0
- package/lib/event/event.handler.js +142 -0
- package/lib/event/event.js +120 -0
- package/lib/event/message.d.ts +102 -0
- package/lib/event/message.handler.d.ts +25 -0
- package/lib/event/message.handler.js +240 -0
- package/lib/event/message.js +70 -0
- package/lib/event/notice.d.ts +49 -0
- package/lib/event/notice.js +15 -0
- package/lib/event/request.d.ts +49 -0
- package/lib/event/request.js +15 -0
- package/lib/event/review.handler.d.ts +54 -0
- package/lib/event/review.handler.js +382 -0
- package/lib/index.d.ts +23 -0
- package/lib/index.js +40 -0
- package/lib/renderer/app.d.ts +53 -0
- package/lib/renderer/app.js +93 -0
- package/lib/renderer/base.d.ts +30 -0
- package/lib/renderer/base.js +71 -0
- package/lib/renderer/client.d.ts +30 -0
- package/lib/renderer/client.js +159 -0
- package/lib/renderer/http.d.ts +19 -0
- package/lib/renderer/http.js +51 -0
- package/lib/renderer/server.d.ts +42 -0
- package/lib/renderer/server.js +112 -0
- package/lib/renderer/wormhole.d.ts +1 -0
- package/lib/renderer/wormhole.js +154 -0
- package/lib/types/adapter.d.ts +575 -0
- package/lib/types/adapter.js +1 -0
- package/lib/types/config.d.ts +327 -0
- package/lib/types/config.js +1 -0
- package/lib/types/element.d.ts +576 -0
- package/lib/types/element.js +1 -0
- package/lib/types/index.d.ts +8 -0
- package/lib/types/index.js +8 -0
- package/lib/types/logger.d.ts +109 -0
- package/lib/types/logger.js +1 -0
- package/lib/types/onebots11.d.ts +1371 -0
- package/lib/types/onebots11.js +1 -0
- package/lib/types/plugin.d.ts +282 -0
- package/lib/types/plugin.js +1 -0
- package/lib/types/render.d.ts +111 -0
- package/lib/types/render.js +1 -0
- package/lib/types/reply.d.ts +40 -0
- package/lib/types/reply.js +1 -0
- package/lib/types/types.d.ts +898 -0
- package/lib/types/types.js +1 -0
- package/lib/utils/YamlEditor.d.ts +62 -0
- package/lib/utils/YamlEditor.js +208 -0
- package/lib/utils/button.d.ts +49 -0
- package/lib/utils/button.js +79 -0
- package/lib/utils/common.d.ts +123 -0
- package/lib/utils/common.js +413 -0
- package/lib/utils/config.d.ts +72 -0
- package/lib/utils/config.js +254 -0
- package/lib/utils/exec.d.ts +22 -0
- package/lib/utils/exec.js +36 -0
- package/lib/utils/ffmpeg.d.ts +12 -0
- package/lib/utils/ffmpeg.js +25 -0
- package/lib/utils/handler.d.ts +76 -0
- package/lib/utils/handler.js +102 -0
- package/lib/utils/logger.d.ts +3 -0
- package/lib/utils/logger.js +104 -0
- package/lib/utils/segment.d.ts +276 -0
- package/lib/utils/segment.js +448 -0
- package/lib/utils/update.d.ts +69 -0
- package/lib/utils/update.js +151 -0
- package/lib/utils/updateVersion.d.ts +33 -0
- package/lib/utils/updateVersion.js +145 -0
- package/package.json +92 -0
|
File without changes
|
package/lib/core/init.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Permission } from '../types/types.js';
|
|
2
|
+
import { PluginApps } from '../types/plugin.js';
|
|
3
|
+
import { KarinElement } from '../types/element.js';
|
|
4
|
+
import { KarinMessage } from '../event/message.js';
|
|
5
|
+
type FncFunction = (e: KarinMessage) => Promise<boolean>;
|
|
6
|
+
type FncElement = string | KarinElement | Array<KarinElement>;
|
|
7
|
+
export interface OptionsCommand {
|
|
8
|
+
/**
|
|
9
|
+
* - 插件名称 不传则使用 插件名称:函数名
|
|
10
|
+
*/
|
|
11
|
+
name?: string;
|
|
12
|
+
/**
|
|
13
|
+
* - 插件描述
|
|
14
|
+
*/
|
|
15
|
+
desc?: string;
|
|
16
|
+
/**
|
|
17
|
+
* - 插件优先级 数字越小优先级越高
|
|
18
|
+
* @default 10000
|
|
19
|
+
*/
|
|
20
|
+
priority?: number;
|
|
21
|
+
/**
|
|
22
|
+
* - 权限
|
|
23
|
+
* @default 'all'
|
|
24
|
+
*/
|
|
25
|
+
permission?: Permission;
|
|
26
|
+
/**
|
|
27
|
+
* - 打印日志
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
log?: boolean | Function;
|
|
31
|
+
}
|
|
32
|
+
export interface OptionsElement extends OptionsCommand {
|
|
33
|
+
/**
|
|
34
|
+
* - 是否加上at 仅在群聊中有效
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
at?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* - 是否加上引用回复
|
|
40
|
+
* @default false
|
|
41
|
+
*/
|
|
42
|
+
reply?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* - 延迟回复 单位毫秒
|
|
45
|
+
*/
|
|
46
|
+
delay?: number;
|
|
47
|
+
/**
|
|
48
|
+
* - 发送是否撤回消息 单位秒
|
|
49
|
+
* @default 0
|
|
50
|
+
*/
|
|
51
|
+
recallMsg?: number;
|
|
52
|
+
/**
|
|
53
|
+
* - 是否停止执行后续插件
|
|
54
|
+
* @default true
|
|
55
|
+
*/
|
|
56
|
+
stop?: boolean;
|
|
57
|
+
}
|
|
58
|
+
export default class Karin {
|
|
59
|
+
/**
|
|
60
|
+
* @param reg - 正则表达式
|
|
61
|
+
* @param fnc - 函数
|
|
62
|
+
* @param options - 选项
|
|
63
|
+
*/
|
|
64
|
+
command(reg: string | RegExp, fnc: FncFunction, options?: OptionsCommand): PluginApps;
|
|
65
|
+
/**
|
|
66
|
+
* @param reg - 正则表达式
|
|
67
|
+
* @param element - 字符串或者KarinElement、KarinElement数组
|
|
68
|
+
* @param options - 选项
|
|
69
|
+
*/
|
|
70
|
+
command(reg: string | RegExp, element: FncElement, options?: OptionsElement): PluginApps;
|
|
71
|
+
}
|
|
72
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import common from '../utils/common.js'
|
|
2
|
+
import PluginApp from './plugin.app.js'
|
|
3
|
+
export default class Karin {
|
|
4
|
+
/**
|
|
5
|
+
* - 快速构建命令
|
|
6
|
+
* @param reg - 正则表达式
|
|
7
|
+
* @param second - 函数或者字符串或者KarinElement、KarinElement数组
|
|
8
|
+
* @param options - 选项
|
|
9
|
+
* @returns - 返回插件对象
|
|
10
|
+
*/
|
|
11
|
+
command (reg, second, options = {}) {
|
|
12
|
+
const Reg = typeof reg === 'string' ? new RegExp(reg) : reg
|
|
13
|
+
const data = {
|
|
14
|
+
name: options.name || 'function',
|
|
15
|
+
priority: options.priority,
|
|
16
|
+
rule: [
|
|
17
|
+
{
|
|
18
|
+
reg: Reg,
|
|
19
|
+
fnc: second,
|
|
20
|
+
permission: options.permission || 'all',
|
|
21
|
+
log: options.log ?? true,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
}
|
|
25
|
+
switch (typeof second) {
|
|
26
|
+
case 'function': {
|
|
27
|
+
return PluginApp(data)
|
|
28
|
+
}
|
|
29
|
+
case 'string':
|
|
30
|
+
case 'number':
|
|
31
|
+
case 'object': {
|
|
32
|
+
const element = common.makeMessage(typeof second === 'function' ? second : String(second))
|
|
33
|
+
const fnc = async (e) => {
|
|
34
|
+
if ('delay' in options && options.delay) { await common.sleep(options.delay) }
|
|
35
|
+
await e.reply(element, {
|
|
36
|
+
at: ('at' in options && options.at) || false,
|
|
37
|
+
reply: ('reply' in options && options.reply) || false,
|
|
38
|
+
recallMsg: ('recallMsg' in options && Number(options.recallMsg)) || 0,
|
|
39
|
+
})
|
|
40
|
+
if ('stop' in options && !options.stop) { return false }
|
|
41
|
+
return true
|
|
42
|
+
}
|
|
43
|
+
data.rule[0].fnc = fnc
|
|
44
|
+
return PluginApp(data)
|
|
45
|
+
}
|
|
46
|
+
default: {
|
|
47
|
+
throw new Error('command: second argument must be a function or string')
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import { KarinAdapter } from '../types/adapter.js';
|
|
4
|
+
import { contact } from '../types/types.js';
|
|
5
|
+
import { KarinElement } from '../types/element.js';
|
|
6
|
+
/**
|
|
7
|
+
* 监听器管理
|
|
8
|
+
*/
|
|
9
|
+
declare const _default: {
|
|
10
|
+
/**
|
|
11
|
+
* Bot索引
|
|
12
|
+
* @type - Bot索引
|
|
13
|
+
*/
|
|
14
|
+
index: number;
|
|
15
|
+
/**
|
|
16
|
+
* 框架名称
|
|
17
|
+
*/
|
|
18
|
+
name: string;
|
|
19
|
+
list: Array<{
|
|
20
|
+
index: number;
|
|
21
|
+
type: KarinAdapter['adapter']['type'];
|
|
22
|
+
bot: KarinAdapter;
|
|
23
|
+
}>;
|
|
24
|
+
adapter: Array<{
|
|
25
|
+
type: KarinAdapter['adapter']['type'];
|
|
26
|
+
adapter: new () => KarinAdapter;
|
|
27
|
+
path: string;
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* 注册Bot 返回索引id
|
|
31
|
+
*/
|
|
32
|
+
addBot(data: {
|
|
33
|
+
bot: KarinAdapter;
|
|
34
|
+
type: KarinAdapter['adapter']['type'];
|
|
35
|
+
}): number | false;
|
|
36
|
+
/**
|
|
37
|
+
* 卸载Bot
|
|
38
|
+
* @param index - Bot的索引id
|
|
39
|
+
*/
|
|
40
|
+
delBot(index: number): void;
|
|
41
|
+
/**
|
|
42
|
+
* 通过Bot uid 获取Bot
|
|
43
|
+
* @param uid - Bot的uid 未传入则返回第一个Bot
|
|
44
|
+
*/
|
|
45
|
+
getBot(uid?: string): KarinAdapter | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* 根据索引获取Bot
|
|
48
|
+
* @param index - Bot的索引id
|
|
49
|
+
*/
|
|
50
|
+
getBotByIndex(index: number): KarinAdapter;
|
|
51
|
+
/**
|
|
52
|
+
* 获取当前已注册Bot数量
|
|
53
|
+
*/
|
|
54
|
+
getBotCount(): number;
|
|
55
|
+
/**
|
|
56
|
+
* 获取所有Bot列表
|
|
57
|
+
* @param isIndex - 是否返回包含的索引列表 默认返回Bot实例列表
|
|
58
|
+
*/
|
|
59
|
+
getBotAll(isIndex?: boolean): KarinAdapter[] | {
|
|
60
|
+
index: number;
|
|
61
|
+
bot: KarinAdapter;
|
|
62
|
+
}[];
|
|
63
|
+
/**
|
|
64
|
+
* 注册适配器
|
|
65
|
+
* @param data - 适配器信息
|
|
66
|
+
* @param data.type - 适配器类型
|
|
67
|
+
* @param data.adapter - 适配器实例
|
|
68
|
+
* @param data.path - 适配器路径
|
|
69
|
+
*/
|
|
70
|
+
addAdapter(data: {
|
|
71
|
+
type: KarinAdapter['adapter']['type'];
|
|
72
|
+
adapter: new () => KarinAdapter;
|
|
73
|
+
path?: string;
|
|
74
|
+
}): void;
|
|
75
|
+
/**
|
|
76
|
+
* 通过path获取适配器 仅适用于反向WS适配器
|
|
77
|
+
* @param path - 适配器路径
|
|
78
|
+
*/
|
|
79
|
+
getAdapter(path?: string): (new () => KarinAdapter) | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* 获取适配器列表
|
|
82
|
+
* @param isType - 是否返回包含的类型列表 默认返回适配器实例列表
|
|
83
|
+
*/
|
|
84
|
+
getAdapterAll(isType?: boolean): {
|
|
85
|
+
type: KarinAdapter['adapter']['type'];
|
|
86
|
+
adapter: new () => KarinAdapter;
|
|
87
|
+
path: string;
|
|
88
|
+
}[] | (new () => KarinAdapter)[];
|
|
89
|
+
/**
|
|
90
|
+
* 发送主动消息
|
|
91
|
+
* @param uid - Bot的uid
|
|
92
|
+
* @param contact - 目标信息
|
|
93
|
+
* @param elements - 消息内容
|
|
94
|
+
* @param options - 消息选项
|
|
95
|
+
* @param options.recallMsg - 发送成功后撤回消息时间
|
|
96
|
+
* @param options.retry_count - 重试次数
|
|
97
|
+
*/
|
|
98
|
+
sendMsg(uid: string, contact: contact, elements: KarinElement, options?: {
|
|
99
|
+
recallMsg: number;
|
|
100
|
+
retry_count: number;
|
|
101
|
+
}): Promise<{
|
|
102
|
+
message_id: string;
|
|
103
|
+
}>;
|
|
104
|
+
[EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: string | symbol, ...args: any[]): void;
|
|
105
|
+
addListener<K_1>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
106
|
+
on<K_2>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
107
|
+
once<K_3>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
108
|
+
removeListener<K_4>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
109
|
+
off<K_5>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
110
|
+
removeAllListeners(eventName?: string | symbol | undefined): any;
|
|
111
|
+
setMaxListeners(n: number): any;
|
|
112
|
+
getMaxListeners(): number;
|
|
113
|
+
listeners<K_6>(eventName: string | symbol): Function[];
|
|
114
|
+
rawListeners<K_7>(eventName: string | symbol): Function[];
|
|
115
|
+
emit<K_8>(eventName: string | symbol, ...args: any[]): boolean;
|
|
116
|
+
listenerCount<K_9>(eventName: string | symbol, listener?: Function | undefined): number;
|
|
117
|
+
prependListener<K_10>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
118
|
+
prependOnceListener<K_11>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
119
|
+
eventNames(): (string | symbol)[];
|
|
120
|
+
};
|
|
121
|
+
export default _default;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { EventEmitter } from 'events'
|
|
2
|
+
import loader from './plugin.loader.js'
|
|
3
|
+
import Common from '../utils/common.js'
|
|
4
|
+
import logger from '../utils/logger.js'
|
|
5
|
+
import Config from '../utils/config.js'
|
|
6
|
+
import MessageHandller from '../event/message.handler.js'
|
|
7
|
+
/**
|
|
8
|
+
* 监听器管理
|
|
9
|
+
*/
|
|
10
|
+
export default new (class Listeners extends EventEmitter {
|
|
11
|
+
/**
|
|
12
|
+
* Bot索引
|
|
13
|
+
* @type - Bot索引
|
|
14
|
+
*/
|
|
15
|
+
index
|
|
16
|
+
/**
|
|
17
|
+
* 框架名称
|
|
18
|
+
*/
|
|
19
|
+
name
|
|
20
|
+
list
|
|
21
|
+
adapter
|
|
22
|
+
constructor () {
|
|
23
|
+
super()
|
|
24
|
+
this.index = 0
|
|
25
|
+
this.name = 'Karin'
|
|
26
|
+
this.list = []
|
|
27
|
+
this.adapter = []
|
|
28
|
+
this.on('error', data => logger.error(data))
|
|
29
|
+
this.on('plugin', () => loader.load())
|
|
30
|
+
this.on('adapter', data => {
|
|
31
|
+
let path = data.path || '无'
|
|
32
|
+
if (path && data.type !== 'grpc') { path = `ws://127.0.0.1:/${Config.Server.http.port}${data.path}` }
|
|
33
|
+
path = logger.green(path)
|
|
34
|
+
logger.info(`[适配器][注册][${data.type}] ` + path)
|
|
35
|
+
this.addAdapter(data)
|
|
36
|
+
})
|
|
37
|
+
this.on('bot', data => {
|
|
38
|
+
this.addBot(data)
|
|
39
|
+
logger.info(`[机器人][注册][${data.type}] ` + logger.green(`[account:${data.bot.account.uid || data.bot.account.uin}(${data.bot.account.name})]`))
|
|
40
|
+
this.emit('karin:online', data.bot.account.uid || data.bot.account.uin)
|
|
41
|
+
})
|
|
42
|
+
this.on('message', data => new MessageHandller(data))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 注册Bot 返回索引id
|
|
47
|
+
*/
|
|
48
|
+
addBot (data) {
|
|
49
|
+
this.index++
|
|
50
|
+
const index = this.index
|
|
51
|
+
if (!data.bot) {
|
|
52
|
+
logger.error('[Bot管理][注册] 注册失败: Bot实例不能为空')
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
this.list.push({ index, type: data.type, bot: data.bot })
|
|
56
|
+
return index
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 卸载Bot
|
|
61
|
+
* @param index - Bot的索引id
|
|
62
|
+
*/
|
|
63
|
+
delBot (index) {
|
|
64
|
+
this.list = this.list.filter(item => item.index !== index)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 通过Bot uid 获取Bot
|
|
69
|
+
* @param uid - Bot的uid 未传入则返回第一个Bot
|
|
70
|
+
*/
|
|
71
|
+
getBot (uid = '') {
|
|
72
|
+
uid = String(uid)
|
|
73
|
+
if (this.list.length === 0) {
|
|
74
|
+
logger.error('[Bot管理][UID] 当前Bot列表为空')
|
|
75
|
+
return undefined
|
|
76
|
+
}
|
|
77
|
+
if (!uid) { return this.list[0].bot }
|
|
78
|
+
const index = this.list.findIndex(item => String(item.bot.account.uid) === uid)
|
|
79
|
+
if (index === -1) {
|
|
80
|
+
logger.error('[Bot管理][UID] 无法找到对应的 Bot 实例')
|
|
81
|
+
return undefined
|
|
82
|
+
}
|
|
83
|
+
return this.list[index].bot
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 根据索引获取Bot
|
|
88
|
+
* @param index - Bot的索引id
|
|
89
|
+
*/
|
|
90
|
+
getBotByIndex (index) {
|
|
91
|
+
index = this.list.findIndex(item => item.index === index)
|
|
92
|
+
if (index === -1) {
|
|
93
|
+
throw new Error('[Bot管理][索引] 无法找到对应的 Bot 实例')
|
|
94
|
+
}
|
|
95
|
+
return this.list[index].bot
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 获取当前已注册Bot数量
|
|
100
|
+
*/
|
|
101
|
+
getBotCount () {
|
|
102
|
+
return this.list.length
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 获取所有Bot列表
|
|
107
|
+
* @param isIndex - 是否返回包含的索引列表 默认返回Bot实例列表
|
|
108
|
+
*/
|
|
109
|
+
getBotAll (isIndex = false) {
|
|
110
|
+
if (isIndex) { return this.list }
|
|
111
|
+
return this.list.map(item => item.bot)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 注册适配器
|
|
116
|
+
* @param data - 适配器信息
|
|
117
|
+
* @param data.type - 适配器类型
|
|
118
|
+
* @param data.adapter - 适配器实例
|
|
119
|
+
* @param data.path - 适配器路径
|
|
120
|
+
*/
|
|
121
|
+
addAdapter (data) {
|
|
122
|
+
const adapter = { type: data.type, adapter: data.adapter, path: '' }
|
|
123
|
+
if (data.path) { adapter.path = data.path }
|
|
124
|
+
this.adapter.push(adapter)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 通过path获取适配器 仅适用于反向WS适配器
|
|
129
|
+
* @param path - 适配器路径
|
|
130
|
+
*/
|
|
131
|
+
getAdapter (path = '') {
|
|
132
|
+
const index = this.adapter.findIndex(item => item?.path === path)
|
|
133
|
+
if (index === -1) {
|
|
134
|
+
logger.error('[适配器管理] 无法找到对应的适配器实例')
|
|
135
|
+
return undefined
|
|
136
|
+
}
|
|
137
|
+
return this.adapter[index].adapter
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 获取适配器列表
|
|
142
|
+
* @param isType - 是否返回包含的类型列表 默认返回适配器实例列表
|
|
143
|
+
*/
|
|
144
|
+
getAdapterAll (isType = false) {
|
|
145
|
+
if (isType) { return this.adapter }
|
|
146
|
+
return this.adapter.map(item => item.adapter)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 发送主动消息
|
|
151
|
+
* @param uid - Bot的uid
|
|
152
|
+
* @param contact - 目标信息
|
|
153
|
+
* @param elements - 消息内容
|
|
154
|
+
* @param options - 消息选项
|
|
155
|
+
* @param options.recallMsg - 发送成功后撤回消息时间
|
|
156
|
+
* @param options.retry_count - 重试次数
|
|
157
|
+
*/
|
|
158
|
+
async sendMsg (uid, contact, elements, options = { recallMsg: 0, retry_count: 1 }) {
|
|
159
|
+
const bot = this.getBot(uid)
|
|
160
|
+
if (!bot) { throw new Error('发送消息失败: 未找到对应Bot实例') }
|
|
161
|
+
const { recallMsg, retry_count } = options
|
|
162
|
+
/** 标准化 */
|
|
163
|
+
const NewElements = Common.makeMessage(elements)
|
|
164
|
+
/** 结果 */
|
|
165
|
+
let result = { message_id: '' }
|
|
166
|
+
const reply_log = Common.makeMessageLog(NewElements)
|
|
167
|
+
const self_id = bot.account.uid || bot.account.uin
|
|
168
|
+
if (contact.scene === 'group') {
|
|
169
|
+
logger.bot('info', self_id, `Send Proactive Group ${contact.peer}: ${reply_log}`)
|
|
170
|
+
} else {
|
|
171
|
+
logger.bot('info', self_id, `Send Proactive private ${contact.peer}: ${reply_log}`)
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
this.emit('karin:count:send', 1)
|
|
175
|
+
/** 取结果 */
|
|
176
|
+
result = await bot.SendMessage(contact, NewElements, retry_count)
|
|
177
|
+
logger.bot('debug', self_id, `主动消息结果:${JSON.stringify(result, null, 2)}`)
|
|
178
|
+
} catch (error) {
|
|
179
|
+
logger.bot('error', self_id, `主动消息发送失败:${reply_log}`)
|
|
180
|
+
logger.bot('error', self_id, error)
|
|
181
|
+
}
|
|
182
|
+
/** 快速撤回 */
|
|
183
|
+
if (bot.RecallMessage && recallMsg > 0 && result?.message_id) {
|
|
184
|
+
setTimeout(() => bot.RecallMessage(contact, result.message_id), recallMsg * 1000)
|
|
185
|
+
}
|
|
186
|
+
return result
|
|
187
|
+
}
|
|
188
|
+
})()
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { dirName, fileName, PluginApps } from '../types/plugin.js';
|
|
2
|
+
export interface PluginAppType {
|
|
3
|
+
file?: {
|
|
4
|
+
dir?: dirName;
|
|
5
|
+
name?: fileName;
|
|
6
|
+
type?: PluginApps['file']['type'];
|
|
7
|
+
fnc?: PluginApps['file']['Fnc'];
|
|
8
|
+
};
|
|
9
|
+
name?: string;
|
|
10
|
+
event?: PluginApps['event'];
|
|
11
|
+
priority?: number;
|
|
12
|
+
accept?: boolean;
|
|
13
|
+
rule?: PluginApps['rule'];
|
|
14
|
+
}
|
|
15
|
+
export default function PluginApp(options: PluginAppType): PluginApps;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default function PluginApp (options) {
|
|
2
|
+
return {
|
|
3
|
+
file: {
|
|
4
|
+
dir: options?.file?.dir || '',
|
|
5
|
+
name: options?.file?.name || '',
|
|
6
|
+
type: options?.file?.type || 'function',
|
|
7
|
+
Fnc: options?.file?.fnc || '',
|
|
8
|
+
},
|
|
9
|
+
name: options.name || '',
|
|
10
|
+
event: options.event || 'message',
|
|
11
|
+
priority: options.priority || 10000,
|
|
12
|
+
accept: options.accept ?? false,
|
|
13
|
+
rule: options.rule || [],
|
|
14
|
+
task: [],
|
|
15
|
+
handler: [],
|
|
16
|
+
button: [],
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { Plugin as PluginType } from '../types/plugin.js';
|
|
2
|
+
import { KarinElement, KarinNodeElement } from '../types/element.js';
|
|
3
|
+
import { E } from '../types/types.js';
|
|
4
|
+
/**
|
|
5
|
+
* 插件基类
|
|
6
|
+
*/
|
|
7
|
+
export default class Plugin implements PluginType {
|
|
8
|
+
e: PluginType['e'];
|
|
9
|
+
init?: () => void;
|
|
10
|
+
accept?: (e: E) => Promise<void>;
|
|
11
|
+
replyCallback: PluginType['replyCallback'];
|
|
12
|
+
/**
|
|
13
|
+
* @param name - 插件名称
|
|
14
|
+
*/
|
|
15
|
+
name: PluginType['name'];
|
|
16
|
+
/**
|
|
17
|
+
* @param dsc - 插件描述
|
|
18
|
+
* @deprecated 请使用desc
|
|
19
|
+
*/
|
|
20
|
+
dsc: PluginType['dsc'];
|
|
21
|
+
/**
|
|
22
|
+
* @param desc - 插件描述
|
|
23
|
+
*/
|
|
24
|
+
desc: PluginType['desc'];
|
|
25
|
+
/**
|
|
26
|
+
* @param event - 监听事件
|
|
27
|
+
*/
|
|
28
|
+
event: PluginType['event'];
|
|
29
|
+
/**
|
|
30
|
+
* @param priority - 优先级 默认5000
|
|
31
|
+
*/
|
|
32
|
+
priority: PluginType['priority'];
|
|
33
|
+
/**
|
|
34
|
+
* @param task - 定时任务
|
|
35
|
+
*/
|
|
36
|
+
task: PluginType['task'];
|
|
37
|
+
/**
|
|
38
|
+
* @param rule - 命令规则
|
|
39
|
+
*/
|
|
40
|
+
rule: PluginType['rule'];
|
|
41
|
+
/**
|
|
42
|
+
* @param button - 按钮
|
|
43
|
+
*/
|
|
44
|
+
button: PluginType['button'];
|
|
45
|
+
/**
|
|
46
|
+
* @param handler - handler
|
|
47
|
+
*/
|
|
48
|
+
handler: PluginType['handler'];
|
|
49
|
+
/**
|
|
50
|
+
* @param userId - 用户ID 一般上下文使用
|
|
51
|
+
*/
|
|
52
|
+
userId?: PluginType['userId'];
|
|
53
|
+
/**
|
|
54
|
+
* @param timeout - 上下文超时
|
|
55
|
+
*/
|
|
56
|
+
timeout: PluginType['timeout'];
|
|
57
|
+
constructor({ name, dsc, desc, event, priority, task, rule, handler, button, }: {
|
|
58
|
+
/**
|
|
59
|
+
* - 插件名称
|
|
60
|
+
*/
|
|
61
|
+
name: string;
|
|
62
|
+
/**
|
|
63
|
+
* - 插件描述 没有则默认为名称
|
|
64
|
+
*/
|
|
65
|
+
dsc: string;
|
|
66
|
+
/**
|
|
67
|
+
* - 插件描述 没有则默认为插件名称
|
|
68
|
+
*/
|
|
69
|
+
desc?: string;
|
|
70
|
+
/**
|
|
71
|
+
* - 监听事件 默认为message
|
|
72
|
+
*/
|
|
73
|
+
event?: PluginType['event'];
|
|
74
|
+
/**
|
|
75
|
+
* - 优先级 默认为5000
|
|
76
|
+
*/
|
|
77
|
+
priority?: PluginType['priority'];
|
|
78
|
+
/**
|
|
79
|
+
* - 定时任务
|
|
80
|
+
*/
|
|
81
|
+
task?: PluginType['task'];
|
|
82
|
+
/**
|
|
83
|
+
* - 命令规则
|
|
84
|
+
*/
|
|
85
|
+
rule?: PluginType['rule'];
|
|
86
|
+
/**
|
|
87
|
+
* - handler
|
|
88
|
+
*/
|
|
89
|
+
handler?: PluginType['handler'];
|
|
90
|
+
/**
|
|
91
|
+
* - 按钮
|
|
92
|
+
*/
|
|
93
|
+
button?: PluginType['button'];
|
|
94
|
+
});
|
|
95
|
+
/**
|
|
96
|
+
* - 快速回复
|
|
97
|
+
*/
|
|
98
|
+
reply(msg?: string | KarinElement | Array<KarinElement | string>, options?: {
|
|
99
|
+
/**
|
|
100
|
+
* @param at - 是否at用户
|
|
101
|
+
* @default false
|
|
102
|
+
*/
|
|
103
|
+
at?: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* @param reply - 是否引用回复
|
|
106
|
+
* @default false
|
|
107
|
+
*/
|
|
108
|
+
reply?: boolean;
|
|
109
|
+
/**
|
|
110
|
+
* @param recallMsg - 群聊是否撤回消息,0-120秒,0不撤回
|
|
111
|
+
* @default 0
|
|
112
|
+
*/
|
|
113
|
+
recallMsg?: number;
|
|
114
|
+
/**
|
|
115
|
+
* @param button - 是否使用按钮
|
|
116
|
+
* @default false
|
|
117
|
+
*/
|
|
118
|
+
button?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* @param retry_count - 重试次数
|
|
121
|
+
* @default 1
|
|
122
|
+
*/
|
|
123
|
+
retry_count?: number;
|
|
124
|
+
}): Promise<{
|
|
125
|
+
/**
|
|
126
|
+
* @param message_id - 消息发送成功返回的消息ID
|
|
127
|
+
*/
|
|
128
|
+
message_id?: string;
|
|
129
|
+
}>;
|
|
130
|
+
/**
|
|
131
|
+
* - 快速回复合并转发
|
|
132
|
+
*/
|
|
133
|
+
replyForward(msg: KarinNodeElement[]): Promise<{
|
|
134
|
+
message_id?: string;
|
|
135
|
+
}>;
|
|
136
|
+
/**
|
|
137
|
+
* - 构建上下文键
|
|
138
|
+
*/
|
|
139
|
+
conKey(): string;
|
|
140
|
+
/**
|
|
141
|
+
* 设置上下文状态
|
|
142
|
+
*/
|
|
143
|
+
setContext(
|
|
144
|
+
/**
|
|
145
|
+
* @param fnc - 执行方法
|
|
146
|
+
*/
|
|
147
|
+
fnc: string,
|
|
148
|
+
/**
|
|
149
|
+
* @param reply - 超时后是否回复
|
|
150
|
+
*/
|
|
151
|
+
reply?: boolean,
|
|
152
|
+
/**
|
|
153
|
+
* @param time - 超时时间,默认120秒
|
|
154
|
+
*/
|
|
155
|
+
time?: number): void;
|
|
156
|
+
/**
|
|
157
|
+
* 获取上下文状态
|
|
158
|
+
*/
|
|
159
|
+
getContext(): {
|
|
160
|
+
plugin: Plugin;
|
|
161
|
+
fnc: string;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* 清除上下文状态
|
|
165
|
+
*/
|
|
166
|
+
finish(): void;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 上下文状态
|
|
170
|
+
*/
|
|
171
|
+
export declare const stateArr: {
|
|
172
|
+
[key: string]: {
|
|
173
|
+
/**
|
|
174
|
+
* @param plugin - 插件实例
|
|
175
|
+
*/
|
|
176
|
+
plugin: Plugin;
|
|
177
|
+
/**
|
|
178
|
+
* @param fnc - 执行方法名称
|
|
179
|
+
*/
|
|
180
|
+
fnc: string;
|
|
181
|
+
};
|
|
182
|
+
};
|