@pubby/sdk
Version:
Pubby Development Kit
176 lines (171 loc) • 7.62 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib = require('tslib');
var module$1 = require('../../module.js');
var index = require('../chat/index.js');
var command = require('./command.js');
var paramsString = require('./params-string.js');
var index$1 = require('./types/index.js');
var _a;
var $commands = Symbol("commands");
var CommandsModule = /** @class */ (function (_super) {
tslib.__extends(CommandsModule, _super);
function CommandsModule(pubby, options) {
var e_1, _b;
var _this = _super.call(this, pubby, options) || this;
_this[_a] = new Map();
_this.params = new paramsString.ParamsString({});
_this.messageListener = function (message) { return tslib.__awaiter(_this, void 0, void 0, function () {
var name, cmd, ctx, _b, _c, handler, err_1, e_2_1;
var e_2, _d;
var _e;
return tslib.__generator(this, function (_f) {
switch (_f.label) {
case 0:
if (!message.text.startsWith(this.options.prefix)) {
return [2 /*return*/];
}
name = message.text
.substring(this.options.prefix.length)
.match(/^\S+/);
if (!name) return [3 /*break*/, 10];
cmd = (_e = this[$commands].get(name[0])) === null || _e === void 0 ? void 0 : _e.options;
if (!cmd || !cmd.enabled)
return [2 /*return*/];
ctx = cmd.context ? cmd.context(message) : {};
_f.label = 1;
case 1:
_f.trys.push([1, 8, 9, 10]);
_b = tslib.__values(cmd.pipeline), _c = _b.next();
_f.label = 2;
case 2:
if (!!_c.done) return [3 /*break*/, 7];
handler = _c.value;
_f.label = 3;
case 3:
_f.trys.push([3, 5, , 6]);
return [4 /*yield*/, Promise.resolve(handler(message, ctx))];
case 4:
if ((_f.sent()) === false) {
return [3 /*break*/, 7];
}
return [3 /*break*/, 6];
case 5:
err_1 = _f.sent();
console.log(err_1);
return [3 /*break*/, 7];
case 6:
_c = _b.next();
return [3 /*break*/, 2];
case 7: return [3 /*break*/, 10];
case 8:
e_2_1 = _f.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 10];
case 9:
try {
if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 10: return [2 /*return*/];
}
});
}); };
pubby.commands = _this;
// Import chat
pubby.use(index.ChatModule);
try {
// Param Types
for (var paramTypes_1 = tslib.__values(index$1), paramTypes_1_1 = paramTypes_1.next(); !paramTypes_1_1.done; paramTypes_1_1 = paramTypes_1.next()) {
var type = paramTypes_1_1.value;
_this.params.typedef(type);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (paramTypes_1_1 && !paramTypes_1_1.done && (_b = paramTypes_1.return)) _b.call(paramTypes_1);
}
finally { if (e_1) throw e_1.error; }
}
return _this;
}
CommandsModule.prototype.init = function () {
var _this = this;
var _b;
this.pubby.chat.on("message", this.messageListener);
var helpCommand = (_b = this.options.helpCommand) !== null && _b !== void 0 ? _b : ["help", "ajuda"];
if (typeof helpCommand === "string" || Array.isArray(helpCommand)) {
this.register({
command: helpCommand,
description: "Exibe a ajuda",
usage: "",
handler: function (msg) {
var _b;
var reg = new RegExp("^" + _this.options.prefix + "(" + [].concat(helpCommand).join("|") + ")\\s*", "gmi");
var cmdName = msg.text.replace(reg, "");
var command = (_b = _this[$commands].get(cmdName)) === null || _b === void 0 ? void 0 : _b.options;
if (command) {
var help_1 = "**Ajuda para " + cmdName + "**\n";
var aliases = [].concat(command.command);
if (command.description) {
help_1 += "\n" + command.description + "\n";
}
if (aliases.length > 1) {
help_1 += "\n**Aliases**: " + aliases.join(" | ") + "\n";
}
if (command.usage) {
help_1 += "\n**Como usar:**\n> " + cmdName + " " + command.usage + "\n";
}
return msg.reply(help_1);
}
var help = "**Ajuda**\n";
_this[$commands].forEach(function (cmd, name) {
help += "- **" + name + "** *" + cmd.options.description + "*\n";
});
msg.reply(help);
},
});
}
};
CommandsModule.prototype.register = function (cmd) {
var e_3, _b;
if (typeof cmd === "function" &&
cmd.prototype instanceof command.Command) {
var command$1 = new cmd();
command$1.commands = this;
try {
for (var _c = tslib.__values(command$1.aliases), _d = _c.next(); !_d.done; _d = _c.next()) {
var alias = _d.value;
this[$commands].set(alias, command$1);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
}
finally { if (e_3) throw e_3.error; }
}
return this;
}
return this.register(/** @class */ (function (_super) {
tslib.__extends(class_1, _super);
function class_1() {
return _super.call(this, cmd) || this;
}
class_1.prototype.run = function (message, args) { };
return class_1;
}(command.Command)));
};
CommandsModule.prototype.get = function (command) {
return this[$commands].get(command);
};
CommandsModule.prototype.dispose = function () {
this.pubby.chat.off("message", this.messageListener);
};
return CommandsModule;
}(module$1.PubbyModule));
_a = $commands;
exports.CommandsModule = CommandsModule;