cumsystem
Version:
simple command system and command handler
141 lines (140 loc) • 5.33 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 (b.hasOwnProperty(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 __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Discord = __importStar(require("discord.js"));
var Command_1 = require("./Command");
var defaultCommands_1 = require("../defaultCommands");
var events_1 = require("events");
Discord; // eslint bein a DUMBASS
Command_1.Command;
/**
* Represents the commands and options of a single bot
*/
var System = /** @class */ (function (_super) {
__extends(System, _super);
/**
* Makes a new command system with no commands except the default commands (ping, help)
* @example
* ```typescript
* const Discord = require('discord.js');
* const CommandSystem = require('cumsystem');
*
* const client = new Discord.Client();
* const cs = new CommandSystem.System(client, '!');
* ```
* @param {Discord.Client} client The client of the command system
* @param {string} prefix The prefix to use for command detection
*/
function System(client, prefix) {
var _this = _super.call(this) || this;
/** The prefix used for detecting commands */
_this.prefix = '!';
/** An array of commands */
_this.commands = [];
_this.setValues = {};
_this.client = client;
_this.prefix = prefix;
defaultCommands_1.addCommands(_this);
client.on('ready', function () {
client.fetchApplication()
.then(function (app) {
var _a;
_this.application = app;
_this.ownerID = (_a = _this.application.owner) === null || _a === void 0 ? void 0 : _a.id;
if (!_this.ownerID)
process.emitWarning('Couldn\'t fetch owner id from the client\'s application');
});
});
return _this;
}
/**
* Add a command to the commands list
* @example
* ```typescript
* cs.addCommand(new CommandSystem.SimpleCommand('hi', () => {
* return 'hello!';
* }));
* ```
* @param {Command} command The command itself
*/
System.prototype.addCommand = function (command) {
this.commands.push(command);
};
/**
* Sets a client for the command system to use
* @param {Discord.Client} clientSet The client
*/
System.prototype.setClient = function (clientSet) {
this.client = clientSet;
};
/**
* Sets a prefix for the command system to use
* @param {string} prefixSet The prefix
*/
System.prototype.setPrefix = function (prefixSet) {
this.prefix = prefixSet;
};
/**
* Set a key to a value in the system (useful for passing values through modules without directly passing them to the function)
* @param {string} key
* @param {any} value
*/
System.prototype.set = function (key, value) {
this.setValues[key] = value;
return value;
};
/**
* Get a value from a key in the system
* @param {string} key
*/
System.prototype.get = function (key) {
return this.setValues[key];
};
/**
* Parses messages, recommended to put in your client message listener
* @param {Discord.Message} message The message
* @param {string} prefixOverride A prefix override to use instead of the default one, useful for custom prefixes
*/
System.prototype.parseMessage = function (message, prefixOverride) {
var _this = this;
var _a;
var content = message.content;
var thisPrefix = this.prefix;
if (prefixOverride)
thisPrefix = prefixOverride;
if (message.author.bot || message.author.id === ((_a = message.client.user) === null || _a === void 0 ? void 0 : _a.id))
return;
if (content.startsWith(thisPrefix) || content.startsWith(this.prefix)) {
content = content.slice(content.startsWith(thisPrefix) ? thisPrefix.length : this.prefix.length, content.length);
var cmd_1 = content.split(' ')[0];
this.commands.forEach(function (command) {
if (command.name === cmd_1 || command.aliases.includes(cmd_1)) {
if ((message.content.startsWith(thisPrefix) || (message.content.startsWith(_this.prefix) && command['ignorePrefix'])) || (thisPrefix == _this.prefix)) {
command.runCommand(message, _this);
}
}
});
}
};
return System;
}(events_1.EventEmitter));
exports.System = System;