koishi-plugin-chat-patch 1.0.13 → 1.0.15

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/lib/index.js CHANGED
@@ -138,6 +138,17 @@ var MessageHandler = class {
138
138
  }
139
139
  logger;
140
140
  utils;
141
+ // 存储正确的 channelId 映射,key 是 selfId,value 是正确的 channelId
142
+ correctChannelIds = /* @__PURE__ */ new Map();
143
+ // 设置正确的 channelId
144
+ setCorrectChannelId(selfId, channelId) {
145
+ this.correctChannelIds.set(selfId, channelId);
146
+ this.logInfo("设置正确的 channelId:", { selfId, channelId });
147
+ }
148
+ // 获取正确的 channelId
149
+ getCorrectChannelId(selfId) {
150
+ return this.correctChannelIds.get(selfId);
151
+ }
141
152
  // 更新机器人信息到JSON文件
142
153
  async updateBotInfoToFile(session) {
143
154
  const data = this.fileManager.readChatDataFromFile();
@@ -170,10 +181,11 @@ var MessageHandler = class {
170
181
  }
171
182
  const channelInfo = {
172
183
  id: session.channelId,
173
- name: session.guildId ? `${guildName} (${session.channelId})` : `私信 ${session.channelId}`,
184
+ name: session.isDirect ? `私信 ${session.channelId}` : `${guildName} (${session.channelId})`,
174
185
  type: session.type || 0,
175
- guildId: session.guildId,
176
- guildName
186
+ channelId: session.channelId,
187
+ guildName,
188
+ isDirect: session.isDirect
177
189
  };
178
190
  data.channels[session.selfId][session.channelId] = channelInfo;
179
191
  this.fileManager.writeChatDataToFile(data);
@@ -226,10 +238,10 @@ var MessageHandler = class {
226
238
  selfId: session.selfId,
227
239
  elements: this.utils.cleanBase64Content(elements),
228
240
  type: "user",
229
- guildId: session.guildId,
230
241
  guildName,
231
242
  platform: session.platform || "unknown",
232
- quote: quoteInfo ? this.utils.cleanBase64Content(quoteInfo) : void 0
243
+ quote: quoteInfo ? this.utils.cleanBase64Content(quoteInfo) : void 0,
244
+ isDirect: session.isDirect
233
245
  };
234
246
  await this.fileManager.addMessageToFile(messageInfo);
235
247
  const messageEvent = {
@@ -243,11 +255,11 @@ var MessageHandler = class {
243
255
  username: session.username || session.event?.user?.name || session.userId || "unknown",
244
256
  avatar: session.event?.user?.avatar,
245
257
  timestamp,
246
- guildId: session.guildId,
247
258
  guildName,
248
259
  channelType: session.type || 0,
249
260
  elements: this.utils.cleanBase64Content(elements),
250
261
  quote: quoteInfo ? this.utils.cleanBase64Content(quoteInfo) : void 0,
262
+ isDirect: session.isDirect,
251
263
  bot: {
252
264
  avatar: session.bot.user?.avatar,
253
265
  name: session.bot.user?.name
@@ -261,6 +273,16 @@ var MessageHandler = class {
261
273
  // 处理机器人发送的消息
262
274
  async broadcastBotMessageEvent(session) {
263
275
  try {
276
+ const correctChannelId = this.getCorrectChannelId(session.selfId);
277
+ this.logInfo("机器人发送消息调试信息:", {
278
+ "session.channelId": session.channelId,
279
+ "session.event?.channel?.id": session.event?.channel?.id,
280
+ "correctChannelId": correctChannelId,
281
+ "session.guildId": session.guildId,
282
+ "session.isDirect": session.isDirect,
283
+ "session.platform": session.platform
284
+ });
285
+ const finalChannelId = correctChannelId || session.channelId;
264
286
  await this.updateBotInfoToFile(session);
265
287
  const guildName = await this.updateChannelInfoToFile(session);
266
288
  const timestamp = Date.now();
@@ -275,30 +297,30 @@ var MessageHandler = class {
275
297
  username: session.bot.user?.name || `Bot-${session.selfId}`,
276
298
  avatar: session.bot.user?.avatar,
277
299
  timestamp,
278
- channelId: session.event?.channel?.id || session.channelId,
300
+ channelId: finalChannelId,
279
301
  selfId: session.selfId,
280
302
  elements: this.utils.cleanBase64Content(session.event?.message?.elements),
281
303
  type: "bot",
282
- guildId: session.event?.guild?.id || session.guildId,
283
304
  guildName,
284
- platform: session.platform || "unknown"
305
+ platform: session.platform || "unknown",
306
+ isDirect: session.isDirect
285
307
  };
286
308
  await this.fileManager.addMessageToFile(messageInfo);
287
309
  const messageEvent = {
288
310
  type: "bot-message",
289
311
  selfId: session.selfId,
290
312
  platform: session.platform || "unknown",
291
- channelId: session.event?.channel?.id || session.channelId,
313
+ channelId: finalChannelId,
292
314
  messageId: `bot-msg-${timestamp}`,
293
315
  content,
294
316
  userId: session.selfId,
295
317
  username: session.bot.user?.name || `Bot-${session.selfId}`,
296
318
  avatar: session.bot.user?.avatar,
297
319
  timestamp,
298
- guildId: session.event?.guild?.id || session.guildId,
299
320
  guildName,
300
321
  channelType: session.event?.channel?.type || session.type || 0,
301
322
  elements: this.utils.cleanBase64Content(session.event?.message?.elements),
323
+ isDirect: session.isDirect,
302
324
  bot: {
303
325
  avatar: session.bot.user?.avatar,
304
326
  name: session.bot.user?.name
@@ -310,11 +332,14 @@ var MessageHandler = class {
310
332
  this.logInfo("机器人发送消息 (before-send):", {
311
333
  selfId: session.selfId,
312
334
  channelId: messageEvent.channelId,
335
+ originalChannelId: session.channelId,
336
+ correctedChannelId: finalChannelId,
313
337
  content,
314
338
  platform: session.platform,
315
339
  imageCount: imageElements.length,
316
340
  imageUrls: imageElements.map((el) => el.attrs?.src || el.attrs?.url || el.attrs?.file)
317
341
  });
342
+ this.correctChannelIds.delete(session.selfId);
318
343
  this.ctx.console.broadcast("chat-bot-message-event", messageEvent);
319
344
  } catch (error) {
320
345
  this.logger.error("广播机器人消息事件失败:", error);
@@ -474,10 +499,11 @@ var FileManager = class {
474
499
  var import_koishi = require("koishi");
475
500
  var import_node_url = require("node:url");
476
501
  var ApiHandlers = class {
477
- constructor(ctx, config, fileManager) {
502
+ constructor(ctx, config, fileManager, messageHandler) {
478
503
  this.ctx = ctx;
479
504
  this.config = config;
480
505
  this.fileManager = fileManager;
506
+ this.messageHandler = messageHandler;
481
507
  this.logger = ctx.logger("chat-patch");
482
508
  }
483
509
  static {
@@ -630,6 +656,7 @@ var ApiHandlers = class {
630
656
  }
631
657
  }
632
658
  }
659
+ this.messageHandler.setCorrectChannelId(data.selfId, data.channelId);
633
660
  const result = await bot.sendMessage(data.channelId, messageContent);
634
661
  this.logInfo("消息发送成功:", result);
635
662
  return {
@@ -972,7 +999,7 @@ async function apply(ctx, config) {
972
999
  const logger = ctx.logger("chat-patch");
973
1000
  const fileManager = new FileManager(ctx, config);
974
1001
  const messageHandler = new MessageHandler(ctx, config, fileManager);
975
- const apiHandlers = new ApiHandlers(ctx, config, fileManager);
1002
+ const apiHandlers = new ApiHandlers(ctx, config, fileManager, messageHandler);
976
1003
  const utils = new Utils(config);
977
1004
  const initialData = fileManager.readChatDataFromFile();
978
1005
  const cleanedData = fileManager.cleanExcessMessages(initialData);
@@ -0,0 +1,19 @@
1
+ import { Context, Session } from 'koishi';
2
+ import { FileManager } from './file-manager';
3
+ import { Config } from './config';
4
+ export declare class MessageHandler {
5
+ private ctx;
6
+ private config;
7
+ private fileManager;
8
+ private logger;
9
+ private utils;
10
+ private correctChannelIds;
11
+ constructor(ctx: Context, config: Config, fileManager: FileManager);
12
+ setCorrectChannelId(selfId: string, channelId: string): void;
13
+ getCorrectChannelId(selfId: string): string | undefined;
14
+ updateBotInfoToFile(session: Session): Promise<void>;
15
+ updateChannelInfoToFile(session: Session): Promise<string>;
16
+ broadcastMessageEvent(session: Session): Promise<void>;
17
+ broadcastBotMessageEvent(session: Session): Promise<void>;
18
+ private logInfo;
19
+ }
package/lib/types.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ import { h } from 'koishi';
2
+ export interface BotInfo {
3
+ selfId: string;
4
+ platform: string;
5
+ username: string;
6
+ avatar?: string;
7
+ status: 'online' | 'offline';
8
+ }
9
+ export interface ChannelInfo {
10
+ id: string;
11
+ name: string;
12
+ type: number | string;
13
+ channelId?: string;
14
+ guildName?: string;
15
+ isDirect?: boolean;
16
+ }
17
+ export interface QuoteInfo {
18
+ messageId: string;
19
+ id: string;
20
+ content: string;
21
+ elements?: h[];
22
+ user: {
23
+ id: string;
24
+ name: string;
25
+ userId: string;
26
+ avatar?: string;
27
+ username: string;
28
+ };
29
+ timestamp: number;
30
+ }
31
+ export interface MessageInfo {
32
+ id: string;
33
+ content: string;
34
+ userId: string;
35
+ username: string;
36
+ avatar?: string;
37
+ timestamp: number;
38
+ channelId: string;
39
+ selfId: string;
40
+ elements?: h[];
41
+ type: 'user' | 'bot';
42
+ guildId?: string;
43
+ guildName?: string;
44
+ platform: string;
45
+ quote?: QuoteInfo;
46
+ isDirect?: boolean;
47
+ }
48
+ export interface ChatData {
49
+ bots: Record<string, BotInfo>;
50
+ channels: Record<string, Record<string, ChannelInfo>>;
51
+ messages: Record<string, MessageInfo[]>;
52
+ pinnedBots: string[];
53
+ pinnedChannels: string[];
54
+ lastSaveTime?: number;
55
+ }
package/lib/utils.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Config } from './config';
2
+ export declare class Utils {
3
+ private config;
4
+ constructor(config: Config);
5
+ isPlatformBlocked(platform: string): boolean;
6
+ extractTextContent(elements: any[]): string;
7
+ private isBase64;
8
+ cleanBase64Content(obj: any): any;
9
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chat-patch",
3
3
  "description": "[<ruby>chat-patch<rp>(</rp><rt>点我预览效果</rt><rp>)</rp></ruby>](https://i0.hdslb.com/bfs/openplatform/71074dfc9e5256fc3333d8bd8478bec1af874046.png) 视奸小插件((bushi( (低性能警告)。手机端适配。灵感来自 chat 插件。",
4
- "version": "1.0.13",
4
+ "version": "1.0.15",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -1,4 +1,5 @@
1
1
  import { FileManager } from './file-manager'
2
+ import { MessageHandler } from './message-handler'
2
3
  import { Context, h, Logger } from 'koishi'
3
4
  import { Config } from './config'
4
5
 
@@ -10,7 +11,8 @@ export class ApiHandlers {
10
11
  constructor(
11
12
  private ctx: Context,
12
13
  private config: Config,
13
- private fileManager: FileManager
14
+ private fileManager: FileManager,
15
+ private messageHandler: MessageHandler
14
16
  ) {
15
17
  this.logger = ctx.logger('chat-patch')
16
18
  }
@@ -218,6 +220,9 @@ export class ApiHandlers {
218
220
  }
219
221
  }
220
222
 
223
+ // 在发送消息前,通知 MessageHandler 正确的 channelId
224
+ this.messageHandler.setCorrectChannelId(data.selfId, data.channelId)
225
+
221
226
  const result = await bot.sendMessage(data.channelId, messageContent)
222
227
  this.logInfo('消息发送成功:', result)
223
228
 
package/src/index.ts CHANGED
@@ -35,7 +35,7 @@ export async function apply(ctx: Context, config: Config) {
35
35
  // 初始化各个模块
36
36
  const fileManager = new FileManager(ctx, config)
37
37
  const messageHandler = new MessageHandler(ctx, config, fileManager)
38
- const apiHandlers = new ApiHandlers(ctx, config, fileManager)
38
+ const apiHandlers = new ApiHandlers(ctx, config, fileManager, messageHandler)
39
39
  const utils = new Utils(config)
40
40
 
41
41
  // 初始化数据
@@ -7,6 +7,8 @@ import { Utils } from './utils'
7
7
  export class MessageHandler {
8
8
  private logger: Logger
9
9
  private utils: Utils
10
+ // 存储正确的 channelId 映射,key 是 selfId,value 是正确的 channelId
11
+ private correctChannelIds: Map<string, string> = new Map()
10
12
 
11
13
  constructor(
12
14
  private ctx: Context,
@@ -17,6 +19,17 @@ export class MessageHandler {
17
19
  this.utils = new Utils(config)
18
20
  }
19
21
 
22
+ // 设置正确的 channelId
23
+ setCorrectChannelId(selfId: string, channelId: string) {
24
+ this.correctChannelIds.set(selfId, channelId)
25
+ this.logInfo('设置正确的 channelId:', { selfId, channelId })
26
+ }
27
+
28
+ // 获取正确的 channelId
29
+ getCorrectChannelId(selfId: string): string | undefined {
30
+ return this.correctChannelIds.get(selfId)
31
+ }
32
+
20
33
  // 更新机器人信息到JSON文件
21
34
  async updateBotInfoToFile(session: Session) {
22
35
  const data = this.fileManager.readChatDataFromFile()
@@ -56,12 +69,13 @@ export class MessageHandler {
56
69
 
57
70
  const channelInfo: ChannelInfo = {
58
71
  id: session.channelId,
59
- name: session.guildId
60
- ? `${guildName} (${session.channelId})`
61
- : `私信 ${session.channelId}`,
72
+ name: session.isDirect
73
+ ? `私信 ${session.channelId}`
74
+ : `${guildName} (${session.channelId})`,
62
75
  type: session.type || 0,
63
- guildId: session.guildId,
64
- guildName: guildName
76
+ channelId: session.channelId,
77
+ guildName: guildName,
78
+ isDirect: session.isDirect
65
79
  }
66
80
 
67
81
  data.channels[session.selfId][session.channelId] = channelInfo
@@ -129,10 +143,10 @@ export class MessageHandler {
129
143
  selfId: session.selfId,
130
144
  elements: this.utils.cleanBase64Content(elements),
131
145
  type: 'user',
132
- guildId: session.guildId,
133
146
  guildName: guildName,
134
147
  platform: session.platform || 'unknown',
135
- quote: quoteInfo ? this.utils.cleanBase64Content(quoteInfo) : undefined
148
+ quote: quoteInfo ? this.utils.cleanBase64Content(quoteInfo) : undefined,
149
+ isDirect: session.isDirect
136
150
  }
137
151
 
138
152
  await this.fileManager.addMessageToFile(messageInfo)
@@ -148,11 +162,11 @@ export class MessageHandler {
148
162
  username: session.username || session.event?.user?.name || session.userId || 'unknown',
149
163
  avatar: session.event?.user?.avatar,
150
164
  timestamp: timestamp,
151
- guildId: session.guildId,
152
165
  guildName: guildName,
153
166
  channelType: session.type || 0,
154
167
  elements: this.utils.cleanBase64Content(elements),
155
168
  quote: quoteInfo ? this.utils.cleanBase64Content(quoteInfo) : undefined,
169
+ isDirect: session.isDirect,
156
170
  bot: {
157
171
  avatar: session.bot.user?.avatar,
158
172
  name: session.bot.user?.name,
@@ -168,6 +182,22 @@ export class MessageHandler {
168
182
  // 处理机器人发送的消息
169
183
  async broadcastBotMessageEvent(session: Session) {
170
184
  try {
185
+ // 获取正确的 channelId
186
+ const correctChannelId = this.getCorrectChannelId(session.selfId)
187
+
188
+ // 调试日志:检查 channelId 的来源
189
+ this.logInfo('机器人发送消息调试信息:', {
190
+ 'session.channelId': session.channelId,
191
+ 'session.event?.channel?.id': session.event?.channel?.id,
192
+ 'correctChannelId': correctChannelId,
193
+ 'session.guildId': session.guildId,
194
+ 'session.isDirect': session.isDirect,
195
+ 'session.platform': session.platform
196
+ })
197
+
198
+ // 使用正确的 channelId,如果没有则使用 session.channelId
199
+ const finalChannelId = correctChannelId || session.channelId
200
+
171
201
  await this.updateBotInfoToFile(session)
172
202
  const guildName = await this.updateChannelInfoToFile(session)
173
203
 
@@ -187,13 +217,13 @@ export class MessageHandler {
187
217
  username: session.bot.user?.name || `Bot-${session.selfId}`,
188
218
  avatar: session.bot.user?.avatar,
189
219
  timestamp: timestamp,
190
- channelId: session.event?.channel?.id || session.channelId,
220
+ channelId: finalChannelId,
191
221
  selfId: session.selfId,
192
222
  elements: this.utils.cleanBase64Content(session.event?.message?.elements),
193
223
  type: 'bot',
194
- guildId: session.event?.guild?.id || session.guildId,
195
224
  guildName: guildName,
196
- platform: session.platform || 'unknown'
225
+ platform: session.platform || 'unknown',
226
+ isDirect: session.isDirect
197
227
  }
198
228
 
199
229
  await this.fileManager.addMessageToFile(messageInfo)
@@ -202,17 +232,17 @@ export class MessageHandler {
202
232
  type: 'bot-message',
203
233
  selfId: session.selfId,
204
234
  platform: session.platform || 'unknown',
205
- channelId: session.event?.channel?.id || session.channelId,
235
+ channelId: finalChannelId,
206
236
  messageId: `bot-msg-${timestamp}`,
207
237
  content: content,
208
238
  userId: session.selfId,
209
239
  username: session.bot.user?.name || `Bot-${session.selfId}`,
210
240
  avatar: session.bot.user?.avatar,
211
241
  timestamp: timestamp,
212
- guildId: session.event?.guild?.id || session.guildId,
213
242
  guildName: guildName,
214
243
  channelType: session.event?.channel?.type || session.type || 0,
215
244
  elements: this.utils.cleanBase64Content(session.event?.message?.elements),
245
+ isDirect: session.isDirect,
216
246
  bot: {
217
247
  avatar: session.bot.user?.avatar,
218
248
  name: session.bot.user?.name,
@@ -227,12 +257,17 @@ export class MessageHandler {
227
257
  this.logInfo('机器人发送消息 (before-send):', {
228
258
  selfId: session.selfId,
229
259
  channelId: messageEvent.channelId,
260
+ originalChannelId: session.channelId,
261
+ correctedChannelId: finalChannelId,
230
262
  content: content,
231
263
  platform: session.platform,
232
264
  imageCount: imageElements.length,
233
265
  imageUrls: imageElements.map((el: any) => el.attrs?.src || el.attrs?.url || el.attrs?.file)
234
266
  })
235
267
 
268
+ // 清理已使用的 channelId 映射
269
+ this.correctChannelIds.delete(session.selfId)
270
+
236
271
  this.ctx.console.broadcast('chat-bot-message-event', messageEvent)
237
272
  } catch (error) {
238
273
  this.logger.error('广播机器人消息事件失败:', error)
package/src/types.ts CHANGED
@@ -12,8 +12,9 @@ export interface ChannelInfo {
12
12
  id: string
13
13
  name: string
14
14
  type: number | string
15
- guildId?: string
15
+ channelId?: string
16
16
  guildName?: string
17
+ isDirect?: boolean
17
18
  }
18
19
 
19
20
  export interface QuoteInfo {
@@ -46,6 +47,7 @@ export interface MessageInfo {
46
47
  guildName?: string
47
48
  platform: string
48
49
  quote?: QuoteInfo
50
+ isDirect?: boolean
49
51
  }
50
52
 
51
53
  export interface ChatData {