UNPKG

@sodacore/discord

Version:

Sodacore Discord is a plugin that offers Discord SSO/OAuth2 support and the ability to create bots in a similar controller pattern.

96 lines (95 loc) 4.69 kB
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); }; import { Inject, Provide, Utils } from '@sodacore/di'; import { Registry } from '@sodacore/registry'; import { REST, Routes } from 'discord.js'; let SlashCommandsProvider = class SlashCommandsProvider { async register() { if (!this.config.token || !this.config.clientId) { throw new Error('You need a valid bot token and client ID to use slash commands.'); } const controllers = []; // Get all discord controllers. const modules = Registry.all(); for (const module of modules) { const type = Utils.getMeta('type', 'autowire')(module.constructor); const services = Utils.getMeta('services', 'controller')(module.constructor, undefined, []); if (!type || !services.includes('discord')) continue; controllers.push(module); } // Now let's get all the slash commands. const commands = controllers.map(controller => { const builder = Utils.getMeta('builder', 'discord')(controller.constructor); if (!builder) return null; return builder.toJSON(); }).filter(Boolean); // Now get the context menu commands. const contextMenuCommands = controllers.map(controller => { // Check if it's a context menu type. const type = Utils.getMeta('type', 'discord')(controller.constructor); if (type !== 'contextmenu') return []; // Now get the methods. const methods = Utils.getMeta('methods', 'discord')(controller, undefined, []); return methods.map(method => { const builder = Utils.getMeta('builder', 'discord')(controller, method.key); if (!builder) return null; return builder.toJSON(); }); }).filter(Boolean).flat(); // Create a rest instance. const rest = new REST().setToken(this.config.token); // Register the commands to the guild. if (this.config.commandRegisterLocation === 'global') { // Register the commands to the guild ID. const data = await rest.put(Routes.applicationCommands(this.config.clientId), { body: [...commands, ...contextMenuCommands] }); return { status: data.length === commands.length, message: `Registered ${data.length} application (/) commands.`, }; } else { // Check for guild ID. if (!this.config.guildId) { throw new Error('You need a valid guild ID to register commands to a guild.'); } // Register the commands to the guild ID. const data = await rest.put(Routes.applicationGuildCommands(this.config.clientId, this.config.guildId), { body: [...commands, ...contextMenuCommands] }); return { status: data.length === commands.length, message: `Registered ${data.length} application (/) guild commands.`, }; } } async unregister() { // Loop the guilds and unregister the commands. // await Promise.all(this.client.guilds.cache.map(async guild => { // // Fetch the guild commands. // const guildCommands = await guild.commands.fetch(); // // Loop the guild commands and delete. // await Promise.all(guildCommands.map(async command => { // await command.delete(); // })); // // Notify the console. // this.logger.verbose('PLUGIN:DISCORD', 'Commands have been unregistered.'); // })); } }; __decorate([ Inject('@discord:config'), __metadata("design:type", Object) ], SlashCommandsProvider.prototype, "config", void 0); SlashCommandsProvider = __decorate([ Provide() ], SlashCommandsProvider); export default SlashCommandsProvider;