UNPKG

@globalart/nestcord

Version:

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

139 lines (138 loc) 6.17 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 __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 CommandsService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandsService = void 0; const common_1 = require("@nestjs/common"); const discord_js_1 = require("discord.js"); const context_menus_1 = require("./context-menus"); const slash_commands_1 = require("./slash-commands"); /** * Represents a service that manages commands. */ let CommandsService = CommandsService_1 = class CommandsService { constructor(client, contextMenusService, slashCommandsService) { this.client = client; this.contextMenusService = contextMenusService; this.slashCommandsService = slashCommandsService; this.logger = new common_1.Logger(CommandsService_1.name); } /** * Registers all commands. * */ registerAllCommands() { return __awaiter(this, void 0, void 0, function* () { const guilds = new Set(this.getCommandsByGuilds().keys()); this.logger.log(`Started refreshing application commands.`); for (const guild of guilds) { yield this.registerInGuild(guild); } this.logger.log(`Successfully reloaded application commands.`); }); } /** * Registers commands in a guild. * @param guildId */ registerInGuild(guildId) { return __awaiter(this, void 0, void 0, function* () { const commands = this.getGuildCommands(guildId); if (commands.length === 0) { this.logger.log(`Skipping ${guildId ? `guild ${guildId}` : 'global'} as it has no commands.`); return; } const rawCommands = commands.flatMap((command) => command.toJSON()); return this.client.application.commands.set(rawCommands, guildId).catch((error) => { this.logger.error(`Failed to register application commands (${guildId ? `in guild ${guildId}` : 'global'}): ${error}`, error.stack); }); }); } getCommands() { return [...this.contextMenusService.cache.values(), ...this.slashCommandsService.cache.values()].flat(); } getCommandsByGuilds() { const collection = new discord_js_1.Collection(); const commands = this.getCommands(); for (const command of commands) { for (const guildId of command.getGuilds()) { const visitedCommands = collection.get(guildId) || []; collection.set(guildId, visitedCommands.concat(command)); } } return collection; } getCommandsByCategoryMap() { return this.getCommands().reduce((map, command) => { const category = command.meta.category || 'no_group'; if (!map.has(category)) { map.set(category, []); } map.get(category).push(command); return map; }, new Map()); } getCommandsMap() { return this.getCommands().reduce((map, command) => { map.set(command.getName(), command); return map; }, new Map()); } getGuildCommandsMap(guildId) { return this.getGuildCommands(guildId).reduce((map, command) => { map.set(command.getName(), command); return map; }, new Map()); } getCommandByName(name) { return this.getCommands().find((command) => command.getName() === name); } getGlobalCommands() { return this.getCommandsByGuilds().get(undefined) || []; } getGlobalCommandByName(name) { return this.getCommandsMap().get(name); } getGuildCommands(guildId) { return this.getCommandsByGuilds().get(guildId) || []; } getGuildCommandByName(guildId, name) { return this.getGuildCommandsMap(guildId).get(name); } getAllCommandsAndSetDiscordResponseMeta() { const commands = this.getCommandsMap(); const commandsCache = this.client.application.commands.cache; const matchingCommands = Array.from(commandsCache.values()).filter((command) => commands.has(command.name)); for (const command of matchingCommands) { const commandByName = commands.get(command.name); if (commandByName.meta) { commandByName.meta.discordResponse = command; } this.slashCommandsService.update(commandByName); } } }; exports.CommandsService = CommandsService; exports.CommandsService = CommandsService = CommandsService_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [discord_js_1.Client, context_menus_1.ContextMenusService, slash_commands_1.SlashCommandsService]) ], CommandsService);