(
msg: Api.Message,
query: string | null,
useSpoiler: boolean,
useRandom: boolean,
type: "kkp" | "search"
)
| 420 | } |
| 421 | |
| 422 | private async findAndSendVideo( |
| 423 | msg: Api.Message, |
| 424 | query: string | null, |
| 425 | useSpoiler: boolean, |
| 426 | useRandom: boolean, |
| 427 | type: "kkp" | "search" |
| 428 | ) { |
| 429 | if (this.config.channelList.length === 0) |
| 430 | throw new Error(`请至少使用 \`${mainPrefix}so add\` 添加一个搜索频道。`); |
| 431 | |
| 432 | const initialMessage = type === "kkp" ? "🎲 正在随机寻找视频..." : "🔍 正在搜索视频..."; |
| 433 | await msg.edit({ text: initialMessage }); |
| 434 | |
| 435 | const searchOrder = [...new Set([this.config.defaultChannel, ...this.config.channelList.map((c) => c.handle)].filter(Boolean) as string[])]; |
| 436 | |
| 437 | let validVideos: Api.Message[] = []; |
| 438 | const processedGroupIds = new Set<string>(); |
| 439 | |
| 440 | for (const [index, channelHandle] of searchOrder.entries()) { |
| 441 | if (index > 0) { |
| 442 | await new Promise(resolve => setTimeout(resolve, 750)); |
| 443 | } |
| 444 | |
| 445 | const channelInfo = this.config.channelList.find((c) => c.handle === channelHandle); |
| 446 | if (!channelInfo) continue; |
| 447 | |
| 448 | let videosInCurrentChannel: Api.Message[] = []; |
| 449 | |
| 450 | try { |
| 451 | await msg.edit({ text: `- 正在搜索... (源: ${index + 1}/${searchOrder.length})` }); |
| 452 | const entity = await this.client.getEntity(channelInfo.handle); |
| 453 | |
| 454 | if (type === "search" && query) { |
| 455 | if (channelInfo.linkedGroup) { |
| 456 | const linkedVideos = await this.searchInChannelWithLinkedGroup(channelInfo, query); |
| 457 | if (linkedVideos.length > 0) videosInCurrentChannel.push(...linkedVideos); |
| 458 | } |
| 459 | |
| 460 | const allQueryMessages = await safeGetMessages(this.client, entity, { limit: 200, search: query }); |
| 461 | |
| 462 | for (const foundMsg of allQueryMessages) { |
| 463 | if (this.isMessageMatching(foundMsg, query)) { |
| 464 | if (foundMsg.groupedId) { |
| 465 | const groupIdStr = foundMsg.groupedId.toString(); |
| 466 | if (processedGroupIds.has(groupIdStr)) continue; |
| 467 | |
| 468 | const surroundingMessages = await safeGetMessages(this.client, entity, { |
| 469 | limit: 20, |
| 470 | offsetId: foundMsg.id + 10, |
| 471 | }); |
| 472 | |
| 473 | const groupedId = foundMsg.groupedId; |
| 474 | if (!groupedId) continue; |
| 475 | const albumMessages = surroundingMessages.filter((m: Api.Message) => m.groupedId?.equals(groupedId)); |
| 476 | const videosInAlbum = albumMessages.filter((m: Api.Message) => m.video && !this.isAdContent(m)); |
| 477 | |
| 478 | if (videosInAlbum.length > 0) { |
| 479 | videosInCurrentChannel.push(...videosInAlbum); |
no test coverage detected