UNPKG

meocord

Version:

MeoCord is a lightweight and modular framework for building scalable Discord bots using TypeScript and Discord.js. It simplifies bot development with an extensible architecture, TypeScript-first approach, and powerful CLI tools.

61 lines 5.77 kB
function _construct(a,b,c){if(_isNativeReflectConstruct())return Reflect.construct.apply(null,arguments);var d=[null];d.push.apply(d,b);var e=new(a.bind.apply(a,d));return c&&_setPrototypeOf(e,c.prototype),e}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}function _isNativeReflectConstruct(){try{var a=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){}return(_isNativeReflectConstruct=function _isNativeReflectConstruct(){return!!a})()}function _toConsumableArray(a){return _arrayWithoutHoles(a)||_iterableToArray(a)||_unsupportedIterableToArray(a)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(b,c){if(b){if("string"==typeof b)return _arrayLikeToArray(b,c);var a={}.toString.call(b).slice(8,-1);return"Object"===a&&b.constructor&&(a=b.constructor.name),"Map"===a||"Set"===a?Array.from(b):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?_arrayLikeToArray(b,c):void 0}}function _iterableToArray(a){if("undefined"!=typeof Symbol&&null!=a[Symbol.iterator]||null!=a["@@iterator"])return Array.from(a)}function _arrayWithoutHoles(a){if(Array.isArray(a))return _arrayLikeToArray(a)}function _arrayLikeToArray(b,c){(null==c||c>b.length)&&(c=b.length);for(var d=0,f=Array(c);d<c;d++)f[d]=b[d];return f}/** * MeoCord Framework * Copyright (C) 2025 Ukasyah Rahmatullah Zada * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */import"reflect-metadata";import{Container,injectable}from"inversify";import{Client}from"discord.js";import{MeoCordApp}from"../core/meocord.app.js";import{loadMeoCordConfig}from"../util/meocord-config-loader.util.js";/** * Binds a class and its dependencies to the Inversify container in singleton scope. * * @param {Container} container - The Inversify container instance. * @param {any} cls - The class to be bound to the container. */function bindDependencies(a,b){if(!a.isBound(b)){a.bind(b).toSelf().inSingletonScope();var c=Reflect.getMetadata("design:paramtypes",b)||[];c.forEach(function(b){return bindDependencies(a,b)})}}/** * Resolves dependencies for a given class by binding them to the container and returning the resolved instances. * * @param {Container} container - The Inversify container instance. * @param {any} target - The target class whose dependencies are to be resolved. * @returns {any[]} - An array of resolved instances of the target's dependencies. */function resolveDependencies(a,b){var c=Reflect.getMetadata("design:paramtypes",b)||[];return c.map(function(b){return bindDependencies(a,b),a.get(b)})}/** The main Inversify container for managing dependencies. */export var mainContainer=new Container;/** * `@MeoCord()` decorator for initializing and setting up the MeoCord application. * * @param {Object} options - The decorator options. * @param {ServiceIdentifier[]} options.controllers - The list of controllers to be registered. * @param {ClientOptions} options.clientOptions - The Discord client options for initializing the bot. * @param {AppActivity[]} [options.activities] - Optional activities for the bot. * @param {ServiceIdentifier[]} [options.services] - Optional services to be registered. * * @example * ```typescript * @MeoCord({ * controllers: [PingSlashController], * clientOptions: { * intents: [ * GatewayIntentBits.Guilds, * GatewayIntentBits.GuildMembers, * GatewayIntentBits.GuildMessages, * GatewayIntentBits.GuildMessageReactions, * GatewayIntentBits.MessageContent, * ], * partials: [Partials.Message, Partials.Channel, Partials.Reaction], * }, * activities: [{ * name: `${sample(['Genshin', 'ZZZ'])} with Romeo`, * type: ActivityType.Playing, * url: 'https://enka.network/u/824957678/', * }], * services: [MyStandaloneService], * }) * class MyApp {} * ``` **/export function MeoCord(a){return function(b){Reflect.hasMetadata("inversify:injectable",b)||injectable()(b);var c=loadMeoCordConfig();if(c){var d=new Client(a.clientOptions);mainContainer.bind(Client).toConstantValue(d)// Bind controllers and services to the container ,[].concat(_toConsumableArray(a.controllers),_toConsumableArray(a.services||[])).forEach(function(a){bindDependencies(mainContainer,a)}),mainContainer.bind(b).toConstantValue(a.clientOptions),a.activities&&mainContainer.bind(b).toConstantValue(a.activities),a.services&&mainContainer.bind(b).toConstantValue(a.services.map(function(a){return mainContainer.get(a)}));var e=new MeoCordApp(a.controllers.map(function(a){return mainContainer.get(a)}),d,c.discordToken,null===a||void 0===a?void 0:a.activities);mainContainer.bind(MeoCordApp).toConstantValue(e),mainContainer.bind(b).toDynamicValue(function(){var a=resolveDependencies(mainContainer,b);return _construct(b,_toConsumableArray(a))}).inSingletonScope(),Reflect.defineMetadata("inversify:container",mainContainer,b)}}}