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

Function formatEntity

manage_admin/manage_admin.ts:35–84  ·  view source on GitHub ↗
(
  target: any,
  mention?: boolean,
  throwErrorIfFailed?: boolean
)

Source from the content-addressed store, hash-verified

33 const client = await getGlobalClient();
34 if (!client) throw new Error("Telegram 客户端未初始化");
35 if (!target) throw new Error("无效的目标");
36 let id: any;
37 let entity: any;
38 try {
39 entity = target?.className
40 ? target
41 : ((await client?.getEntity(target)) as any);
42 if (!entity) throw new Error("无法获取 entity");
43 id = entity.id;
44 if (!id) throw new Error("无法获取 entity id");
45 } catch (e: any) {
46 console.error(e);
47 if (throwErrorIfFailed)
48 throw new Error(
49 `无法获取 ${target} 的 entity: ${e?.message || "未知错误"}`
50 );
51 }
52 const displayParts: string[] = [];
53
54 if (entity?.title) displayParts.push(entity.title);
55 if (entity?.firstName) displayParts.push(entity.firstName);
56 if (entity?.lastName) displayParts.push(entity.lastName);
57 if (entity?.username)
58 displayParts.push(
59 mention ? `@${htmlEscape(entity.username)}` : codeTag(`@${entity.username}`)
60 );
61
62 if (id) {
63 displayParts.push(
64 entity instanceof Api.User
65 ? `<a href="tg://user?id=${id}">${id}</a>`
66 : `<a href="https://t.me/c/${id}">${id}</a>`
67 );
68 } else if (!target?.className) {
69 displayParts.push(codeTag(target));
70 }
71
72 return {
73 id,
74 entity,
75 display: displayParts.join(" ").trim(),
76 };
77}
78function getTxtFromMsg(msg: Api.Message | string, n: number): string {
79 return (typeof msg === "string" ? msg : msg?.message || "")
80 .replace(new RegExp(`^\\S+${Array(n).fill("\\s+\\S+").join("")}`), "")
81 .trim();
82}
83class ManageAdminPlugin extends Plugin {
84
85 description: string = `\n管理管理员\n\n${help_text}`;
86 cmdHandlers: Record<
87 string,

Callers 2

addOrUpdateAdminMethod · 0.70
removeAdminMethod · 0.70

Calls 3

errorMethod · 0.80
htmlEscapeFunction · 0.70
codeTagFunction · 0.70

Tested by

no test coverage detected