From 35d86f8ec39e8e5e075ebc5a282c3cf412f0ca5f Mon Sep 17 00:00:00 2001 From: Shrev Dev Date: Tue, 21 Oct 2025 11:30:24 -0500 Subject: [PATCH] fix: implement debounce for config saves + fix duplicate embed issues --- src/config/storage.ts | 21 +++++++++++++++++++++ src/index.ts | 3 +++ src/services/discord.service.ts | 3 ++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/config/storage.ts b/src/config/storage.ts index cc08512..778dc8f 100644 --- a/src/config/storage.ts +++ b/src/config/storage.ts @@ -25,6 +25,7 @@ export class ConfigStorage { private configPath: string; private config: MultiGuildConfig; private logger: Logger; + private saveTimeout: NodeJS.Timeout | null = null; constructor() { this.logger = new Logger('ConfigStorage'); @@ -73,12 +74,32 @@ export class ConfigStorage { } private save(): void { + // Debounce saves to prevent excessive writes + if (this.saveTimeout) { + clearTimeout(this.saveTimeout); + } + + this.saveTimeout = setTimeout(() => { + this.forceSave(); + }, 100); // 100ms debounce + } + + private forceSave(): void { try { writeFileSync(this.configPath, JSON.stringify(this.config, null, 2), 'utf-8'); this.logger.info('Saved configuration to storage'); } catch (error: any) { this.logger.error(`Failed to save config: ${error.message}`); } + this.saveTimeout = null; + } + + public flush(): void { + // Force immediate save (useful for shutdown) + if (this.saveTimeout) { + clearTimeout(this.saveTimeout); + this.forceSave(); + } } private getDefaultConfig(): GuildConfig { diff --git a/src/index.ts b/src/index.ts index f8b2db6..a01ae70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -188,6 +188,9 @@ class UptimeKumaDiscordBot { this.uptimeKuma.disconnect(); this.discord.disconnect(); + // Flush any pending config saves + configStorage.flush(); + this.logger.info('Shutdown complete'); process.exit(0); } catch (error: any) { diff --git a/src/services/discord.service.ts b/src/services/discord.service.ts index aec172d..0d482f0 100644 --- a/src/services/discord.service.ts +++ b/src/services/discord.service.ts @@ -362,8 +362,9 @@ export class DiscordService { } } catch (error: any) { this.logger.error(`Failed to update message for guild ${guildId}: ${error.message}`); + // Don't create new messages here - let the main flow handle it + // Just clear the message IDs so next update will create new ones configStorage.setMessageIds(guildId, []); - await this.createNewMessages(guildId, channel, embeds); return; } }