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

Class NtpPlugin

ntp/ntp.ts:236–324  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

234 > = {
235 ntp: async (msg: Api.Message) => {
236 const text = msg.message || "";
237 const args = text.trim().split(/\s+/g).slice(1); // remove command
238
239 // .ntp -> show current offset
240 if (args.length === 0) {
241 try {
242 await msg.edit({ text: "⏳ 正在查询 NTP..." });
243 } catch {}
244 try {
245 const result = await queryBest();
246 const now = Date.now();
247 const lines = [
248 `🕒 NTP 查询完成`,
249 `• 服务器: <code>${htmlEscape(result.server)}</code>${result.ip ? ` (${htmlEscape(result.ip)})` : ""}`,
250 `• 分层: <code>${result.stratum ?? "-"}</code>`,
251 `• 往返延迟: <code>${fmtMs(result.delayMs)}</code>`,
252 `• 本地相对偏移: <code>${fmtMs(result.offsetMs)}</code>`,
253 `• 本地时间: <code>${formatDateCN(new Date(now))}</code>`,
254 result.serverTimeMs
255 ? `• 服务器时间(估算): <code>${formatDateCN(
256 new Date(result.serverTimeMs)
257 )}</code>`
258 : undefined,
259 ].filter(Boolean);
260 await msg.edit({ text: lines.join("\n"), parseMode: "html" });
261 } catch (e: any) {
262 await msg.edit({ text: `❌ 查询失败:${htmlEscape(String(e?.message || e))}`, parseMode: "html" });
263 }
264 return;
265 }
266
267 // .ntp s -> sync time (best effort, requires privileges)
268 if (args[0].toLowerCase() === "s") {
269 try {
270 await msg.edit({ text: "🔧 正在对时(NTP 查询)..." });
271 } catch {}
272 try {
273 const result = await queryBest();
274 if (!result.serverTimeMs) throw new Error("无法获取服务器时间");
275
276 const now = Date.now();
277 const beforeOffset = result.offsetMs ?? result.serverTimeMs - now;
278 const header = [
279 `🔧 NTP 对时`,
280 `• 服务器: <code>${htmlEscape(result.server)}</code>${result.ip ? ` (${htmlEscape(result.ip)})` : ""}`,
281 `• 估算偏移: <code>${fmtMs(beforeOffset)}</code>`,
282 `• 往返延迟: <code>${fmtMs(result.delayMs)}</code>`,
283 ].join("\n");
284
285 // Try set system time
286 const setRes = await setSystemTimeBestEffort(result.serverTimeMs);
287 if (setRes.ok) {
288 await msg.edit({
289 text:
290 header +
291 `\n• 已尝试设置系统时间:<code>${htmlEscape(setRes.command || "")}</code>` +
292 (setRes.stdout ? `\n<pre>${htmlEscape((setRes.stdout || "").trim())}</pre>` : ""),
293 parseMode: "html",

Callers

nothing calls this directly

Calls 6

queryBestFunction · 0.85
fmtMsFunction · 0.85
formatDateCNFunction · 0.85
setSystemTimeBestEffortFunction · 0.85
htmlEscapeFunction · 0.70
editMethod · 0.45

Tested by

no test coverage detected