mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-20 05:23:31 +01:00
JK, back to Structures.extend
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Allows for the extension of built-in Discord.js structures that are instantiated by {@link DataStore DataStores}.
|
* Allows for the extension of built-in Discord.js structures that are instantiated by {@link DataStore DataStores}.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
class Structures {
|
class Structures {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -19,32 +17,46 @@ class Structures {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces a structure class with an extended one.
|
* Extends a structure.
|
||||||
* @param {string} structure Name of the structure to replace
|
* @param {string} structure Name of the structure class to extend
|
||||||
* @param {Function} extended Extended structure class/prototype function to replace with
|
* @param {Function} extender Function that takes the base class to extend as its only parameter and returns the
|
||||||
|
* extended class/prototype
|
||||||
|
* @returns {Function} Extended class/prototype returned from the extender
|
||||||
* @example
|
* @example
|
||||||
* const { Structures } = require('discord.js');
|
* const { Structures } = require('discord.js');
|
||||||
*
|
*
|
||||||
* class CoolGuild extends Structures.get('Guild') {
|
* Structures.extend('Guild', Guild => {
|
||||||
* constructor(client, data) {
|
* class CoolGuild extends Guild {
|
||||||
* super(client, data);
|
* constructor(client, data) {
|
||||||
* this.cool = true;
|
* super(client, data);
|
||||||
|
* this.cool = true;
|
||||||
|
* }
|
||||||
* }
|
* }
|
||||||
* }
|
|
||||||
*
|
*
|
||||||
* Structures.set('Guild', CoolGuild);
|
* return CoolGuild;
|
||||||
|
* });
|
||||||
*/
|
*/
|
||||||
static set(structure, extended) {
|
static extend(structure, extender) {
|
||||||
if (!structures[structure]) throw new RangeError(`"${structure}" is not a valid extensible structure.`);
|
if (!structures[structure]) throw new RangeError(`"${structure}" is not a valid extensible structure.`);
|
||||||
if (typeof extended !== 'function') {
|
if (typeof extender !== 'function') {
|
||||||
|
const received = `(received ${typeof extender})`;
|
||||||
throw new TypeError(
|
throw new TypeError(
|
||||||
`"extended" argument must be a structure class/prototype function (received ${typeof extended})`
|
`"extender" argument must be a function that returns the extended structure class/prototype ${received}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (Object.getPrototypeOf(extended) !== structures[structure]) {
|
|
||||||
throw new Error('The class/prototype function provided doesn\'t extend the existing structure class/prototype.');
|
const extended = extender(structures[structure]);
|
||||||
|
if (typeof extended !== 'function') {
|
||||||
|
throw new TypeError('The extender function must return the extended structure class/prototype.');
|
||||||
}
|
}
|
||||||
|
if (Object.getPrototypeOf(extended) !== structures[structure]) {
|
||||||
|
throw new Error(
|
||||||
|
'The class/prototype returned from the extender function must extend the existing structure class/prototype.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
structures[structure] = extended;
|
structures[structure] = extended;
|
||||||
|
return extended;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user