mirror of
https://github.com/BrenBroZAYT/uptime-kuma-discord-bot.git
synced 2026-06-13 16:40:03 +00:00
fix: implement debounce for config saves + fix duplicate embed issues
This commit is contained in:
@@ -25,6 +25,7 @@ export class ConfigStorage {
|
|||||||
private configPath: string;
|
private configPath: string;
|
||||||
private config: MultiGuildConfig;
|
private config: MultiGuildConfig;
|
||||||
private logger: Logger;
|
private logger: Logger;
|
||||||
|
private saveTimeout: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.logger = new Logger('ConfigStorage');
|
this.logger = new Logger('ConfigStorage');
|
||||||
@@ -73,12 +74,32 @@ export class ConfigStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private save(): void {
|
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 {
|
try {
|
||||||
writeFileSync(this.configPath, JSON.stringify(this.config, null, 2), 'utf-8');
|
writeFileSync(this.configPath, JSON.stringify(this.config, null, 2), 'utf-8');
|
||||||
this.logger.info('Saved configuration to storage');
|
this.logger.info('Saved configuration to storage');
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
this.logger.error(`Failed to save config: ${error.message}`);
|
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 {
|
private getDefaultConfig(): GuildConfig {
|
||||||
|
|||||||
@@ -188,6 +188,9 @@ class UptimeKumaDiscordBot {
|
|||||||
this.uptimeKuma.disconnect();
|
this.uptimeKuma.disconnect();
|
||||||
this.discord.disconnect();
|
this.discord.disconnect();
|
||||||
|
|
||||||
|
// Flush any pending config saves
|
||||||
|
configStorage.flush();
|
||||||
|
|
||||||
this.logger.info('Shutdown complete');
|
this.logger.info('Shutdown complete');
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
@@ -362,8 +362,9 @@ export class DiscordService {
|
|||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
this.logger.error(`Failed to update message for guild ${guildId}: ${error.message}`);
|
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, []);
|
configStorage.setMessageIds(guildId, []);
|
||||||
await this.createNewMessages(guildId, channel, embeds);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user