koishi-plugin-chat-patch 2.1.2 → 2.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 +90 -90
- package/client/index.ts +25 -25
- package/client/vue/chat-logic.ts +683 -678
- package/client/vue/composables/useChatActions.ts +1 -1
- package/client/vue/composables/useChatData.ts +1 -1
- package/client/vue/composables/useImageCache.ts +1 -1
- package/client/vue/composables/useVideoCache.ts +130 -0
- package/client/vue/index.vue +658 -612
- package/client/vue/types.ts +1 -1
- package/dist/index.js +2 -2
- package/lib/api-handlers.d.ts +1 -0
- package/lib/file-manager.d.ts +25 -0
- package/lib/index.js +362 -200
- package/lib/message-handler.d.ts +7 -5
- package/lib/utils.d.ts +13 -0
- package/package.json +52 -52
- package/readme.md +25 -25
- package/src/api-handlers.ts +704 -704
- package/src/config.ts +45 -46
- package/src/file-manager.ts +215 -169
- package/src/index.ts +175 -125
- package/src/message-handler.ts +440 -407
- package/src/types.ts +62 -62
- package/src/utils.ts +151 -144
package/src/message-handler.ts
CHANGED
|
@@ -1,407 +1,440 @@
|
|
|
1
|
-
import { BotInfo, ChannelInfo, MessageInfo, QuoteInfo } from './types'
|
|
2
|
-
import { Context, Session, h, Logger } from 'koishi'
|
|
3
|
-
import { FileManager } from './file-manager'
|
|
4
|
-
import { Config } from './config'
|
|
5
|
-
import { Utils } from './utils'
|
|
6
|
-
import { } from '@koishijs/plugin-console'
|
|
7
|
-
|
|
8
|
-
export class MessageHandler {
|
|
9
|
-
private logger: Logger
|
|
10
|
-
private utils: Utils
|
|
11
|
-
|
|
12
|
-
private correctChannelIds: Map<string, string> = new Map()
|
|
13
|
-
|
|
14
|
-
constructor(
|
|
15
|
-
private ctx: Context,
|
|
16
|
-
private config: Config,
|
|
17
|
-
private fileManager: FileManager
|
|
18
|
-
) {
|
|
19
|
-
this.logger = ctx.logger('chat-patch')
|
|
20
|
-
this.utils = new Utils(config)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
this.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
content
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
1
|
+
import { BotInfo, ChannelInfo, MessageInfo, QuoteInfo } from './types'
|
|
2
|
+
import { Context, Session, h, Logger } from 'koishi'
|
|
3
|
+
import { FileManager } from './file-manager'
|
|
4
|
+
import { Config } from './config'
|
|
5
|
+
import { Utils } from './utils'
|
|
6
|
+
import { } from '@koishijs/plugin-console'
|
|
7
|
+
|
|
8
|
+
export class MessageHandler {
|
|
9
|
+
private logger: Logger
|
|
10
|
+
private utils: Utils
|
|
11
|
+
|
|
12
|
+
private correctChannelIds: Map<string, string> = new Map()
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
private ctx: Context,
|
|
16
|
+
private config: Config,
|
|
17
|
+
private fileManager: FileManager
|
|
18
|
+
) {
|
|
19
|
+
this.logger = ctx.logger('chat-patch')
|
|
20
|
+
this.utils = new Utils(config)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
recordUserMessage(session: Session, timestamp: number) {
|
|
24
|
+
|
|
25
|
+
setImmediate(() => {
|
|
26
|
+
this.processUserMessage(session, timestamp).catch(error => {
|
|
27
|
+
this.logger.error('记录用户消息失败:', error)
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
recordBotMessage(session: Session, timestamp: number) {
|
|
33
|
+
|
|
34
|
+
setImmediate(() => {
|
|
35
|
+
this.processBotMessage(session, timestamp).catch(error => {
|
|
36
|
+
this.logger.error('记录机器人消息失败:', error)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
setCorrectChannelId(selfId: string, channelId: string) {
|
|
42
|
+
this.correctChannelIds.set(selfId, channelId)
|
|
43
|
+
this.logInfo('设置正确的 channelId:', { selfId, channelId })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getCorrectChannelId(selfId: string): string | undefined {
|
|
47
|
+
return this.correctChannelIds.get(selfId)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
updateBotInfoToFile(session: Session) {
|
|
51
|
+
|
|
52
|
+
setImmediate(() => {
|
|
53
|
+
try {
|
|
54
|
+
const data = this.fileManager.readChatDataFromFile()
|
|
55
|
+
|
|
56
|
+
const botInfo: BotInfo = {
|
|
57
|
+
selfId: session.selfId,
|
|
58
|
+
platform: session.platform || 'unknown',
|
|
59
|
+
username: session.bot.user?.name || `Bot-${session.selfId}`,
|
|
60
|
+
avatar: session.bot.user?.avatar,
|
|
61
|
+
status: 'online'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
data.bots[session.selfId] = botInfo
|
|
65
|
+
this.fileManager.writeChatDataToFile(data)
|
|
66
|
+
this.logInfo('更新机器人信息到文件:', botInfo.username)
|
|
67
|
+
} catch (error) {
|
|
68
|
+
this.logger.error('更新机器人信息失败:', error)
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
updateChannelInfoToFile(session: Session): string {
|
|
74
|
+
const isDirect = session.isDirect || session.channelId?.includes('private')
|
|
75
|
+
const directUserName = session.username || session.event?.user?.name || session.userId
|
|
76
|
+
|
|
77
|
+
const data = this.fileManager.readChatDataFromFile()
|
|
78
|
+
const existingChannel = data.channels[session.selfId]?.[session.channelId]
|
|
79
|
+
|
|
80
|
+
let immediateName = session.channelId
|
|
81
|
+
if (isDirect) {
|
|
82
|
+
if (directUserName && directUserName !== session.userId) {
|
|
83
|
+
immediateName = `私聊(${directUserName})`
|
|
84
|
+
} else if (existingChannel?.name && !existingChannel.name.includes('未知')) {
|
|
85
|
+
immediateName = existingChannel.name
|
|
86
|
+
} else if (session.platform && session.platform.toLowerCase().includes('sandbox')) {
|
|
87
|
+
immediateName = `私聊(${session.userId})`
|
|
88
|
+
} else {
|
|
89
|
+
immediateName = '私聊(未知用户)'
|
|
90
|
+
}
|
|
91
|
+
} else if (existingChannel?.guildName) {
|
|
92
|
+
immediateName = existingChannel.guildName
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
setImmediate(async () => {
|
|
96
|
+
try {
|
|
97
|
+
let guildName = session.channelId
|
|
98
|
+
|
|
99
|
+
if (!isDirect) {
|
|
100
|
+
try {
|
|
101
|
+
|
|
102
|
+
if (session.guildId && session.bot.getGuild && typeof session.bot.getGuild === 'function') {
|
|
103
|
+
const guild = await session.bot.getGuild(session.guildId)
|
|
104
|
+
guildName = guild?.name || session.channelId
|
|
105
|
+
} else if (session.guildId && !session.bot.getGuild && session.bot.getChannel && typeof session.bot.getChannel === 'function') {
|
|
106
|
+
try {
|
|
107
|
+
const channel = await session.bot.getChannel(session.guildId)
|
|
108
|
+
guildName = channel?.name || session.channelId
|
|
109
|
+
} catch (channelError) {
|
|
110
|
+
this.logInfo('获取频道信息失败,使用频道ID作为备用:', channelError)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} catch (error) {
|
|
114
|
+
this.logInfo('获取频道信息失败,使用频道ID作为备用:', error)
|
|
115
|
+
guildName = session.channelId
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const freshData = this.fileManager.readChatDataFromFile()
|
|
120
|
+
if (!freshData.channels[session.selfId]) {
|
|
121
|
+
freshData.channels[session.selfId] = {}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const existingChannel = freshData.channels[session.selfId][session.channelId]
|
|
125
|
+
|
|
126
|
+
let finalName: string
|
|
127
|
+
if (isDirect) {
|
|
128
|
+
if (directUserName && directUserName !== session.userId) {
|
|
129
|
+
finalName = `私聊(${directUserName})`
|
|
130
|
+
} else if (existingChannel?.name && !existingChannel.name.includes('未知')) {
|
|
131
|
+
finalName = existingChannel.name
|
|
132
|
+
} else if (session.platform && session.platform.toLowerCase().includes('sandbox')) {
|
|
133
|
+
finalName = `私聊(${session.userId})`
|
|
134
|
+
} else {
|
|
135
|
+
finalName = '私聊(未知用户)'
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
finalName = guildName || session.channelId
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const channelInfo: ChannelInfo = {
|
|
142
|
+
id: session.channelId,
|
|
143
|
+
name: finalName,
|
|
144
|
+
type: session.type || 0,
|
|
145
|
+
channelId: session.channelId,
|
|
146
|
+
guildName: guildName,
|
|
147
|
+
isDirect: !!isDirect
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (existingChannel && existingChannel.name !== finalName) {
|
|
151
|
+
this.logInfo('更新频道名称:', {
|
|
152
|
+
channelId: session.channelId,
|
|
153
|
+
oldName: existingChannel.name,
|
|
154
|
+
newName: finalName
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
freshData.channels[session.selfId][session.channelId] = channelInfo
|
|
159
|
+
this.fileManager.writeChatDataToFile(freshData)
|
|
160
|
+
this.logInfo('更新频道信息到文件:', channelInfo.name)
|
|
161
|
+
} catch (error) {
|
|
162
|
+
this.logger.error('异步更新频道信息失败:', error)
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
return immediateName
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
public async downloadAndCacheMedia(url: string, type: 'image' | 'media' | 'avatar') {
|
|
170
|
+
try {
|
|
171
|
+
if (!url || url.startsWith('data:') || url.startsWith('file:')) return url
|
|
172
|
+
|
|
173
|
+
let folder = 'media'
|
|
174
|
+
if (type === 'image') folder = 'images'
|
|
175
|
+
else if (type === 'avatar') folder = 'avatars'
|
|
176
|
+
|
|
177
|
+
const dir = require('node:path').join(this.ctx.baseDir, 'data', 'chat-patch', 'persist-media', folder)
|
|
178
|
+
if (!require('node:fs').existsSync(dir)) {
|
|
179
|
+
require('node:fs').mkdirSync(dir, { recursive: true })
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const crypto = require('node:crypto')
|
|
183
|
+
const hash = crypto.createHash('md5').update(url).digest('hex')
|
|
184
|
+
const ext = require('node:path').extname(new URL(url).pathname) || (type === 'image' ? '.jpg' : '.mp4')
|
|
185
|
+
const filename = `${hash}${ext}`
|
|
186
|
+
const filePath = require('node:path').join(dir, filename)
|
|
187
|
+
|
|
188
|
+
if (require('node:fs').existsSync(filePath)) {
|
|
189
|
+
return require('node:url').pathToFileURL(filePath).href
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const buffer = await this.ctx.http.get(url, { responseType: 'arraybuffer' })
|
|
193
|
+
require('node:fs').writeFileSync(filePath, Buffer.from(buffer))
|
|
194
|
+
|
|
195
|
+
return require('node:url').pathToFileURL(filePath).href
|
|
196
|
+
} catch (e) {
|
|
197
|
+
return url
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private processMediaElementsAsync(elements: h[], isUserMessage: boolean = true) {
|
|
202
|
+
if (!elements) return
|
|
203
|
+
|
|
204
|
+
setImmediate(async () => {
|
|
205
|
+
try {
|
|
206
|
+
for (const el of elements) {
|
|
207
|
+
if (['image', 'img', 'mface'].includes(el.type)) {
|
|
208
|
+
const src = el.attrs.src || el.attrs.url || el.attrs.file
|
|
209
|
+
if (src && isUserMessage) {
|
|
210
|
+
|
|
211
|
+
this.downloadAndCacheMedia(src, 'image').catch(e => {
|
|
212
|
+
this.logger.warn('缓存图片失败:', e)
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
} else if (el.type === 'audio') {
|
|
216
|
+
const src = el.attrs.src || el.attrs.url || el.attrs.file
|
|
217
|
+
if (src && isUserMessage) {
|
|
218
|
+
|
|
219
|
+
this.downloadAndCacheMedia(src, 'media').catch(e => {
|
|
220
|
+
this.logger.warn('缓存语音失败:', e)
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (el.children) this.processMediaElementsAsync(el.children, isUserMessage)
|
|
226
|
+
}
|
|
227
|
+
} catch (error) {
|
|
228
|
+
this.logger.error('处理媒体元素失败:', error)
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private async processUserMessage(session: Session, timestamp?: number) {
|
|
234
|
+
try {
|
|
235
|
+
if (!timestamp) timestamp = Date.now()
|
|
236
|
+
|
|
237
|
+
this.updateBotInfoToFile(session)
|
|
238
|
+
|
|
239
|
+
const guildName = this.updateChannelInfoToFile(session)
|
|
240
|
+
const isDirect = session.isDirect || session.channelId?.includes('private')
|
|
241
|
+
|
|
242
|
+
if (session.elements) {
|
|
243
|
+
this.processMediaElementsAsync(session.elements, true)
|
|
244
|
+
}
|
|
245
|
+
if (session.quote?.elements) {
|
|
246
|
+
this.processMediaElementsAsync(session.quote.elements, true)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
let quoteInfo: QuoteInfo | undefined = undefined
|
|
250
|
+
if (session.quote) {
|
|
251
|
+
quoteInfo = {
|
|
252
|
+
messageId: session.quote.messageId || session.quote.id,
|
|
253
|
+
id: session.quote.id,
|
|
254
|
+
content: session.quote.content || '',
|
|
255
|
+
elements: session.quote.elements,
|
|
256
|
+
user: {
|
|
257
|
+
id: session.quote.user?.id || session.quote.user?.userId || 'unknown',
|
|
258
|
+
name: session.quote.user?.name || session.quote.user?.username || 'unknown',
|
|
259
|
+
userId: session.quote.user?.userId || session.quote.user?.id || 'unknown',
|
|
260
|
+
avatar: session.quote.user?.avatar,
|
|
261
|
+
username: session.quote.user?.username || session.quote.user?.name || 'unknown'
|
|
262
|
+
},
|
|
263
|
+
timestamp: session.quote.timestamp || Date.now()
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
let content = ''
|
|
268
|
+
let elements: h[] = []
|
|
269
|
+
|
|
270
|
+
if (session.content) {
|
|
271
|
+
content = session.content
|
|
272
|
+
} else if (session.stripped?.content) {
|
|
273
|
+
content = session.stripped.content
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (session.elements) {
|
|
277
|
+
elements = session.elements
|
|
278
|
+
if (!content) {
|
|
279
|
+
content = session.elements
|
|
280
|
+
.filter((element: any) => element.type === 'text')
|
|
281
|
+
.map((element: any) => element.attrs?.content || '')
|
|
282
|
+
.join('')
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const messageInfo: MessageInfo = {
|
|
287
|
+
id: session.event?.message?.id || `msg-${timestamp}`,
|
|
288
|
+
content: content || session.content || '',
|
|
289
|
+
userId: session.userId || session.event?.user?.id || 'unknown',
|
|
290
|
+
username: session.username || session.event?.user?.name || session.userId || 'unknown',
|
|
291
|
+
avatar: session.event?.user?.avatar,
|
|
292
|
+
timestamp: timestamp,
|
|
293
|
+
channelId: session.channelId,
|
|
294
|
+
selfId: session.selfId,
|
|
295
|
+
elements: this.utils.cleanBase64Content(elements, false),
|
|
296
|
+
type: 'user',
|
|
297
|
+
guildName: guildName,
|
|
298
|
+
platform: session.platform || 'unknown',
|
|
299
|
+
quote: quoteInfo ? this.utils.cleanBase64Content(quoteInfo, false) : undefined,
|
|
300
|
+
isDirect: !!isDirect
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
await this.fileManager.addMessageToFile(messageInfo)
|
|
304
|
+
|
|
305
|
+
const messageEvent = {
|
|
306
|
+
type: 'message',
|
|
307
|
+
selfId: session.selfId,
|
|
308
|
+
platform: session.platform || 'unknown',
|
|
309
|
+
channelId: session.channelId,
|
|
310
|
+
messageId: session.event?.message?.id || `msg-${timestamp}`,
|
|
311
|
+
content: content || session.content || '',
|
|
312
|
+
userId: session.userId || session.event?.user?.id || 'unknown',
|
|
313
|
+
username: session.username || session.event?.user?.name || session.userId || 'unknown',
|
|
314
|
+
avatar: session.event?.user?.avatar,
|
|
315
|
+
timestamp: timestamp,
|
|
316
|
+
guildName: guildName,
|
|
317
|
+
channelType: session.type || 0,
|
|
318
|
+
elements: this.utils.cleanBase64Content(elements, false),
|
|
319
|
+
quote: quoteInfo ? this.utils.cleanBase64Content(quoteInfo, false) : undefined,
|
|
320
|
+
isDirect: session.isDirect,
|
|
321
|
+
bot: {
|
|
322
|
+
avatar: session.bot.user?.avatar,
|
|
323
|
+
name: session.bot.user?.name,
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
this.ctx.console.broadcast('chat-message-event', messageEvent)
|
|
328
|
+
} catch (error) {
|
|
329
|
+
this.logger.error('处理用户消息失败:', error)
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private async processBotMessage(session: Session, timestamp?: number) {
|
|
334
|
+
try {
|
|
335
|
+
if (!timestamp) timestamp = Date.now()
|
|
336
|
+
|
|
337
|
+
const correctChannelId = this.getCorrectChannelId(session.selfId)
|
|
338
|
+
const finalChannelId = correctChannelId || session.channelId
|
|
339
|
+
|
|
340
|
+
this.updateBotInfoToFile(session)
|
|
341
|
+
|
|
342
|
+
const guildName = this.updateChannelInfoToFile(session)
|
|
343
|
+
const isDirect = session.isDirect || finalChannelId?.includes('private')
|
|
344
|
+
|
|
345
|
+
let content = session.content || ''
|
|
346
|
+
|
|
347
|
+
if (!content && session.event?.message?.elements) {
|
|
348
|
+
content = this.utils.extractTextContent(session.event.message.elements).trim()
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let quoteInfo: QuoteInfo | undefined = undefined
|
|
352
|
+
const quoteMatch = content.match(/<quote id="([^"]+)"\/>/)
|
|
353
|
+
if (quoteMatch) {
|
|
354
|
+
const quoteId = quoteMatch[1]
|
|
355
|
+
const data = this.fileManager.readChatDataFromFile()
|
|
356
|
+
const channelKey = `${session.selfId}:${finalChannelId}`
|
|
357
|
+
const quotedMsg = data.messages[channelKey]?.find(m => m.id === quoteId)
|
|
358
|
+
|
|
359
|
+
if (quotedMsg) {
|
|
360
|
+
const realId = quotedMsg.id.startsWith('bot-msg-') ? (quotedMsg as any).realId : quotedMsg.id
|
|
361
|
+
|
|
362
|
+
quoteInfo = {
|
|
363
|
+
messageId: realId || quotedMsg.id,
|
|
364
|
+
id: realId || quotedMsg.id,
|
|
365
|
+
content: quotedMsg.content,
|
|
366
|
+
elements: quotedMsg.elements,
|
|
367
|
+
user: {
|
|
368
|
+
id: quotedMsg.userId,
|
|
369
|
+
name: quotedMsg.username,
|
|
370
|
+
userId: quotedMsg.userId,
|
|
371
|
+
avatar: quotedMsg.avatar,
|
|
372
|
+
username: quotedMsg.username
|
|
373
|
+
},
|
|
374
|
+
timestamp: quotedMsg.timestamp
|
|
375
|
+
}
|
|
376
|
+
content = content.replace(/<quote id="[^"]+"\/>\s*/, '')
|
|
377
|
+
|
|
378
|
+
if (realId) {
|
|
379
|
+
session.content = session.content.replace(/id="[^"]+"/, `id="${realId}"`)
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// 创建机器人消息信息对象
|
|
385
|
+
const messageInfo: MessageInfo = {
|
|
386
|
+
id: `bot-msg-${timestamp}`,
|
|
387
|
+
content: content,
|
|
388
|
+
userId: session.selfId,
|
|
389
|
+
username: session.bot.user?.name || `Bot-${session.selfId}`,
|
|
390
|
+
avatar: session.bot.user?.avatar,
|
|
391
|
+
timestamp: timestamp,
|
|
392
|
+
channelId: finalChannelId,
|
|
393
|
+
selfId: session.selfId,
|
|
394
|
+
elements: this.utils.cleanBase64Content(session.event?.message?.elements, true),
|
|
395
|
+
type: 'bot',
|
|
396
|
+
guildName: guildName,
|
|
397
|
+
platform: session.platform || 'unknown',
|
|
398
|
+
quote: quoteInfo,
|
|
399
|
+
isDirect: !!isDirect,
|
|
400
|
+
sending: true // 标记为正在发送
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// 异步保存消息(不阻塞)
|
|
404
|
+
await this.fileManager.addMessageToFile(messageInfo)
|
|
405
|
+
|
|
406
|
+
const messageEvent = {
|
|
407
|
+
type: 'bot-message',
|
|
408
|
+
selfId: session.selfId,
|
|
409
|
+
platform: session.platform || 'unknown',
|
|
410
|
+
channelId: finalChannelId,
|
|
411
|
+
messageId: `bot-msg-${timestamp}`,
|
|
412
|
+
content: content,
|
|
413
|
+
userId: session.selfId,
|
|
414
|
+
username: session.bot.user?.name || `Bot-${session.selfId}`,
|
|
415
|
+
avatar: session.bot.user?.avatar,
|
|
416
|
+
timestamp: timestamp,
|
|
417
|
+
guildName: guildName,
|
|
418
|
+
channelType: session.event?.channel?.type || session.type || 0,
|
|
419
|
+
elements: this.utils.cleanBase64Content(session.event?.message?.elements, true),
|
|
420
|
+
quote: quoteInfo,
|
|
421
|
+
isDirect: !!isDirect,
|
|
422
|
+
sending: true,
|
|
423
|
+
bot: {
|
|
424
|
+
avatar: session.bot.user?.avatar,
|
|
425
|
+
name: session.bot.user?.name,
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
this.ctx.console.broadcast('chat-bot-message-event', messageEvent)
|
|
430
|
+
} catch (error) {
|
|
431
|
+
this.logger.error('处理机器人消息失败:', error)
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
private logInfo(...args: any[]) {
|
|
436
|
+
if (this.config.loggerinfo) {
|
|
437
|
+
(this.logger.info as (...args: any[]) => void)(...args)
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|