koishi-plugin-chat-patch 1.2.0 → 1.3.0
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/client/icons/activity.vue +9 -9
- package/client/icons/index.ts +4 -4
- package/client/index.scss +4 -4
- package/client/index.ts +22 -22
- package/client/vue/chat-logic.ts +3784 -3784
- package/client/vue/index.vue +361 -361
- package/client/vue/style.css +1997 -1973
- package/dist/index.js +1 -1
- package/dist/style.css +1 -1
- package/lib/index.js +20 -3
- package/package.json +40 -40
- package/readme.md +25 -25
- package/src/api-handlers.ts +657 -657
- package/src/config.ts +49 -49
- package/src/file-manager.ts +168 -168
- package/src/index.ts +125 -125
- package/src/message-handler.ts +304 -282
- package/src/types.ts +59 -59
- package/src/utils.ts +99 -99
- package/lib/config.d.ts +0 -14
package/src/index.ts
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
|
|
2
|
-
import { Console } from '@koishijs/console'
|
|
3
|
-
import { Context, h } from 'koishi'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
|
|
6
|
-
import { MessageHandler } from './message-handler'
|
|
7
|
-
import { FileManager } from './file-manager'
|
|
8
|
-
import { ApiHandlers } from './api-handlers'
|
|
9
|
-
import { Config } from './config'
|
|
10
|
-
import { Utils } from './utils'
|
|
11
|
-
|
|
12
|
-
export const name = 'chat-patch'
|
|
13
|
-
export const reusable = false
|
|
14
|
-
export const filter = true
|
|
15
|
-
export const inject = {
|
|
16
|
-
required: ['console']
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
declare module 'koishi' {
|
|
20
|
-
interface Context {
|
|
21
|
-
console: Console
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const usage = `
|
|
26
|
-
|
|
27
|
-
---
|
|
28
|
-
|
|
29
|
-
开启后,即可在koishi控制台操作机器人收发消息啦
|
|
30
|
-
|
|
31
|
-
暂时只支持接受图文消息 / 发送文字消息
|
|
32
|
-
|
|
33
|
-
---
|
|
34
|
-
`
|
|
35
|
-
|
|
36
|
-
export { Config } from './config'
|
|
37
|
-
|
|
38
|
-
export async function apply(ctx: Context, config: Config) {
|
|
39
|
-
const logger = ctx.logger('chat-patch')
|
|
40
|
-
|
|
41
|
-
// 初始化各个模块
|
|
42
|
-
const fileManager = new FileManager(ctx, config)
|
|
43
|
-
const messageHandler = new MessageHandler(ctx, config, fileManager)
|
|
44
|
-
const apiHandlers = new ApiHandlers(ctx, config, fileManager, messageHandler)
|
|
45
|
-
const utils = new Utils(config)
|
|
46
|
-
|
|
47
|
-
// 初始化数据
|
|
48
|
-
const initialData = fileManager.readChatDataFromFile()
|
|
49
|
-
const cleanedData = fileManager.cleanExcessMessages(initialData)
|
|
50
|
-
|
|
51
|
-
// 如果清理了数据,立即写回文件
|
|
52
|
-
const originalCount = Object.values(initialData.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
53
|
-
const cleanedCount = Object.values(cleanedData.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
54
|
-
|
|
55
|
-
if (originalCount !== cleanedCount) {
|
|
56
|
-
fileManager.writeChatDataToFile(cleanedData)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// 日志调试函数
|
|
60
|
-
function logInfo(...args: any[]) {
|
|
61
|
-
if (config.loggerinfo) {
|
|
62
|
-
(logger.info as (...args: any[]) => void)(...args)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
logInfo('插件加载完成,数据统计:', {
|
|
67
|
-
机器人数量: Object.keys(cleanedData.bots).length,
|
|
68
|
-
频道数量: Object.keys(cleanedData.channels).reduce((total, botId) =>
|
|
69
|
-
total + Object.keys(cleanedData.channels[botId] || {}).length, 0),
|
|
70
|
-
消息频道数: Object.keys(cleanedData.messages).length,
|
|
71
|
-
总消息数: Object.values(cleanedData.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
// 监听用户消息
|
|
75
|
-
ctx.on('message', async (session) => {
|
|
76
|
-
// 检查平台是否被屏蔽
|
|
77
|
-
if (utils.isPlatformBlocked(session.platform || 'unknown')) {
|
|
78
|
-
logInfo(`忽略来自被屏蔽平台的消息: ${session.platform}`)
|
|
79
|
-
return
|
|
80
|
-
}
|
|
81
|
-
// logInfo(session)
|
|
82
|
-
// 广播消息事件给前端处理
|
|
83
|
-
await messageHandler.broadcastMessageEvent(session)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
// 监听机器人发送的消息
|
|
87
|
-
ctx.on('before-send', async (session) => {
|
|
88
|
-
// 检查平台是否被屏蔽
|
|
89
|
-
if (utils.isPlatformBlocked(session.platform || 'unknown')) {
|
|
90
|
-
logInfo(`忽略来自被屏蔽平台的机器人消息: ${session.platform}`)
|
|
91
|
-
return
|
|
92
|
-
}
|
|
93
|
-
// 广播机器人消息事件给前端处理
|
|
94
|
-
await messageHandler.broadcastBotMessageEvent(session)
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
// 插件启动时设置定期清理过期消息
|
|
98
|
-
ctx.on('ready', async () => {
|
|
99
|
-
logInfo('插件启动完成,开始监听消息')
|
|
100
|
-
|
|
101
|
-
// 定期清理超量消息
|
|
102
|
-
setInterval(() => {
|
|
103
|
-
const data = fileManager.readChatDataFromFile()
|
|
104
|
-
const cleanedData = fileManager.cleanExcessMessages(data)
|
|
105
|
-
|
|
106
|
-
// 如果有消息被清理,写回文件
|
|
107
|
-
const originalCount = Object.values(data.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
108
|
-
const cleanedCount = Object.values(cleanedData.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
109
|
-
|
|
110
|
-
if (originalCount !== cleanedCount) {
|
|
111
|
-
fileManager.writeChatDataToFile(cleanedData)
|
|
112
|
-
logInfo('定期清理完成,清理了', originalCount - cleanedCount, '条超量消息')
|
|
113
|
-
}
|
|
114
|
-
}, 300000) // 每5分钟清理一次
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
// 注册所有 API 处理器
|
|
118
|
-
apiHandlers.registerApiHandlers()
|
|
119
|
-
|
|
120
|
-
// 注册控制台页面
|
|
121
|
-
ctx.console.addEntry({
|
|
122
|
-
dev: path.resolve(__dirname, '../client/index.ts'),
|
|
123
|
-
prod: path.resolve(__dirname, '../dist'),
|
|
124
|
-
})
|
|
125
|
-
}
|
|
1
|
+
|
|
2
|
+
import { Console } from '@koishijs/console'
|
|
3
|
+
import { Context, h } from 'koishi'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
|
|
6
|
+
import { MessageHandler } from './message-handler'
|
|
7
|
+
import { FileManager } from './file-manager'
|
|
8
|
+
import { ApiHandlers } from './api-handlers'
|
|
9
|
+
import { Config } from './config'
|
|
10
|
+
import { Utils } from './utils'
|
|
11
|
+
|
|
12
|
+
export const name = 'chat-patch'
|
|
13
|
+
export const reusable = false
|
|
14
|
+
export const filter = true
|
|
15
|
+
export const inject = {
|
|
16
|
+
required: ['console']
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare module 'koishi' {
|
|
20
|
+
interface Context {
|
|
21
|
+
console: Console
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const usage = `
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
开启后,即可在koishi控制台操作机器人收发消息啦
|
|
30
|
+
|
|
31
|
+
暂时只支持接受图文消息 / 发送文字消息
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
`
|
|
35
|
+
|
|
36
|
+
export { Config } from './config'
|
|
37
|
+
|
|
38
|
+
export async function apply(ctx: Context, config: Config) {
|
|
39
|
+
const logger = ctx.logger('chat-patch')
|
|
40
|
+
|
|
41
|
+
// 初始化各个模块
|
|
42
|
+
const fileManager = new FileManager(ctx, config)
|
|
43
|
+
const messageHandler = new MessageHandler(ctx, config, fileManager)
|
|
44
|
+
const apiHandlers = new ApiHandlers(ctx, config, fileManager, messageHandler)
|
|
45
|
+
const utils = new Utils(config)
|
|
46
|
+
|
|
47
|
+
// 初始化数据
|
|
48
|
+
const initialData = fileManager.readChatDataFromFile()
|
|
49
|
+
const cleanedData = fileManager.cleanExcessMessages(initialData)
|
|
50
|
+
|
|
51
|
+
// 如果清理了数据,立即写回文件
|
|
52
|
+
const originalCount = Object.values(initialData.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
53
|
+
const cleanedCount = Object.values(cleanedData.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
54
|
+
|
|
55
|
+
if (originalCount !== cleanedCount) {
|
|
56
|
+
fileManager.writeChatDataToFile(cleanedData)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 日志调试函数
|
|
60
|
+
function logInfo(...args: any[]) {
|
|
61
|
+
if (config.loggerinfo) {
|
|
62
|
+
(logger.info as (...args: any[]) => void)(...args)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
logInfo('插件加载完成,数据统计:', {
|
|
67
|
+
机器人数量: Object.keys(cleanedData.bots).length,
|
|
68
|
+
频道数量: Object.keys(cleanedData.channels).reduce((total, botId) =>
|
|
69
|
+
total + Object.keys(cleanedData.channels[botId] || {}).length, 0),
|
|
70
|
+
消息频道数: Object.keys(cleanedData.messages).length,
|
|
71
|
+
总消息数: Object.values(cleanedData.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
// 监听用户消息
|
|
75
|
+
ctx.on('message', async (session) => {
|
|
76
|
+
// 检查平台是否被屏蔽
|
|
77
|
+
if (utils.isPlatformBlocked(session.platform || 'unknown')) {
|
|
78
|
+
logInfo(`忽略来自被屏蔽平台的消息: ${session.platform}`)
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
// logInfo(session)
|
|
82
|
+
// 广播消息事件给前端处理
|
|
83
|
+
await messageHandler.broadcastMessageEvent(session)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// 监听机器人发送的消息
|
|
87
|
+
ctx.on('before-send', async (session) => {
|
|
88
|
+
// 检查平台是否被屏蔽
|
|
89
|
+
if (utils.isPlatformBlocked(session.platform || 'unknown')) {
|
|
90
|
+
logInfo(`忽略来自被屏蔽平台的机器人消息: ${session.platform}`)
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
// 广播机器人消息事件给前端处理
|
|
94
|
+
await messageHandler.broadcastBotMessageEvent(session)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
// 插件启动时设置定期清理过期消息
|
|
98
|
+
ctx.on('ready', async () => {
|
|
99
|
+
logInfo('插件启动完成,开始监听消息')
|
|
100
|
+
|
|
101
|
+
// 定期清理超量消息
|
|
102
|
+
setInterval(() => {
|
|
103
|
+
const data = fileManager.readChatDataFromFile()
|
|
104
|
+
const cleanedData = fileManager.cleanExcessMessages(data)
|
|
105
|
+
|
|
106
|
+
// 如果有消息被清理,写回文件
|
|
107
|
+
const originalCount = Object.values(data.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
108
|
+
const cleanedCount = Object.values(cleanedData.messages).reduce((total, msgs) => total + msgs.length, 0)
|
|
109
|
+
|
|
110
|
+
if (originalCount !== cleanedCount) {
|
|
111
|
+
fileManager.writeChatDataToFile(cleanedData)
|
|
112
|
+
logInfo('定期清理完成,清理了', originalCount - cleanedCount, '条超量消息')
|
|
113
|
+
}
|
|
114
|
+
}, 300000) // 每5分钟清理一次
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
// 注册所有 API 处理器
|
|
118
|
+
apiHandlers.registerApiHandlers()
|
|
119
|
+
|
|
120
|
+
// 注册控制台页面
|
|
121
|
+
ctx.console.addEntry({
|
|
122
|
+
dev: path.resolve(__dirname, '../client/index.ts'),
|
|
123
|
+
prod: path.resolve(__dirname, '../dist'),
|
|
124
|
+
})
|
|
125
|
+
}
|