UNPKG

@globalart/nestcord

Version:

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

60 lines (59 loc) 2.75 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 SlashCommandsService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.SlashCommandsService = void 0; const common_1 = require("@nestjs/common"); const discord_js_1 = require("discord.js"); const core_1 = require("@nestjs/core"); const decorators_1 = require("./decorators"); /** * Represents a service that manages slash commands. */ let SlashCommandsService = SlashCommandsService_1 = class SlashCommandsService { constructor(reflector) { this.reflector = reflector; this.logger = new common_1.Logger(SlashCommandsService_1.name); this.cache = new discord_js_1.Collection(); } add(command) { if (this.cache.has(command.getName())) { this.logger.warn(`Slash Command : ${command.getName()} already exists`); } this.cache.set(command.getName(), command); } update(command) { this.cache.set(command.getName(), command); } get(commandName) { return this.cache.get(commandName); } remove(commandName) { return this.cache.delete(commandName); } addSubCommand(subCommand) { let rootCommand = this.reflector.get(decorators_1.SlashCommand.KEY, subCommand.getClass()); let subCommandGroup = this.reflector.get(decorators_1.SubcommandGroup.KEY, subCommand.getClass()); rootCommand = this.cache.ensure(rootCommand.getName(), () => rootCommand); if (subCommandGroup) { subCommandGroup = rootCommand.ensureSubcommand(subCommandGroup); subCommandGroup.ensureSubcommand(subCommand); } else { rootCommand.ensureSubcommand(subCommand); } } }; exports.SlashCommandsService = SlashCommandsService; exports.SlashCommandsService = SlashCommandsService = SlashCommandsService_1 = __decorate([ (0, common_1.Injectable)({ scope: common_1.Scope.DEFAULT, durable: true }), __metadata("design:paramtypes", [core_1.Reflector]) ], SlashCommandsService);