@pubby.club/sdk
Version:
Pubby Development Kit
61 lines (60 loc) • 2.27 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandsModule = void 0;
var module_1 = require("../../module");
var chat_1 = require("../chat/chat");
var $commands = Symbol("commands");
var CommandsModule = /** @class */ (function (_super) {
__extends(CommandsModule, _super);
function CommandsModule(client, options) {
var _this = _super.call(this, client, options) || this;
_this[_a] = new Map();
_this.messageListener = function (message) {
if (!message.text.startsWith(_this.options.prefix)) {
return;
}
var name = message.text
.substring(_this.options.prefix.length)
.match(/^\S+/);
if (name) {
var handler = _this[$commands].get(name[0]);
if (handler) {
handler(message);
}
}
};
client.commands = _this;
// Import chat
client.use(chat_1.ChatModule);
client.chat.on("message", _this.messageListener);
return _this;
}
CommandsModule.prototype.init = function () { };
CommandsModule.prototype.add = function (name, handler) {
if (name.includes(" ")) {
throw new Error("The command name cannot contain spaces.");
}
this[$commands].set(name, handler);
return this;
};
CommandsModule.prototype.dispose = function () {
this.pubby.chat.off("message", this.messageListener);
};
return CommandsModule;
}(module_1.PubbyModule));
exports.CommandsModule = CommandsModule;
_a = $commands;