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;
#index: Collection<string, MusicIndexEntry>;
#stats: Collection<string, MusicStatsEntry>;
#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<void>
{
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');

View File

@ -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')}\`\`\`
`;
}

View File

@ -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')}\`\`\`
`;
}
}

View File

@ -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';