commandbot
Version:
A framework that helps you create your own Discord bot easier.
102 lines (101 loc) • 3.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrefixManager = void 0;
const discord_js_1 = require("discord.js");
const commandsTypes_js_1 = require("../commands/commandsTypes.js");
/**
* Maps server IDs with command prefixes and allows to manage them
* @class
* @experimental This feature should be fully functional but it doesn't store its data in any kind of local storage or cache. All informations get lost after restarting the application. It is possible to create a store for that data (exporting to a file or in some kind of database) and then load it every time the application starts.
*/
class PrefixManager {
/**
* @constructor
* @param manager - manager attached to this object
* @param {?string} [defaultPrefix] - default global prefix
*/
constructor(manager, defaultPrefix) {
/**
* Prefixes data
* @type {Map<string, string>}
* @private
* @readonly
*/
this._prefixes = new Map();
/**
* Global scope identifier
* @type {string}
* @private
* @readonly
*/
this._global = "GLOBAL";
this.manager = manager;
if (defaultPrefix) {
if (!commandsTypes_js_1.CommandRegExps.prefix.test(defaultPrefix)) {
throw new Error(`Prefix value for ${this._global} is incorrect`);
}
this._prefixes.set(this._global, defaultPrefix);
}
}
/**
* Manager global prefix
* @type {?string}
*/
get globalPrefix() {
return this._prefixes.get(this._global) || null;
}
/**
* Manager data
* @type {Readonly<Map<string, string>>}
*/
get prefixes() {
return Object.freeze(Object.create(this._prefixes));
}
/**
* Get prefix for a specified scope
* @param {ScopeResolvable} scope - guild object or ID
* @returns {?string} a prefix used in given scope
* @public
*/
get(scope) {
if (!scope)
return this.globalPrefix;
else {
const id = scope instanceof discord_js_1.Guild ? scope.id : scope;
return this._prefixes.get(id) || this.globalPrefix;
}
}
/**
* Set prefix for a specific scope
* @param {string} prefix - new prefix
* @param {?ScopeResolvable} [scope] - guild string or ID
* @return {void}
* @public
*/
set(prefix, scope) {
if (!commandsTypes_js_1.CommandRegExps.prefix.test(prefix))
throw new Error(`"${prefix}" is not a valid prefix`);
if (!scope) {
this._prefixes.set(this._global, prefix);
}
else {
const id = scope instanceof discord_js_1.Guild ? scope.id : scope;
if (!this.manager.client.client.guilds.cache.get(id))
throw new Error(`${id} is not a valid guild ID`);
this._prefixes.set(id, prefix);
}
}
/**
* Remove prefix for the specified scope
* @param {?ScopeResolvable} [scope] - guild string or ID
* @return {boolean}
* @public
*/
remove(scope) {
if (!scope)
return this._prefixes.delete(this._global);
const id = scope instanceof discord_js_1.Guild ? scope.id : scope;
return this._prefixes.delete(id);
}
}
exports.PrefixManager = PrefixManager;