| 36 | const client = await getGlobalClient(); |
| 37 | if (!client) throw new Error("Telegram 客户端未初始化"); |
| 38 | if (!target) throw new Error("无效的目标"); |
| 39 | let id: any; |
| 40 | let entity: any; |
| 41 | try { |
| 42 | entity = target?.className |
| 43 | ? target |
| 44 | : ((await client?.getEntity(target)) as any); |
| 45 | if (!entity) throw new Error("无法获取 entity"); |
| 46 | id = entity.id; |
| 47 | if (!id) throw new Error("无法获取 entity id"); |
| 48 | } catch (e: any) { |
| 49 | console.error(e); |
| 50 | if (throwErrorIfFailed) |
| 51 | throw new Error( |
| 52 | `无法获取 ${target} 的 entity: ${e?.message || "未知错误"}` |
| 53 | ); |
| 54 | } |
| 55 | const displayParts: string[] = []; |
| 56 | |
| 57 | if (entity?.title) displayParts.push(entity.title); |
| 58 | if (entity?.firstName) displayParts.push(entity.firstName); |
| 59 | if (entity?.lastName) displayParts.push(entity.lastName); |
| 60 | |
| 61 | return { |
| 62 | id, |
| 63 | entity, |
| 64 | username: entity?.username || null, |
| 65 | display: displayParts.join(" ").trim(), |
| 66 | }; |
| 67 | } |
| 68 | function getLastOnlineDays(user: Api.User): number | null { |
| 69 | if (!user.status) return null; |
| 70 | if ( |
| 71 | user.status instanceof Api.UserStatusOnline || |
| 72 | user.status instanceof Api.UserStatusRecently |
| 73 | ) { |
| 74 | return 0; |
| 75 | } |
| 76 | if (user.status instanceof Api.UserStatusOffline) { |
| 77 | if (user.status.wasOnline) { |