Skip to content

Commit 2dcba63

Browse files
committed
Add configurable automatic time sync
Add autoTimeSync (default: true) to the Napcat config and honor NAPCAT_DISABLE_TIME_SYNC=1 or autoTimeSync=false to disable automatic time synchronization. When enabled, time sync is attempted via getServerTime with errors logged; warning threshold for significant drift increased from 1s to 5s. Adds informational log when auto time sync is disabled.
1 parent 53ceb85 commit 2dcba63

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

packages/napcat-core/helper/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const NapcatConfigSchema = Type.Object({
2323
packetBackend: Type.String({ default: 'auto' }),
2424
packetServer: Type.String({ default: '' }),
2525
o3HookMode: Type.Number({ default: 0 }),
26+
autoTimeSync: Type.Boolean({ default: true }),
2627
bypass: Type.Optional(BypassOptionsSchema),
2728
});
2829

packages/napcat-core/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,20 @@ export class NapCatCore {
176176
fs.mkdirSync(this.NapCatTempPath, { recursive: true });
177177
}
178178
// 通过 getServerTime 全局 hook Date.now() 矫正本地时间偏差
179-
try {
180-
const offsetMs = hookGlobalDateNow(() => this.context.session.getMSFService().getServerTime());
181-
if (Math.abs(offsetMs) > 1000) {
182-
this.context.logger.logWarn(`[ServerTime] 本地时间与服务器时间偏差 ${(offsetMs / 1000).toFixed(1)}s,已自动矫正`);
183-
} else {
184-
this.context.logger.log(`[ServerTime] 时间同步完成,偏差 ${offsetMs}ms`);
179+
// 可通过环境变量 NAPCAT_DISABLE_TIME_SYNC=1 或配置 autoTimeSync: false 禁用
180+
if (process.env['NAPCAT_DISABLE_TIME_SYNC'] === '1' || !this.configLoader.configData.autoTimeSync) {
181+
this.context.logger.log('[ServerTime] 自动对时已禁用');
182+
} else {
183+
try {
184+
const offsetMs = hookGlobalDateNow(() => this.context.session.getMSFService().getServerTime());
185+
if (Math.abs(offsetMs) > 5000) {
186+
this.context.logger.logWarn(`[ServerTime] 本地时间与服务器时间偏差 ${(offsetMs / 1000).toFixed(1)}s,已自动矫正`);
187+
} else {
188+
this.context.logger.log(`[ServerTime] 时间同步完成,偏差 ${offsetMs}ms`);
189+
}
190+
} catch (e) {
191+
this.context.logger.logError('[ServerTime] 时间矫正失败', e);
185192
}
186-
} catch (e) {
187-
this.context.logger.logError('[ServerTime] 时间矫正失败', e);
188193
}
189194
// 遍历this.apis[i].initApi 如果存在该函数进行async 调用
190195
for (const apiKey in this.apis) {

0 commit comments

Comments
 (0)