mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Make structures for data stores extensible
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
54
src/util/Structures.js
Normal file
54
src/util/Structures.js
Normal file
@@ -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;
|
||||
Reference in New Issue
Block a user