Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

Commit

Permalink
Gateway code cleanup (#489)
Browse files Browse the repository at this point in the history
* cleanup: move code to a more appropriate place

* fix: client not this

* fix: option is plural not singular

* docs: these don't belong here...
  • Loading branch information
UnseenFaith authored and kyranet committed Nov 20, 2018
1 parent b2d6ce0 commit 28da135
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 49 deletions.
25 changes: 0 additions & 25 deletions src/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const TaskStore = require('./structures/TaskStore');
const GatewayDriver = require('./settings/gateway/GatewayDriver');

// lib/settings/schema
const Gateway = require('./settings/gateway/Gateway');
const Schema = require('./settings/schema/Schema');

// lib/util
Expand Down Expand Up @@ -248,30 +247,6 @@ class KlasaClient extends Discord.Client {
*/
this.gateways = new GatewayDriver(this);

const { guilds, users, clientStorage } = this.options.gateways;
const guildSchema = 'schema' in guilds ? guilds.schema : this.constructor.defaultGuildSchema;
const userSchema = 'schema' in users ? users.schema : this.constructor.defaultUserSchema;
const clientSchema = 'schema' in clientStorage ? clientStorage.schema : this.constructor.defaultClientSchema;

// Update Guild Schema with Keys needed in Klasa
const prefixKey = guildSchema.get('prefix');
if (!prefixKey || prefixKey.default === null) {
guildSchema.add('prefix', 'string', { array: Array.isArray(this.options.prefix), default: this.options.prefix });
}

const languageKey = guildSchema.get('language');
if (!languageKey || languageKey.default === null) {
guildSchema.add('language', 'language', { default: this.options.language });
}

guildSchema.add('disableNaturalPrefix', 'boolean', { configurable: Boolean(this.options.regexPrefix) });

// Register default gateways
this.gateways
.register(new Gateway(this, 'guilds', { schema: guildSchema, ...this.options.gateways.guilds || {} }))
.register(new Gateway(this, 'users', { schema: userSchema, ...this.options.gateways.users || {} }))
.register(new Gateway(this, 'clientStorage', { schema: clientSchema, ...this.options.gateways.clientStorage || {} }));

/**
* The Settings instance that handles this client's settings
* @since 0.5.0
Expand Down
49 changes: 25 additions & 24 deletions src/lib/settings/gateway/GatewayDriver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const GatewayStorage = require('./GatewayStorage');
const Gateway = require('./Gateway');
const Type = require('../../util/Type');
const { Collection } = require('discord.js');

Expand All @@ -8,30 +9,6 @@ const { Collection } = require('discord.js');
*/
class GatewayDriver extends Collection {

/**
* @typedef {Object} GatewayDriverRegisterOptions
* @property {string} [provider = this.client.options.providers.default] The name of the provider to use
* @property {Schema} [schema] The schema to use for this gateway.
* @property {string|string[]|true} [syncArg] The sync args to pass to Gateway#sync during Gateway init
*/

/**
* @typedef {Object} GatewayDriverGuildsSchema
* @property {SchemaPieceOptions} prefix The per-guild's configurable prefix key
* @property {SchemaPieceOptions} language The per-guild's configurable language key
* @property {SchemaPieceOptions} disableNaturalPrefix The per-guild's configurable disableNaturalPrefix key
* @property {SchemaPieceOptions} disabledCommands The per-guild's configurable disabledCommands key
* @private
*/

/**
* @typedef {Object} GatewayDriverClientStorageSchema
* @property {SchemaPieceOptions} userBlacklist The client's configurable user blacklist key
* @property {SchemaPieceOptions} guildBlacklist The client's configurable guild blacklist key
* @property {SchemaPieceOptions} schedules The schedules where {@link ScheduledTask}s are stored at
* @private
*/

/**
* @since 0.3.0
* @param {KlasaClient} client The Klasa client
Expand All @@ -47,6 +24,30 @@ class GatewayDriver extends Collection {
* @readonly
*/
Object.defineProperty(this, 'client', { value: client });

// Setup default gateways and adjust client options as necessary
const { guilds, users, clientStorage } = client.options.gateways;
guilds.schema = 'schema' in guilds ? guilds.schema : client.constructor.defaultGuildSchema;
users.schema = 'schema' in users ? users.schema : client.constructor.defaultUserSchema;
clientStorage.schema = 'schema' in clientStorage ? clientStorage.schema : client.constructor.defaultClientSchema;

const prefix = guilds.schema.get('prefix');
const language = guilds.schema.get('language');

if (!prefix || prefix.default === null) {
guilds.schema.add('prefix', 'string', { array: Array.isArray(client.options.prefix), default: client.options.prefix });
}

if (!language || language.default === null) {
guilds.schema.add('language', 'language', { default: client.options.language });
}

guilds.schema.add('disableNaturalPrefix', 'boolean', { configurable: Boolean(client.options.regexPrefix) });

this
.register(new Gateway(client, 'guilds', guilds))
.register(new Gateway(client, 'users', users))
.register(new Gateway(client, 'clientStorage', clientStorage));
}


Expand Down

0 comments on commit 28da135

Please sign in to comment.