feat: Add hourly server time synchronization

This commit is contained in:
时瑾 committed 2026-04-13 19:49:06 +08:00
1 parent 45966f072e
commit d849188ef2
1 file changed
+6
+6
View File
@@ -180,6 +180,7 @@ export class NapCatCore {
if (process.env['NAPCAT_DISABLE_TIME_SYNC'] === '1' || !this.configLoader.configData.autoTimeSync) { if (process.env['NAPCAT_DISABLE_TIME_SYNC'] === '1' || !this.configLoader.configData.autoTimeSync) {
this.context.logger.log('[ServerTime] 自动对时已禁用'); this.context.logger.log('[ServerTime] 自动对时已禁用');
} else { } else {
const doTimeSync = () => {
try { try {
const offsetMs = hookGlobalDateNow(() => this.context.session.getMSFService().getServerTime()); const offsetMs = hookGlobalDateNow(() => this.context.session.getMSFService().getServerTime());
if (Math.abs(offsetMs) > 5000) { if (Math.abs(offsetMs) > 5000) {
@@ -190,6 +191,11 @@ export class NapCatCore {
} catch (e) { } catch (e) {
this.context.logger.logError('[ServerTime] 时间矫正失败', e); this.context.logger.logError('[ServerTime] 时间矫正失败', e);
} }
};
doTimeSync();
// 每小时定期重同步,防止长时间运行后本地时钟漂移触发风控
const syncTimer = setInterval(doTimeSync, 3600000);
syncTimer.unref();
} }
// 遍历this.apis[i].initApi 如果存在该函数进行async 调用 // 遍历this.apis[i].initApi 如果存在该函数进行async 调用
for (const apiKey in this.apis) { for (const apiKey in this.apis) {