(
client: TelegramClient,
packName: string,
username: string,
stickerInfo: { isAnimated: boolean; isVideo: boolean; isStatic: boolean }
)
| 337 | throw new StickerError(`无法访问贴纸包 <code>${htmlEscape(packName)}</code>。请确保它存在且您有权访问。`); |
| 338 | } |
| 339 | } |
| 340 | } else { |
| 341 | throw new StickerError("参数错误。"); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private async findOrCreatePack( |
| 346 | client: TelegramClient, |
| 347 | packName: string, |
| 348 | username: string, |
| 349 | stickerInfo: { isAnimated: boolean; isVideo: boolean; isStatic: boolean } |
| 350 | ): Promise<{ packName: string; shouldCreate: boolean }> { |
| 351 | if (packName) { // User specified a pack (default or temporary) |
| 352 | try { |
| 353 | const result = await client.invoke(new Api.messages.GetStickerSet({ |
| 354 | stickerset: new Api.InputStickerSetShortName({ shortName: packName }), |
| 355 | hash: 0, |
| 356 | })); |
| 357 | // 修复: 使用类型守卫安全访问 .set 属性 |
| 358 | if (result instanceof Api.messages.StickerSet) { |
| 359 | if (result.set.count >= 120) { |
| 360 | throw new StickerError(`贴纸包 <code>${htmlEscape(packName)}</code> 已满 (120/120)。`); |
| 361 | } |
| 362 | return { packName, shouldCreate: false }; |
| 363 | } |
| 364 | // Handle StickerSetNotModified case if necessary, though unlikely with hash: 0 |
| 365 | return { packName, shouldCreate: false }; |
| 366 | } catch (error: any) { |
| 367 | if (error.errorMessage === 'STICKERSET_INVALID') { |
| 368 | return { packName, shouldCreate: true }; |
| 369 | } |
| 370 | throw new StickerError(`检查贴纸包 <code>${htmlEscape(packName)}</code> 时出错: ${htmlEscape(error.message)}`); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // Auto-generation logic - 为每种类型贴纸分配专用后缀 |
| 375 | let suffix = "_static"; // 默认静态贴纸 |
| 376 | if (stickerInfo.isAnimated) { |
| 377 | suffix = "_animated"; |
| 378 | } else if (stickerInfo.isVideo) { |
| 379 | suffix = "_video"; |
| 380 | } |
| 381 | |
| 382 | for (let i = 1; i <= 50; i++) { // Try up to 50 packs |
| 383 | const autoPackName = `${username}${suffix}_${i}`; |
| 384 | try { |
| 385 | const result = await client.invoke(new Api.messages.GetStickerSet({ |
| 386 | stickerset: new Api.InputStickerSetShortName({ shortName: autoPackName }), |
| 387 | hash: 0, |
| 388 | })); |
| 389 | // 修复: 使用类型守卫安全访问 .set 属性 |
| 390 | if (result instanceof Api.messages.StickerSet) { |
| 391 | if (result.set.count < 120) { |
| 392 | return { packName: autoPackName, shouldCreate: false }; |
| 393 | } |
| 394 | } |
| 395 | // If full or not modified, loop continues to the next index |
| 396 | } catch (error: any) { |
no test coverage detected