Save cache on an interval and formatting fixes

This commit is contained in:
Erik 2024-03-29 21:55:50 +02:00
parent 574a69bd97
commit eea615893a
4 changed files with 15 additions and 3 deletions

View File

@ -28,6 +28,7 @@ class MusicLibrary implements Initialisable
#downloader: MusicDownloader; #downloader: MusicDownloader;
#index: Collection<string, MusicIndexEntry>; #index: Collection<string, MusicIndexEntry>;
#stats: Collection<string, MusicStatsEntry>; #stats: Collection<string, MusicStatsEntry>;
#interval!: NodeJS.Timeout;
constructor (client: DiscordClient, libraryPath: string) constructor (client: DiscordClient, libraryPath: string)
{ {
@ -38,6 +39,7 @@ class MusicLibrary implements Initialisable
this.#downloader = new MusicDownloader(client, path.join(libraryPath, '.downloads')); this.#downloader = new MusicDownloader(client, path.join(libraryPath, '.downloads'));
this.#logger = client.createLogger(this); this.#logger = client.createLogger(this);
this.#currentId = 0; this.#currentId = 0;
} }
get ready () get ready ()
@ -66,12 +68,16 @@ class MusicLibrary implements Initialisable
await this.scanLibrary(); await this.scanLibrary();
this.#ready = true; this.#ready = true;
this.#logger.info(`Music library initialised with ${this.#index.size} entries`); this.#logger.info(`Music library initialised with ${this.#index.size} entries`);
this.#interval = setInterval(this.#saveCache.bind(this), 15 * 60 * 1000);
} }
stop (): void | Promise<void> stop (): void | Promise<void>
{ {
clearInterval(this.#interval);
this.#saveIndex(); this.#saveIndex();
this.#saveStats(); this.#saveStats();
this.#ready = false;
} }
async download (keyword: string) async download (keyword: string)
@ -269,6 +275,12 @@ class MusicLibrary implements Initialisable
return entry; return entry;
} }
#saveCache ()
{
this.#saveIndex();
this.#saveStats();
}
#loadStats () #loadStats ()
{ {
this.#logger.info('Loading stats cache'); this.#logger.info('Loading stats cache');

View File

@ -35,7 +35,7 @@ class QueueCommand extends Command
if (!queue.length) if (!queue.length)
return 'Queue empty'; return 'Queue empty';
return ` 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')}\`\`\`
`; `;
} }

View File

@ -36,7 +36,7 @@ class SearchCommand extends Command
if (!results.length) if (!results.length)
return 'No results found'; return 'No results found';
return ` 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')}\`\`\`
`; `;
} }
} }

View File

@ -39,7 +39,7 @@ class StatsCommand extends Command
{ {
pos++; pos++;
const [ idx ] = this.client.musicPlayer.library.search({ title: entry.name }); 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'); const top10Plays = mostPlayed.map(mapper).join('\n');
stat = 'skips'; stat = 'skips';