From 7e648d1c26d1dab6c8511b97c8bdc3f8aa7b4ad4 Mon Sep 17 00:00:00 2001 From: Roman Khabarov Date: Wed, 29 May 2024 00:33:32 +0200 Subject: [PATCH] refactor --- .../nestcord-stat-reporter.service.ts | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/packages/stat-reporter/nestcord-stat-reporter.service.ts b/packages/stat-reporter/nestcord-stat-reporter.service.ts index 14a7ceb..a0b0c23 100644 --- a/packages/stat-reporter/nestcord-stat-reporter.service.ts +++ b/packages/stat-reporter/nestcord-stat-reporter.service.ts @@ -18,16 +18,21 @@ export class NestCordStatReporterService implements OnModuleInit { ) {} onModuleInit() { - this.client.on('ready', () => { - const isFirstShard = (this.client.shard && this.client.shard.ids?.[0] === 0) || !this.client.shard; - const isProduction = !this.options.development; - - if (isFirstShard && isProduction) { + this.client.once('ready', () => { + if (this.isFirstShard() && this.isProduction()) { this.setupCronJobs(); } }); } + private isFirstShard(): boolean { + return (this.client.shard?.ids?.[0] === 0) || !this.client.shard; + } + + private isProduction(): boolean { + return !this.options.development; + } + private setupCronJobs() { this.options.services.forEach((service) => { const job = new CronJob(service.schedule, () => this.reportStats(service)); @@ -38,18 +43,9 @@ export class NestCordStatReporterService implements OnModuleInit { } private async reportStats(service: ServiceOption) { - await this.client.application.fetch(); - let serverCount: number; + await this.client.application?.fetch(); + const serverCount = await this.calculateServerCount(); const shardCount = this.client.shard?.count || 1; - if (this.client.shard) { - const totalServersOnAllShards = (await this.client.shard.fetchClientValues('guilds.cache.size')) as number[]; - serverCount = - totalServersOnAllShards.reduce((acc, guildCount) => acc + guildCount, 0) || - this.client.application.approximateGuildCount; - } else { - serverCount = this.client.guilds.cache.size; - } - const bodyData = this.replacePlaceholders(service.bodyData, { serverCount, shardCount }); const headerData = service.headerData || {}; @@ -61,26 +57,33 @@ export class NestCordStatReporterService implements OnModuleInit { headers: headerData, }) .subscribe({ - next: () => { - if (this.options.log || this.options.log === undefined) { - this.logger.log(`Reporting stats for ${service.name}, servers: ${serverCount}, shards: ${shardCount}`); - } - }, - error: (err) => { - this.logger.error(`Error reporting stats for ${service.name}`, err); - }, + next: () => this.logStats(service.name, serverCount, shardCount), + error: (err) => this.logger.error(`Error reporting stats for ${service.name}`, err), }); } - private replacePlaceholders(obj: unknown, replacements: unknown) { - if (typeof obj === 'string' && isNaN(Number(obj))) { - let replaced = obj.replace(/{{(.*?)}}/g, (match, key) => - replacements.hasOwnProperty(key) ? replacements[key] : match, - ); - return !isNaN(Number(replaced)) ? Number(replaced) : replaced; + private async calculateServerCount(): Promise { + if (this.client.shard) { + const shardGuildSizes = await this.client.shard.fetchClientValues('guilds.cache.size') as number[]; + return shardGuildSizes.reduce((acc, size) => acc + size, 0) || this.client.application?.approximateGuildCount || 0; + } + return this.client.guilds.cache.size; + } + + private logStats(serviceName: string, serverCount: number, shardCount: number) { + if (this.options.log ?? true) { + this.logger.log(`Reporting stats for ${serviceName}, servers: ${serverCount}, shards: ${shardCount}`); } - if (typeof obj === 'object' && obj !== null) { - for (const key in obj) obj[key] = this.replacePlaceholders(obj[key], replacements); + } + + private replacePlaceholders(obj: any, replacements: { [key: string]: any }): any { + if (typeof obj === 'string') { + return obj.replace(/{{(.*?)}}/g, (_, key) => replacements[key] ?? _); + } + if (obj && typeof obj === 'object') { + Object.entries(obj).forEach(([key, value]) => { + obj[key] = this.replacePlaceholders(value, replacements); + }); } return obj; }