mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 18:13:29 +01:00
Merge branch 'custom-structures'
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
const AbstractHandler = require('./AbstractHandler');
|
const AbstractHandler = require('./AbstractHandler');
|
||||||
const { Events } = require('../../../../util/Constants');
|
const { Events } = require('../../../../util/Constants');
|
||||||
const ClientUser = require('../../../../structures/ClientUser');
|
let ClientUser;
|
||||||
|
|
||||||
class ReadyHandler extends AbstractHandler {
|
class ReadyHandler extends AbstractHandler {
|
||||||
handle(packet) {
|
handle(packet) {
|
||||||
@@ -12,6 +12,7 @@ class ReadyHandler extends AbstractHandler {
|
|||||||
data.user.user_settings = data.user_settings;
|
data.user.user_settings = data.user_settings;
|
||||||
data.user.user_guild_settings = data.user_guild_settings;
|
data.user.user_guild_settings = data.user_guild_settings;
|
||||||
|
|
||||||
|
if (!ClientUser) ClientUser = require('../../../../structures/ClientUser');
|
||||||
const clientUser = new ClientUser(client, data.user);
|
const clientUser = new ClientUser(client, data.user);
|
||||||
client.user = clientUser;
|
client.user = clientUser;
|
||||||
client.readyAt = new Date();
|
client.readyAt = new Date();
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ module.exports = {
|
|||||||
Permissions: require('./util/Permissions'),
|
Permissions: require('./util/Permissions'),
|
||||||
Snowflake: require('./util/Snowflake'),
|
Snowflake: require('./util/Snowflake'),
|
||||||
SnowflakeUtil: require('./util/Snowflake'),
|
SnowflakeUtil: require('./util/Snowflake'),
|
||||||
|
Structures: require('./util/Structures'),
|
||||||
Util: Util,
|
Util: Util,
|
||||||
util: Util,
|
util: Util,
|
||||||
version: require('../package.json').version,
|
version: require('../package.json').version,
|
||||||
@@ -46,7 +47,10 @@ module.exports = {
|
|||||||
CategoryChannel: require('./structures/CategoryChannel'),
|
CategoryChannel: require('./structures/CategoryChannel'),
|
||||||
Channel: require('./structures/Channel'),
|
Channel: require('./structures/Channel'),
|
||||||
ClientApplication: require('./structures/ClientApplication'),
|
ClientApplication: require('./structures/ClientApplication'),
|
||||||
ClientUser: require('./structures/ClientUser'),
|
get ClientUser() {
|
||||||
|
// This is a getter so that it properly extends any custom User class
|
||||||
|
return require('./structures/ClientUser');
|
||||||
|
},
|
||||||
ClientUserChannelOverride: require('./structures/ClientUserChannelOverride'),
|
ClientUserChannelOverride: require('./structures/ClientUserChannelOverride'),
|
||||||
ClientUserGuildSettings: require('./structures/ClientUserGuildSettings'),
|
ClientUserGuildSettings: require('./structures/ClientUserGuildSettings'),
|
||||||
ClientUserSettings: require('./structures/ClientUserSettings'),
|
ClientUserSettings: require('./structures/ClientUserSettings'),
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
|
let Structures;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages the creation, retrieval and deletion of a specific data model.
|
* Manages the creation, retrieval and deletion of a specific data model.
|
||||||
@@ -7,8 +8,9 @@ const Collection = require('../util/Collection');
|
|||||||
class DataStore extends Collection {
|
class DataStore extends Collection {
|
||||||
constructor(client, iterable, holds) {
|
constructor(client, iterable, holds) {
|
||||||
super();
|
super();
|
||||||
|
if (!Structures) Structures = require('../util/Structures');
|
||||||
Object.defineProperty(this, 'client', { value: client });
|
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);
|
if (iterable) for (const item of iterable) this.create(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,32 +66,37 @@ class Channel extends Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static create(client, data, guild) {
|
static create(client, data, guild) {
|
||||||
const DMChannel = require('./DMChannel');
|
const Structures = require('../util/Structures');
|
||||||
const GroupDMChannel = require('./GroupDMChannel');
|
|
||||||
const TextChannel = require('./TextChannel');
|
|
||||||
const VoiceChannel = require('./VoiceChannel');
|
|
||||||
const CategoryChannel = require('./CategoryChannel');
|
|
||||||
const GuildChannel = require('./GuildChannel');
|
|
||||||
let channel;
|
let channel;
|
||||||
if (data.type === ChannelTypes.DM) {
|
if (data.type === ChannelTypes.DM) {
|
||||||
|
const DMChannel = Structures.get('DMChannel');
|
||||||
channel = new DMChannel(client, data);
|
channel = new DMChannel(client, data);
|
||||||
} else if (data.type === ChannelTypes.GROUP) {
|
} else if (data.type === ChannelTypes.GROUP) {
|
||||||
|
const GroupDMChannel = Structures.get('GroupDMChannel');
|
||||||
channel = new GroupDMChannel(client, data);
|
channel = new GroupDMChannel(client, data);
|
||||||
} else {
|
} else {
|
||||||
guild = guild || client.guilds.get(data.guild_id);
|
guild = guild || client.guilds.get(data.guild_id);
|
||||||
if (guild) {
|
if (guild) {
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case ChannelTypes.TEXT:
|
case ChannelTypes.TEXT: {
|
||||||
|
const TextChannel = Structures.get('TextChannel');
|
||||||
channel = new TextChannel(guild, data);
|
channel = new TextChannel(guild, data);
|
||||||
break;
|
break;
|
||||||
case ChannelTypes.VOICE:
|
}
|
||||||
|
case ChannelTypes.VOICE: {
|
||||||
|
const VoiceChannel = Structures.get('VoiceChannel');
|
||||||
channel = new VoiceChannel(guild, data);
|
channel = new VoiceChannel(guild, data);
|
||||||
break;
|
break;
|
||||||
case ChannelTypes.CATEGORY:
|
}
|
||||||
|
case ChannelTypes.CATEGORY: {
|
||||||
|
const CategoryChannel = Structures.get('CategoryChannel');
|
||||||
channel = new CategoryChannel(guild, data);
|
channel = new CategoryChannel(guild, data);
|
||||||
break;
|
break;
|
||||||
default:
|
}
|
||||||
|
default: {
|
||||||
|
const GuildChannel = Structures.get('GuildChannel');
|
||||||
channel = new GuildChannel(guild, data);
|
channel = new GuildChannel(guild, data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
guild.channels.set(channel.id, channel);
|
guild.channels.set(channel.id, channel);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const User = require('./User');
|
const Structures = require('../util/Structures');
|
||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
const ClientUserSettings = require('./ClientUserSettings');
|
const ClientUserSettings = require('./ClientUserSettings');
|
||||||
const ClientUserGuildSettings = require('./ClientUserGuildSettings');
|
const ClientUserGuildSettings = require('./ClientUserGuildSettings');
|
||||||
@@ -11,7 +11,7 @@ const Guild = require('./Guild');
|
|||||||
* Represents the logged in client's Discord user.
|
* Represents the logged in client's Discord user.
|
||||||
* @extends {User}
|
* @extends {User}
|
||||||
*/
|
*/
|
||||||
class ClientUser extends User {
|
class ClientUser extends Structures.get('User') {
|
||||||
_patch(data) {
|
_patch(data) {
|
||||||
super._patch(data);
|
super._patch(data);
|
||||||
|
|
||||||
|
|||||||
80
src/util/Structures.js
Normal file
80
src/util/Structures.js
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
/**
|
||||||
|
* Allows for the extension of built-in Discord.js structures that are instantiated by {@link DataStore DataStores}.
|
||||||
|
*/
|
||||||
|
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.
|
||||||
|
* @param {string} 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') {
|
||||||
|
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;
|
||||||
|
return extended;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const structures = {
|
||||||
|
Emoji: require('../structures/Emoji'),
|
||||||
|
DMChannel: require('../structures/DMChannel'),
|
||||||
|
GroupDMChannel: require('../structures/GroupDMChannel'),
|
||||||
|
TextChannel: require('../structures/TextChannel'),
|
||||||
|
VoiceChannel: require('../structures/VoiceChannel'),
|
||||||
|
CategoryChannel: require('../structures/CategoryChannel'),
|
||||||
|
GuildChannel: require('../structures/GuildChannel'),
|
||||||
|
GuildMember: require('../structures/GuildMember'),
|
||||||
|
Guild: require('../structures/Guild'),
|
||||||
|
Message: require('../structures/Message'),
|
||||||
|
MessageReaction: require('../structures/MessageReaction'),
|
||||||
|
Presence: require('../structures/Presence').Presence,
|
||||||
|
Role: require('../structures/Role'),
|
||||||
|
User: require('../structures/User'),
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Structures;
|
||||||
Reference in New Issue
Block a user