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

Function formatEntity

acron/acron.ts:143–192  ·  view source on GitHub ↗
(
  target: any,
  mention?: boolean,
  throwErrorIfFailed?: boolean,
)

Source from the content-addressed store, hash-verified

141}
142
143async function formatEntity(
144 target: any,
145 mention?: boolean,
146 throwErrorIfFailed?: boolean,
147) {
148 const client = await getGlobalClient();
149 if (!client) throw new Error("Telegram 客户端未初始化");
150 if (!target) throw new Error("无效的目标");
151 let id: any;
152 let entity: any;
153 try {
154 entity = target?.className
155 ? target
156 : ((await client?.getEntity(target)) as any);
157 if (!entity) throw new Error("无法获取 entity");
158 id = entity.id;
159 if (!id) throw new Error("无法获取 entity id");
160 } catch (e: any) {
161 console.error(e);
162 if (throwErrorIfFailed)
163 throw new Error(
164 `无法获取 ${target} 的 entity: ${e?.message || "未知错误"}`,
165 );
166 }
167 const displayParts: string[] = [];
168
169 if (entity?.title) displayParts.push(entity.title);
170 if (entity?.firstName) displayParts.push(entity.firstName);
171 if (entity?.lastName) displayParts.push(entity.lastName);
172 if (entity?.username)
173 displayParts.push(
174 mention ? `@${entity.username}` : `<code>@${entity.username}</code>`,
175 );
176
177 if (id) {
178 displayParts.push(
179 entity instanceof Api.User
180 ? `<a href="tg://user?id=${id}">${id}</a>`
181 : `<a href="https://t.me/c/${id}">${id}</a>`,
182 );
183 } else if (!target?.className) {
184 displayParts.push(`<code>${target}</code>`);
185 }
186
187 return {
188 id,
189 entity,
190 display: displayParts.join(" ").trim(),
191 };
192}
193
194function makeCronKey(id: string) {
195 return `acron:${id}`;

Callers 1

AcronPluginClass · 0.70

Calls 1

errorMethod · 0.80

Tested by

no test coverage detected