UNPKG

@globalart/nestcord

Version:

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

137 lines (136 loc) 6.25 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 NestCordService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestCordService = void 0; const common_1 = require("@nestjs/common"); const discord_js_1 = require("discord.js"); const nestcord_consts_1 = require("./nestcord.consts"); const nestcord_module_definition_1 = require("./nestcord.module-definition"); let NestCordService = NestCordService_1 = class NestCordService { constructor(options, discordClient) { this.options = options; this.discordClient = discordClient; this.dataFetchInterval = 60000; this.logger = new common_1.Logger(NestCordService_1.name); this.assetsMap = new Map(); this.emojisMap = new Map(); this.setupProxy(); } onApplicationBootstrap() { this.discordClient.once('ready', () => { this.updateData(); setInterval(() => this.updateData(), this.dataFetchInterval); }); } setupProxy() { if (this.options.proxyPath) { this.proxyConfig = this.options.proxyPath; const maskedPath = this.maskProxyUrl(this.options.proxyPath); this.logger.log(`Proxy path configured: ${maskedPath}`); process.env.HTTPS_PROXY = this.options.proxyPath; process.env.HTTP_PROXY = this.options.proxyPath; } else if (this.options.proxy) { const { host, port, auth, protocol = 'http' } = this.options.proxy; const authString = auth ? `${auth.username}:${auth.password}@` : ''; const proxyUrl = `${protocol}://${authString}${host}:${port}`; this.proxyConfig = this.options.proxy; const maskedHost = this.maskString(host); this.logger.log(`Proxy configured: ${protocol}://${maskedHost}:${port}`); process.env.HTTPS_PROXY = proxyUrl; process.env.HTTP_PROXY = proxyUrl; } } maskProxyUrl(url) { try { const proxyUrl = new URL(url); const maskedHost = this.maskString(proxyUrl.hostname); return `${proxyUrl.protocol}//${proxyUrl.username ? '****:****@' : ''}${maskedHost}:${proxyUrl.port}`; } catch (_a) { return '****'; } } maskString(str) { if (!str) return ''; const visibleChars = 2; const start = str.slice(0, visibleChars); return start + '*'.repeat(Math.max(str.length - visibleChars, 3)); } getProxyConfig() { return this.proxyConfig; } getApplicationEmoji(name) { return this.emojisMap.get(name) || null; } getApplicationAsset(name) { return this.assetsMap.get(name) || null; } getApplicationEmojiPlain(name) { const emoji = this.getApplicationEmoji(name); return emoji ? emoji.toString() : ''; } updateData() { return __awaiter(this, void 0, void 0, function* () { yield Promise.allSettled([this.fetchEmojis(), this.fetchApplicationAssets()]); }); } fetchEmojis() { return __awaiter(this, void 0, void 0, function* () { try { yield this.discordClient.application.emojis.fetch(); this.emojisMap = new Map(this.discordClient.application.emojis.cache.map((emoji) => [emoji.name, emoji])); } catch (error) { this.logger.error('Error fetching emojis:', error); } }); } fetchApplicationAssets() { return __awaiter(this, void 0, void 0, function* () { const rest = new discord_js_1.REST({ version: '10' }).setToken(this.options.token); try { const assets = yield rest.get(`/oauth2/applications/${this.discordClient.user.id}/assets`); this.assetsMap = new Map(assets.map((asset) => [ asset.name, Object.assign(Object.assign({}, asset), { url: this.getAssetUrl(asset.id) }), ])); } catch (error) { this.logger.error('Error fetching assets:', error); throw error; } }); } getAssetUrl(assetId) { return `${nestcord_consts_1.DISCORD_CDN_URL}/app-assets/${this.discordClient.user.id}/${assetId}.png`; } }; exports.NestCordService = NestCordService; exports.NestCordService = NestCordService = NestCordService_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(nestcord_module_definition_1.NESTCORD_MODULE_OPTIONS)), __metadata("design:paramtypes", [Object, discord_js_1.Client]) ], NestCordService);