From cfa512c447469956d738df938137f97104734363 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Sun, 19 Nov 2017 21:16:14 -0500 Subject: [PATCH] Make structures for data stores extensible --- src/index.js | 1 + src/stores/DataStore.js | 3 ++- src/util/Structures.js | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/util/Structures.js diff --git a/src/index.js b/src/index.js index de6a6e66c..90cf5b4c0 100644 --- a/src/index.js +++ b/src/index.js @@ -18,6 +18,7 @@ module.exports = { Permissions: require('./util/Permissions'), Snowflake: require('./util/Snowflake'), SnowflakeUtil: require('./util/Snowflake'), + Structures: require('./util/Structures'), Util: Util, util: Util, version: require('../package.json').version, diff --git a/src/stores/DataStore.js b/src/stores/DataStore.js index 398910d50..fc9aa414d 100644 --- a/src/stores/DataStore.js +++ b/src/stores/DataStore.js @@ -1,4 +1,5 @@ const Collection = require('../util/Collection'); +const Structures = require('../util/Structures'); /** * Manages the creation, retrieval and deletion of a specific data model. @@ -8,7 +9,7 @@ class DataStore extends Collection { constructor(client, iterable, holds) { super(); Object.defineProperty(this, 'client', { value: client }); - Object.defineProperty(this, 'holds', { value: holds }); + Object.defineProperty(this, 'holds', { value: Structures.get(holds.name) }); if (iterable) for (const item of iterable) this.create(item); } diff --git a/src/util/Structures.js b/src/util/Structures.js new file mode 100644 index 000000000..d09684c50 --- /dev/null +++ b/src/util/Structures.js @@ -0,0 +1,54 @@ +/** + * Allows for the extension of built-in Discord.js structures that are instantiated by {@link DataStore}s. + * When extending a built-in structure, it is important to both get the class you're extending from here, + * and to set it here afterwards. + * @example + * const { Structures } = require('discord.js); + * + * class CoolGuild extends Structures.get('Guild') { + * constructor(client, data) { + * super(client, data); + * this.cool = true; + * } + * } + * + * Structures.set('Guild', CoolGuild); + */ +class Structures { + constructor() { + throw new Error(`The ${this.constructor.name} class may not be instantiated.`); + } + + /** + * Retrieves a structure class. + * @param {string} name Name of the base structure + * @returns {Function} + */ + static get(name) { + return structures[name]; + } + + /** + * Overrides a structure class. + * @param {string} name Name of the base structure + * @param {Function} custom Extended structure class to override with + */ + static set(name, custom) { + structures[name] = custom; + } +} + +const structures = { + Channel: require('./structures/Channel'), + Emoji: require('./structures/Emoji'), + GuildChannel: require('./structures/GuildChannel'), + GuildMember: require('./structures/GuildMember'), + Guild: require('./structures/Guild'), + Message: require('./structures/Message'), + Presence: require('./structures/Presence'), + Reaction: require('./structures/Reaction'), + Role: require('./structures/Role'), + User: require('./structures/User'), +}; + +module.exports = Structures;