grammy-class-composer
Version:
A simple composer to use Grammy js in a more Object oriented way
68 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClassComposer = void 0;
const grammy_1 = require("grammy");
const glob_1 = require("glob");
const decorators_1 = require("../decorators");
class ClassComposer extends grammy_1.Composer {
constructor(patterns, devMode = false) {
super();
this.botCommands = [];
this.registerClasses(patterns);
this.populateComposers();
}
populateComposers() {
decorators_1.classEntries.forEach(entry => {
entry.findEntryByType(decorators_1.MethodType.AFTER_CONSTRUCT).forEach(afterConstructEntry => entry.instance[afterConstructEntry.propertyKey](this));
entry.entries.forEach(methodEntry => {
const { query, propertyKey } = methodEntry;
switch (methodEntry.type) {
case decorators_1.MethodType.COMMAND:
this.handleCommandCase(entry, methodEntry, this);
break;
case decorators_1.MethodType.INLINE:
this.inlineQuery(query, (ctx, next) => entry.instance[propertyKey](ctx, next));
break;
case decorators_1.MethodType.HEARS:
this.hears(query, (ctx, next) => entry.instance[propertyKey](ctx, next));
break;
case decorators_1.MethodType.ON:
this.on(query, (ctx, next) => entry.instance[propertyKey](ctx, next));
break;
}
});
});
}
handleCommandCase(entry, methodEntry, composer) {
const { query, description } = methodEntry;
composer.command(query, (ctx, next) => entry.instance[methodEntry.propertyKey](ctx, next));
if (description.length == 0)
return;
if (!Array.isArray(query)) {
this.botCommands.push({
command: `${query}`,
description: description
});
return;
}
query.forEach(commandName => {
this.botCommands.push({
command: `${commandName}`,
description: description
});
});
}
registerClasses(patterns, options = {}) {
const files = [];
patterns.forEach(pattern => files.push(...glob_1.glob.sync(pattern, options)));
const workingDirectory = options.cwd ? options.cwd : process.cwd();
files.forEach(file => {
require(`${workingDirectory}/${file}`);
});
}
get BotCommands() {
return this.botCommands;
}
}
exports.ClassComposer = ClassComposer;
//# sourceMappingURL=ClassComposer.js.map