- 1 :
const Settings = require("./settings/Settings");
- 2 :
const Resolver = require("./settingResolver");
- 3 :
- 4 :
/**
- 5 :
* SettingGateway's driver to make new instances of it, with the purpose to handle different databases simultaneously.
- 6 :
* @class SettingsCache
- 7 :
*/
- 8 :
class SettingsCache {
- 9 :
- 10 :
/**
- 11 :
* @param {KomadaClient} client The Komada client
- 12 :
*/
- 13 :
constructor(client) {
- 14 :
/**
- 15 :
* The client this SettingsCache was created with.
- 16 :
* @name SettingsCache#client
- 17 :
* @type {KomadaClient}
- 18 :
* @readonly
- 19 :
*/
- 20 :
Object.defineProperty(this, "client", { value: client });
- 21 :
- 22 :
this.resolver = new Resolver(client);
- 23 :
- 24 :
/**
- 25 :
* The SettingGateway instance created to handle guild settings.
- 26 :
* @name SettingsCache#guilds
- 27 :
* @type {SettingGateway}
- 28 :
*/
- 29 :
this.guilds = new Settings(client, "guilds", this.validate.bind(null, this.resolver), this.defaultDataSchema, this.resolver);
- 30 :
}
- 31 :
- 32 :
/**
- 33 :
* Add a new instance of SettingGateway, with its own validateFunction and schema.
- 34 :
* @param {string} name The name for the new instance.
- 35 :
* @param {Function} validateFunction The function that validates the input.
- 36 :
* @param {Object} [schema={}] The schema.
- 37 :
* @returns {SettingGateway}
- 38 :
* @example
- 39 :
* // Add a new SettingGateway instance, called 'users', which input takes users, and stores a quote which is a string between 2 and 140 characters.
- 40 :
* const validate = async function(resolver, user) {
- 41 :
* const result = await resolver.user(user);
- 42 :
* if (!result) throw "The parameter <User> expects either a User ID or a User Object.";
- 43 :
* return result;
- 44 :
* };
- 45 :
* const schema = {
- 46 :
* quote: {
- 47 :
* type: "String",
- 48 :
* default: null,
- 49 :
* array: false,
- 50 :
* min: 2,
- 51 :
* max: 140,
- 52 :
* },
- 53 :
* };
- 54 :
* SettingsCache.add("users", validate, schema);
- 55 :
*/
- 56 :
async add(name, validateFunction, schema = {}) {
- 57 :
if (!name || typeof name !== "string") throw "You must pass a name for your new gateway and it must be a string.";
- 58 :
if (name in this) throw "There is already a Gateway with that name.";
- 59 :
if (typeof validateFunction !== "function") throw "You must pass a validate function.";
- 60 :
validateFunction = validateFunction.bind(null, this.resolver);
- 61 :
if (schema.constructor.name !== "Object") throw "Schema must be a valid object or left undefined for an empty object.";
- 62 :
this[name] = new Settings(this, name, validateFunction, schema, this.resolver);
- 63 :
return this[name];
- 64 :
}
- 65 :
- 66 :
/**
- 67 :
* The validator function Komada uses for guild settings.
- 68 :
* @param {SettingResolver} resolver The resolver instance this SettingGateway uses to parse the data.
- 69 :
* @param {(Object|string)} guild The data to validate.
- 70 :
* @returns {Promise<any>}
- 71 :
*/
- 72 :
async validate(resolver, guild) { // eslint-disable-line class-methods-use-this
- 73 :
const result = await resolver.guild(guild);
- 74 :
if (!result) throw "The parameter <Guild> expects either a Guild ID or a Guild Object.";
- 75 :
return result;
- 76 :
}
- 77 :
- 78 :
/**
- 79 :
* The data schema Komada uses for guild settings.
- 80 :
* @readonly
- 81 :
* @returns {Object}
- 82 :
*/
- 83 :
get defaultDataSchema() {
- 84 :
return {
- 85 :
prefix: {
- 86 :
type: "String",
- 87 :
default: this.client.config.prefix,
- 88 :
array: this.client.config.prefix.constructor.name === "Array",
- 89 :
sql: `TEXT NOT NULL DEFAULT '${this.client.config.prefix.constructor.name === "Array" ? JSON.stringify(this.client.config.prefix) : this.client.config.prefix}'`,
- 90 :
},
- 91 :
modRole: {
- 92 :
type: "Role",
- 93 :
default: null,
- 94 :
array: false,
- 95 :
sql: "TEXT",
- 96 :
},
- 97 :
adminRole: {
- 98 :
type: "Role",
- 99 :
default: null,
- 100 :
array: false,
- 101 :
sql: "TEXT",
- 102 :
},
- 103 :
disabledCommands: {
- 104 :
type: "Command",
- 105 :
default: [],
- 106 :
array: true,
- 107 :
sql: "TEXT DEFAULT '[]'",
- 108 :
},
- 109 :
};
- 110 :
}
- 111 :
- 112 :
}
- 113 :
- 114 :
module.exports = SettingsCache;