mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
Replace Structures.extend with set
This commit is contained in:
@@ -8,51 +8,30 @@ class Structures {
|
||||
|
||||
/**
|
||||
* Retrieves a structure class.
|
||||
* @param {string|Function} structure Name of the structure or a class/prototype function to use the name of
|
||||
* @param {string} structure Name of the structure to retrieve
|
||||
* @returns {Function}
|
||||
*/
|
||||
static get(structure) {
|
||||
if (typeof structure === 'string') return structures[structure];
|
||||
if (typeof structure === 'function') return structures[structure.name];
|
||||
throw new TypeError(`Structure to retrieve must be a string or class/prototype function, not ${typeof structure}.`);
|
||||
throw new TypeError(`"structure" argument must be a string (received ${typeof structure})`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extends a structure.
|
||||
* @param {string} name Name of the structure class to extend
|
||||
* @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
|
||||
* const { Structures } = require('discord.js');
|
||||
*
|
||||
* Structures.extend('Guild', Guild =>
|
||||
* class CoolGuild extends Guild {
|
||||
* constructor(client, data) {
|
||||
* super(client, data);
|
||||
* this.cool = true;
|
||||
* }
|
||||
* }
|
||||
* );
|
||||
* Replaces a structure class with an extended one.
|
||||
* @param {string} structure Name of the structure to replace
|
||||
* @param {Function} extended Extended structure class/prototype function to replace with
|
||||
*/
|
||||
static extend(name, extender) {
|
||||
if (!structures[name]) throw new RangeError(`"${name}" is not a valid extensible structure.`);
|
||||
if (typeof extender !== 'function') {
|
||||
throw new TypeError('The extender must be a function that returns the extended structure class/prototype.');
|
||||
}
|
||||
|
||||
const custom = extender(structures[name]);
|
||||
if (typeof custom !== 'function') {
|
||||
throw new TypeError('The extender function must return the extended structure class/prototype.');
|
||||
}
|
||||
if (Object.getPrototypeOf(custom) !== structures[name]) {
|
||||
throw new Error(
|
||||
"The class/prototype returned from the extender function doesn't extend the existing structure class/prototype."
|
||||
static set(structure, extended) {
|
||||
if (!structures[structure]) throw new RangeError(`"${structure}" is not a valid extensible structure.`);
|
||||
if (typeof extended !== 'function') {
|
||||
throw new TypeError(
|
||||
`"extended" argument must be a structure class/prototype function (received ${typeof extended})`
|
||||
);
|
||||
}
|
||||
|
||||
structures[name] = custom;
|
||||
return custom;
|
||||
if (Object.getPrototypeOf(extended) !== structures[structure]) {
|
||||
throw new Error('The class/prototype function provided doesn\'t extend the existing structure class/prototype.');
|
||||
}
|
||||
structures[structure] = extended;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user