Structures.js
3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
/**
* An extendable structure:
* * **`GuildEmoji`**
* * **`DMChannel`**
* * **`TextChannel`**
* * **`VoiceChannel`**
* * **`CategoryChannel`**
* * **`NewsChannel`**
* * **`StoreChannel`**
* * **`GuildMember`**
* * **`Guild`**
* * **`Message`**
* * **`MessageReaction`**
* * **`Presence`**
* * **`ClientPresence`**
* * **`VoiceState`**
* * **`Role`**
* * **`User`**
* @typedef {string} ExtendableStructure
*/
/**
* Allows for the extension of built-in Discord.js structures that are instantiated by {@link BaseManager Managers}.
*/
class Structures {
constructor() {
throw new Error(`The ${this.constructor.name} class may not be instantiated.`);
}
/**
* Retrieves a structure class.
* @param {string} structure Name of the structure to retrieve
* @returns {Function}
*/
static get(structure) {
if (typeof structure === 'string') return structures[structure];
throw new TypeError(`"structure" argument must be a string (received ${typeof structure})`);
}
/**
* Extends a structure.
* <warn> Make sure to extend all structures before instantiating your client.
* Extending after doing so may not work as expected. </warn>
* @param {ExtendableStructure} structure 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;
* }
* }
*
* return CoolGuild;
* });
*/
static extend(structure, extender) {
if (!structures[structure]) throw new RangeError(`"${structure}" is not a valid extensible structure.`);
if (typeof extender !== 'function') {
const received = `(received ${typeof extender})`;
throw new TypeError(
`"extender" argument must be a function that returns the extended structure class/prototype ${received}.`,
);
}
const extended = extender(structures[structure]);
if (typeof extended !== 'function') {
const received = `(received ${typeof extended})`;
throw new TypeError(`The extender function must return the extended structure class/prototype ${received}.`);
}
if (!(extended.prototype instanceof structures[structure])) {
const prototype = Object.getPrototypeOf(extended);
const received = `${extended.name || 'unnamed'}${prototype.name ? ` extends ${prototype.name}` : ''}`;
throw new Error(
'The class/prototype returned from the extender function must extend the existing structure class/prototype' +
` (received function ${received}; expected extension of ${structures[structure].name}).`,
);
}
structures[structure] = extended;
return extended;
}
}
const structures = {
GuildEmoji: require('../structures/GuildEmoji'),
DMChannel: require('../structures/DMChannel'),
TextChannel: require('../structures/TextChannel'),
VoiceChannel: require('../structures/VoiceChannel'),
CategoryChannel: require('../structures/CategoryChannel'),
NewsChannel: require('../structures/NewsChannel'),
StoreChannel: require('../structures/StoreChannel'),
GuildMember: require('../structures/GuildMember'),
Guild: require('../structures/Guild'),
Message: require('../structures/Message'),
MessageReaction: require('../structures/MessageReaction'),
Presence: require('../structures/Presence').Presence,
ClientPresence: require('../structures/ClientPresence'),
VoiceState: require('../structures/VoiceState'),
Role: require('../structures/Role'),
User: require('../structures/User'),
};
module.exports = Structures;