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