MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / formatEntity

Function formatEntity

shift/shift.ts:23–72  ·  view source on GitHub ↗
(
  target: any,
  mention?: boolean,
  throwErrorIfFailed?: boolean
)

Source from the content-addressed store, hash-verified

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

Callers 1

ShiftPluginClass · 0.70

Calls 2

errorMethod · 0.80
htmlEscapeFunction · 0.70

Tested by

no test coverage detected