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

Function formatEntity

bs/bs.ts:107–158  ·  view source on GitHub ↗
(
  target: any,
  mention?: boolean,
  throwErrorIfFailed?: boolean
)

Source from the content-addressed store, hash-verified

105}
106
107async function formatEntity(
108 target: any,
109 mention?: boolean,
110 throwErrorIfFailed?: boolean
111) {
112 const client = await getGlobalClient();
113 if (!client) throw new Error("Telegram 客户端未初始化");
114 if (!target) throw new Error("无效的目标");
115 let id: any;
116 let entity: any;
117 try {
118 entity = target?.className
119 ? target
120 : ((await client?.getEntity(target)) as any);
121 if (!entity) throw new Error("无法获取 entity");
122 id = entity.id;
123 if (!id) throw new Error("无法获取 entity id");
124 } catch (e: any) {
125 console.error(e);
126 if (throwErrorIfFailed)
127 throw new Error(
128 `无法获取 ${target} 的 entity: ${e?.message || "未知错误"}`
129 );
130 }
131 const displayParts: string[] = [];
132
133 if (entity?.title) displayParts.push(escapeHtml(entity.title));
134 if (entity?.firstName) displayParts.push(escapeHtml(entity.firstName));
135 if (entity?.lastName) displayParts.push(escapeHtml(entity.lastName));
136 if (entity?.username)
137 displayParts.push(
138 mention
139 ? escapeHtml(`@${entity.username}`)
140 : `<code>@${escapeHtml(entity.username)}</code>`
141 );
142
143 if (id) {
144 displayParts.push(
145 entity instanceof Api.User
146 ? `<a href="tg://user?id=${id}">${id}</a>`
147 : `<a href="https://t.me/c/${id}">${id}</a>`
148 );
149 } else if (!target?.className) {
150 displayParts.push(`<code>${escapeHtml(String(target))}</code>`);
151 }
152
153 return {
154 id,
155 entity,
156 display: displayParts.join(" ").trim(),
157 };
158}
159
160function escapeHtml(text: string): string {
161 const escaped = (text || "").replace(/[&<>"']/g, (ch) => {

Callers 2

forwardToTargetFunction · 0.70
BsPluginClass · 0.70

Calls 2

errorMethod · 0.80
escapeHtmlFunction · 0.70

Tested by

no test coverage detected