UNPKG

@globalart/nestcord

Version:

A module for creating Discord bots using NestJS, based on Discord.js

119 lines (118 loc) 6.06 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var NestCordStatReporterService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestCordStatReporterService = void 0; const common_1 = require("@nestjs/common"); const discord_js_1 = require("discord.js"); const nestcord_stat_reporter_module_definition_1 = require("./nestcord-stat-reporter.module-definition"); const schedule_1 = require("@nestjs/schedule"); const cron_1 = require("cron"); const axios_1 = require("@nestjs/axios"); const text_utils_1 = require("@globalart/text-utils"); const rxjs_1 = require("rxjs"); let NestCordStatReporterService = NestCordStatReporterService_1 = class NestCordStatReporterService { constructor(client, shard, options, schedulerRegistry, httpService) { this.client = client; this.shard = shard; this.options = options; this.schedulerRegistry = schedulerRegistry; this.httpService = httpService; this.logger = new common_1.Logger(NestCordStatReporterService_1.name); } onModuleInit() { this.client.once('ready', () => { if (this.isFirstShard() && this.isProduction()) { this.setupCronJobs(); } }); } isFirstShard() { return !this.shard || (Array.isArray(this.shard.ids) && this.shard.ids[0] === 0); } isProduction() { return !this.options.development; } setupCronJobs() { this.options.services.forEach((service) => { const job = new cron_1.CronJob(service.schedule, () => this.reportStats(service)); this.schedulerRegistry.addCronJob(service.name, job); job.start(); this.logger.log(`Job for ${service.name} scheduled with cron pattern: ${service.schedule}`); }); } reportStats(service) { return __awaiter(this, void 0, void 0, function* () { var _a; try { yield this.client.application.fetch(); const [serverCount, shardCount] = yield Promise.all([ this.calculateServerCount(), Promise.resolve(((_a = this.shard) === null || _a === void 0 ? void 0 : _a.count) || 1), ]); const headerData = service.headerData || {}; yield (0, rxjs_1.lastValueFrom)((0, rxjs_1.from)(service.actions).pipe((0, rxjs_1.mergeMap)((action) => this.httpService.request({ method: action.method || 'POST', url: action.url, data: (0, text_utils_1.replacePlaceholdersInObject)(action.bodyData, { serverCount, shardCount }), headers: headerData, proxy: service.proxy || this.options.proxy, })))); this.logStats(service.name, serverCount, shardCount); } catch (err) { this.logErrors(service.name, err); } }); } calculateServerCount() { return __awaiter(this, void 0, void 0, function* () { var _a; if (this.shard) { const shardGuildSizes = (yield this.shard.fetchClientValues('guilds.cache.size')); return (shardGuildSizes.reduce((acc, size) => acc + size, 0) || ((_a = this.client.application) === null || _a === void 0 ? void 0 : _a.approximateGuildCount) || 0); } return this.client.guilds.cache.size; }); } logStats(serviceName, serverCount, shardCount) { var _a; if ((_a = this.options.log) !== null && _a !== void 0 ? _a : true) { this.logger.log(`Reporting stats for ${serviceName}, servers: ${serverCount}, shards: ${shardCount}`); } } logErrors(serviceName, error) { var _a; if ((_a = this.options.log) !== null && _a !== void 0 ? _a : true) { this.logger.error(`Error reporting stats for ${serviceName}`, error); } } }; exports.NestCordStatReporterService = NestCordStatReporterService; exports.NestCordStatReporterService = NestCordStatReporterService = NestCordStatReporterService_1 = __decorate([ (0, common_1.Injectable)(), __param(2, (0, common_1.Inject)(nestcord_stat_reporter_module_definition_1.NESTCORD_STAT_REPORTER_OPTIONS_TOKEN)), __metadata("design:paramtypes", [discord_js_1.Client, discord_js_1.ShardClientUtil, Object, schedule_1.SchedulerRegistry, axios_1.HttpService]) ], NestCordStatReporterService);