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