( target: any, mention?: boolean, throwErrorIfFailed?: boolean, )
| 25 | function formatDate(date: Date): string { |
| 26 | return date.toLocaleString("zh-CN", { timeZone: CN_TIME_ZONE }); |
| 27 | } |
| 28 | |
| 29 | async function formatEntity( |
| 30 | target: any, |
| 31 | mention?: boolean, |
| 32 | throwErrorIfFailed?: boolean, |
| 33 | ) { |
| 34 | const client = await getGlobalClient(); |
| 35 | if (!client) throw new Error("Telegram 客户端未初始化"); |
| 36 | if (!target) throw new Error("无效的目标"); |
| 37 | let id: any; |
| 38 | let entity: any; |
| 39 | try { |
| 40 | entity = target?.className |
| 41 | ? target |
| 42 | : ((await client?.getEntity(target)) as any); |
| 43 | if (!entity) throw new Error("无法获取 entity"); |
| 44 | id = entity.id; |
| 45 | if (!id) throw new Error("无法获取 entity id"); |
| 46 | } catch (e: any) { |
| 47 | console.error(e); |
| 48 | if (throwErrorIfFailed) |
| 49 | throw new Error( |
| 50 | `无法获取 ${target} 的 entity: ${e?.message || "未知错误"}`, |
| 51 | ); |
| 52 | } |
| 53 | const displayParts: string[] = []; |
| 54 | |
| 55 | if (entity?.title) displayParts.push(htmlEscape(entity.title)); |
| 56 | if (entity?.firstName) displayParts.push(htmlEscape(entity.firstName)); |
| 57 | if (entity?.lastName) displayParts.push(htmlEscape(entity.lastName)); |
| 58 | if (entity?.username) |
| 59 | displayParts.push( |
| 60 | mention |
| 61 | ? htmlEscape(`@${entity.username}`) |
| 62 | : `<code>@${htmlEscape(entity.username)}</code>`, |
| 63 | ); |
| 64 | |
| 65 | if (id) { |
| 66 | displayParts.push( |
| 67 | entity instanceof Api.User |
| 68 | ? `<a href="tg://user?id=${id}">${id}</a>` |
| 69 | : `<a href="https://t.me/c/${id}">${id}</a>`, |
| 70 | ); |
| 71 | } else if (!target?.className) { |
| 72 | displayParts.push(`<code>${htmlEscape(String(target))}</code>`); |
| 73 | } |
| 74 | |
| 75 | return { |
| 76 | id, |
| 77 | entity, |
| 78 | display: displayParts.join(" ").trim(), |
| 79 | }; |
| 80 | } |
| 81 | class DbdjPlugin extends Plugin { |
no test coverage detected