commandbot
Version:
A framework that helps you create your own Discord bot easier.
105 lines (104 loc) • 3.36 kB
JavaScript
import { Guild } from "discord.js";
import { CommandRegExps } from "../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.
*/
export class PrefixManager {
/**
* Prefixes data
* @type {Map<string, string>}
* @private
* @readonly
*/
_prefixes = new Map();
/**
* Global scope identifier
* @type {string}
* @private
* @readonly
*/
_global = "GLOBAL";
/**
* Command manager associated with the object
* @type {CommandManager}
* @public
* @readonly
*/
manager;
/**
* @constructor
* @param manager - manager attached to this object
* @param {?string} [defaultPrefix] - default global prefix
*/
constructor(manager, defaultPrefix) {
this.manager = manager;
if (defaultPrefix) {
if (!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 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 (!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 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 Guild ? scope.id : scope;
return this._prefixes.delete(id);
}
}