hotcat-bot-qq 0.1.5 → 0.1.7
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/dist/botConsole.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export type NoticeEvent = Record<string, any> & {
|
|
|
13
13
|
sub_type?: string;
|
|
14
14
|
};
|
|
15
15
|
/** BotConsole 日志分类 */
|
|
16
|
-
export type BotConsoleType = 'system' | 'groupMessage' | 'privateMessage' | 'error' | 'sendGroupMessage' | 'sendPrivateMessage' | 'groupMessageSelf' | 'privateMessageSelf' | 'requestFriend' | 'requestGroupAdd' | 'requestGroupInvite' | 'notice';
|
|
16
|
+
export type BotConsoleType = 'system' | 'groupMessage' | 'privateMessage' | 'error' | 'sendGroupMessage' | 'sendPrivateMessage' | 'groupMessageSelf' | 'privateMessageSelf' | 'requestFriend' | 'requestGroupAdd' | 'requestGroupInvite' | 'notice' | 'plugin';
|
|
17
17
|
/** ANSI 颜色/样式码 */
|
|
18
18
|
export declare const colors: {
|
|
19
19
|
reset: string;
|
|
@@ -66,7 +66,13 @@ export declare const formatMessage: (m: Message) => string;
|
|
|
66
66
|
*/
|
|
67
67
|
export declare class BotConsole {
|
|
68
68
|
type: BotConsoleType;
|
|
69
|
-
content: string | GroupMessage | GroupMessageSelf | PrivateMessage | PrivateMessageSelf | Message[] | RequestFriend | RequestGroupAdd | RequestGroupInvite | NoticeEvent
|
|
70
|
-
|
|
69
|
+
content: string | GroupMessage | GroupMessageSelf | PrivateMessage | PrivateMessageSelf | Message[] | RequestFriend | RequestGroupAdd | RequestGroupInvite | NoticeEvent | {
|
|
70
|
+
name: string;
|
|
71
|
+
msg: string;
|
|
72
|
+
};
|
|
73
|
+
constructor(type: BotConsoleType, content: string | GroupMessage | GroupMessageSelf | PrivateMessage | PrivateMessageSelf | Message[] | RequestFriend | RequestGroupAdd | RequestGroupInvite | NoticeEvent | {
|
|
74
|
+
name: string;
|
|
75
|
+
msg: string;
|
|
76
|
+
});
|
|
71
77
|
log(): void;
|
|
72
78
|
}
|
package/dist/botConsole.js
CHANGED
|
@@ -293,5 +293,9 @@ export class BotConsole {
|
|
|
293
293
|
('name_new' in event ? `${colors.green}${sanitize(event.name_new)}${colors.reset}` : '') +
|
|
294
294
|
('file' in event ? `文件:${event.file.name}` : ''));
|
|
295
295
|
}
|
|
296
|
+
else if (this.type === 'plugin') {
|
|
297
|
+
const { name, msg } = this.content;
|
|
298
|
+
console.log(`${colors.magenta}[plugin:${name}]:${colors.reset} ${msg}`);
|
|
299
|
+
}
|
|
296
300
|
}
|
|
297
301
|
}
|
package/dist/botPlugin.d.ts
CHANGED
package/dist/botPlugin.js
CHANGED
|
@@ -70,7 +70,7 @@ export class BotPluginSystem {
|
|
|
70
70
|
throw new Error(`插件 "${name}" 已注册`);
|
|
71
71
|
}
|
|
72
72
|
this.registry.set(name, { ctor, meta });
|
|
73
|
-
new BotConsole('
|
|
73
|
+
new BotConsole('plugin', { name: meta.name, msg: '已注册' }).log();
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* 加载插件
|
|
@@ -87,7 +87,7 @@ export class BotPluginSystem {
|
|
|
87
87
|
const instance = record.ctor.create(this.api, this.client, record.meta);
|
|
88
88
|
await instance.load();
|
|
89
89
|
this.instances.set(name, instance);
|
|
90
|
-
new BotConsole('
|
|
90
|
+
new BotConsole('plugin', { name: record.meta.name, msg: '已加载' }).log();
|
|
91
91
|
}
|
|
92
92
|
/**
|
|
93
93
|
* 卸载插件
|
|
@@ -100,7 +100,7 @@ export class BotPluginSystem {
|
|
|
100
100
|
}
|
|
101
101
|
await instance.unload();
|
|
102
102
|
this.instances.delete(name);
|
|
103
|
-
new BotConsole('
|
|
103
|
+
new BotConsole('plugin', { name: this.registry.get(name)?.meta.name || name, msg: '已卸载' }).log();
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
106
|
* 热重载:先卸载再加载
|
|
@@ -122,6 +122,15 @@ export class BotPluginSystem {
|
|
|
122
122
|
loaded() {
|
|
123
123
|
return Array.from(this.instances.keys());
|
|
124
124
|
}
|
|
125
|
+
findIndex(dir, name) {
|
|
126
|
+
const js = path.join(dir, name, 'index.js');
|
|
127
|
+
if (fs.existsSync(js))
|
|
128
|
+
return js;
|
|
129
|
+
const ts = path.join(dir, name, 'index.ts');
|
|
130
|
+
if (fs.existsSync(ts))
|
|
131
|
+
return ts;
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
125
134
|
/**
|
|
126
135
|
* 扫描目录,自动注册并加载所有插件
|
|
127
136
|
* @param dir - 插件目录路径
|
|
@@ -137,8 +146,8 @@ export class BotPluginSystem {
|
|
|
137
146
|
if (!entry.isDirectory())
|
|
138
147
|
continue;
|
|
139
148
|
const name = entry.name;
|
|
140
|
-
const indexPath =
|
|
141
|
-
if (!
|
|
149
|
+
const indexPath = this.findIndex(dir, name);
|
|
150
|
+
if (!indexPath)
|
|
142
151
|
continue;
|
|
143
152
|
try {
|
|
144
153
|
const mod = await import(path.resolve(indexPath));
|
|
@@ -179,7 +188,7 @@ export class BotPluginSystem {
|
|
|
179
188
|
return;
|
|
180
189
|
}
|
|
181
190
|
this.watched = new Set(this.list());
|
|
182
|
-
new BotConsole('
|
|
191
|
+
new BotConsole('plugin', { name: 'watch', msg: `开始监听 ${this.watchDir}` }).log();
|
|
183
192
|
this.watcher = setInterval(async () => {
|
|
184
193
|
if (!fs.existsSync(this.watchDir))
|
|
185
194
|
return;
|
|
@@ -189,8 +198,8 @@ export class BotPluginSystem {
|
|
|
189
198
|
if (!entry.isDirectory())
|
|
190
199
|
continue;
|
|
191
200
|
const name = entry.name;
|
|
192
|
-
const indexPath =
|
|
193
|
-
if (!
|
|
201
|
+
const indexPath = this.findIndex(this.watchDir, name);
|
|
202
|
+
if (!indexPath)
|
|
194
203
|
continue;
|
|
195
204
|
current.add(name);
|
|
196
205
|
if (!this.watched.has(name)) {
|
|
@@ -199,12 +208,13 @@ export class BotPluginSystem {
|
|
|
199
208
|
}
|
|
200
209
|
for (const name of Array.from(this.watched)) {
|
|
201
210
|
if (!current.has(name) && this.registry.has(name)) {
|
|
211
|
+
const displayName = this.registry.get(name)?.meta.name || name;
|
|
202
212
|
try {
|
|
203
213
|
if (this.instances.has(name)) {
|
|
204
214
|
await this.unload(name);
|
|
205
215
|
}
|
|
206
216
|
this.registry.delete(name);
|
|
207
|
-
new BotConsole('
|
|
217
|
+
new BotConsole('plugin', { name: displayName, msg: '目录已删除,已移除' }).log();
|
|
208
218
|
}
|
|
209
219
|
catch { }
|
|
210
220
|
}
|
|
@@ -218,11 +228,13 @@ export class BotPluginSystem {
|
|
|
218
228
|
clearInterval(this.watcher);
|
|
219
229
|
this.watcher = null;
|
|
220
230
|
this.watchDir = null;
|
|
221
|
-
new BotConsole('
|
|
231
|
+
new BotConsole('plugin', { name: 'watch', msg: '已停止' }).log();
|
|
222
232
|
}
|
|
223
233
|
}
|
|
224
234
|
async scanOne(dir, name) {
|
|
225
|
-
const indexPath =
|
|
235
|
+
const indexPath = this.findIndex(dir, name);
|
|
236
|
+
if (!indexPath)
|
|
237
|
+
return;
|
|
226
238
|
try {
|
|
227
239
|
const mod = await import(indexPath);
|
|
228
240
|
const ctor = mod[name.charAt(0).toUpperCase() + name.slice(1) + 'Plugin'] || mod.default || Object.values(mod).find((v) => typeof v === 'function' && v.create);
|
|
@@ -234,7 +246,7 @@ export class BotPluginSystem {
|
|
|
234
246
|
};
|
|
235
247
|
this.register(name, ctor, meta);
|
|
236
248
|
await this.load(name);
|
|
237
|
-
new BotConsole('
|
|
249
|
+
new BotConsole('plugin', { name: meta.name, msg: '已自动加载' }).log();
|
|
238
250
|
}
|
|
239
251
|
catch (e) {
|
|
240
252
|
new BotConsole('error', `自动加载插件 "${name}" 失败: ${e.message || e}`).log();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hotcat-bot-qq",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "基于 node-napcat-ts 的 TypeScript QQ 机器人框架",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
],
|
|
53
53
|
"scripts": {
|
|
54
54
|
"build": "tsc -p tsconfig.build.json",
|
|
55
|
-
"
|
|
55
|
+
"build:plugin": "tsc --outDir plugins/hotCatPlugin --rootDir plugins/hotCatPlugin --module ESNext --target ES2020 --moduleResolution bundler --declaration --skipLibCheck plugins/hotCatPlugin/index.ts",
|
|
56
|
+
"prepublishOnly": "bun run build && bun run build:plugin",
|
|
56
57
|
"docs:dev": "vitepress dev docs",
|
|
57
58
|
"docs:build": "vitepress build docs",
|
|
58
59
|
"docs:preview": "vitepress preview docs"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PluginBase } from 'hotcat-bot-qq/plugin';
|
|
2
|
+
import { BotApi } from 'hotcat-bot-qq/botApi';
|
|
3
|
+
import { BotClient } from 'hotcat-bot-qq/botClient';
|
|
4
|
+
export declare class HotCatPlugin extends PluginBase {
|
|
5
|
+
static meta: {
|
|
6
|
+
name: string;
|
|
7
|
+
version: string;
|
|
8
|
+
description: string;
|
|
9
|
+
author: string;
|
|
10
|
+
};
|
|
11
|
+
private startTime;
|
|
12
|
+
static create(api: BotApi, bot: BotClient): HotCatPlugin;
|
|
13
|
+
load(): Promise<void>;
|
|
14
|
+
unload(): Promise<void>;
|
|
15
|
+
private onGroupMsg;
|
|
16
|
+
private formatUptime;
|
|
17
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { PluginBase } from 'hotcat-bot-qq/plugin';
|
|
2
|
+
import { Message } from 'hotcat-bot-qq/message';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
export class HotCatPlugin extends PluginBase {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.startTime = 0;
|
|
8
|
+
this.onGroupMsg = async (_bot, event) => {
|
|
9
|
+
const msg = event.raw_message.trim();
|
|
10
|
+
if (msg === '#hotCat' || msg === '#hotcat') {
|
|
11
|
+
const uptime = this.formatUptime(Date.now() - this.startTime);
|
|
12
|
+
const platform = os.platform();
|
|
13
|
+
const arch = os.arch();
|
|
14
|
+
await this.api.sendGroupMessage(event.group_id, Message.reply(event.message_id), Message.text(`HotCat 信息\n` +
|
|
15
|
+
`版本:${this.meta.version}\n` +
|
|
16
|
+
`平台:${platform} ${arch}\n` +
|
|
17
|
+
`运行时间:${uptime}`));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
static create(api, bot) {
|
|
22
|
+
return new HotCatPlugin(api, bot);
|
|
23
|
+
}
|
|
24
|
+
async load() {
|
|
25
|
+
this.startTime = Date.now();
|
|
26
|
+
this.bot.event.message.onGroupMessage(this.onGroupMsg);
|
|
27
|
+
}
|
|
28
|
+
async unload() {
|
|
29
|
+
this.bot.event.message.offGroupMessage(this.onGroupMsg);
|
|
30
|
+
}
|
|
31
|
+
formatUptime(ms) {
|
|
32
|
+
const s = Math.floor(ms / 1000);
|
|
33
|
+
const d = Math.floor(s / 86400);
|
|
34
|
+
const h = Math.floor((s % 86400) / 3600);
|
|
35
|
+
const m = Math.floor((s % 3600) / 60);
|
|
36
|
+
const parts = [];
|
|
37
|
+
if (d > 0)
|
|
38
|
+
parts.push(`${d}天`);
|
|
39
|
+
if (h > 0)
|
|
40
|
+
parts.push(`${h}小时`);
|
|
41
|
+
if (m > 0)
|
|
42
|
+
parts.push(`${m}分钟`);
|
|
43
|
+
return parts.length > 0 ? parts.join(' ') : '小于1分钟';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
HotCatPlugin.meta = {
|
|
47
|
+
name: 'hotCatPlugin',
|
|
48
|
+
version: '0.1.7',
|
|
49
|
+
description: 'HotCat 默认插件(#hotCat 指令查询 bot 状态)',
|
|
50
|
+
author: 'CHCAT1320',
|
|
51
|
+
};
|