From eea615893a0ede3c9f097a60f9160fee5b3ae3de Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Fri, 29 Mar 2024 21:55:50 +0200 Subject: [PATCH] Save cache on an interval and formatting fixes --- src/client/components/MusicLibrary.ts | 12 ++++++++++++ src/client/components/commands/Queue.ts | 2 +- src/client/components/commands/Search.ts | 2 +- src/client/components/commands/Stats.ts | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/client/components/MusicLibrary.ts b/src/client/components/MusicLibrary.ts index f665e7d..2f1c29c 100644 --- a/src/client/components/MusicLibrary.ts +++ b/src/client/components/MusicLibrary.ts @@ -28,6 +28,7 @@ class MusicLibrary implements Initialisable #downloader: MusicDownloader; #index: Collection; #stats: Collection; + #interval!: NodeJS.Timeout; constructor (client: DiscordClient, libraryPath: string) { @@ -38,6 +39,7 @@ class MusicLibrary implements Initialisable this.#downloader = new MusicDownloader(client, path.join(libraryPath, '.downloads')); this.#logger = client.createLogger(this); this.#currentId = 0; + } get ready () @@ -66,12 +68,16 @@ class MusicLibrary implements Initialisable await this.scanLibrary(); this.#ready = true; this.#logger.info(`Music library initialised with ${this.#index.size} entries`); + + this.#interval = setInterval(this.#saveCache.bind(this), 15 * 60 * 1000); } stop (): void | Promise { + clearInterval(this.#interval); this.#saveIndex(); this.#saveStats(); + this.#ready = false; } async download (keyword: string) @@ -269,6 +275,12 @@ class MusicLibrary implements Initialisable return entry; } + #saveCache () + { + this.#saveIndex(); + this.#saveStats(); + } + #loadStats () { this.#logger.info('Loading stats cache'); diff --git a/src/client/components/commands/Queue.ts b/src/client/components/commands/Queue.ts index b1cc4c5..59bcd63 100644 --- a/src/client/components/commands/Queue.ts +++ b/src/client/components/commands/Queue.ts @@ -35,7 +35,7 @@ class QueueCommand extends Command if (!queue.length) return 'Queue empty'; return ` - **Music queue:**\n\`\`\`${queue.map(entry => `\t\\- **${entry.title}** by ${entry.artist}`).join('\n')}\`\`\` + **Music queue:**\n\`\`\`${queue.map(entry => `\t\\- ${entry.title} by ${entry.artist}`).join('\n')}\`\`\` `; } diff --git a/src/client/components/commands/Search.ts b/src/client/components/commands/Search.ts index 1d29a22..0c45fbb 100644 --- a/src/client/components/commands/Search.ts +++ b/src/client/components/commands/Search.ts @@ -36,7 +36,7 @@ class SearchCommand extends Command if (!results.length) return 'No results found'; return ` - **Search results:**\n\`\`\`${results.map(result => `\t\\- [id: ${result.id}] **${result.title}** - ${result.artist} (Album: ${result.album ?? 'Unknown'}) [${result.year ?? 'Unknown year'}]`).join('\n')}\`\`\` + **Search results:**\n\`\`\`${results.map(result => `\t- [id: ${result.id}] **${result.title}** - ${result.artist} (Album: ${result.album ?? 'Unknown'}) [${result.year ?? 'Unknown year'}]`).join('\n')}\`\`\` `; } } diff --git a/src/client/components/commands/Stats.ts b/src/client/components/commands/Stats.ts index 13c5f3a..4f033e6 100644 --- a/src/client/components/commands/Stats.ts +++ b/src/client/components/commands/Stats.ts @@ -39,7 +39,7 @@ class StatsCommand extends Command { pos++; const [ idx ] = this.client.musicPlayer.library.search({ title: entry.name }); - return `\t[${('00' + pos).slice(-2)}]\t**${idx.title}** by ${idx.artist} (${entry[stat]})`; + return `\t[${('00' + pos).slice(-2)}]\t${idx.title} by ${idx.artist} (${entry[stat]})`; }; const top10Plays = mostPlayed.map(mapper).join('\n'); stat = 'skips';