fix(Structures): remove Structures (#6027)

This commit is contained in:
1Computer1
2021-07-04 07:26:35 -04:00
committed by GitHub
parent db60e367b4
commit ab0b3b9a07
11 changed files with 31 additions and 193 deletions

View File

@@ -10,6 +10,7 @@ const ChannelManager = require('../managers/ChannelManager');
const GuildManager = require('../managers/GuildManager');
const UserManager = require('../managers/UserManager');
const ShardClientUtil = require('../sharding/ShardClientUtil');
const ClientPresence = require('../structures/ClientPresence');
const GuildPreview = require('../structures/GuildPreview');
const GuildTemplate = require('../structures/GuildTemplate');
const Invite = require('../structures/Invite');
@@ -22,7 +23,6 @@ const DataResolver = require('../util/DataResolver');
const Intents = require('../util/Intents');
const Options = require('../util/Options');
const Permissions = require('../util/Permissions');
const Structures = require('../util/Structures');
/**
* The main hub for interacting with the Discord API, and the starting point for any bot.
@@ -119,7 +119,6 @@ class Client extends BaseClient {
*/
this.channels = new ChannelManager(this);
const ClientPresence = Structures.get('ClientPresence');
/**
* The presence of the Client
* @private

View File

@@ -1,8 +1,10 @@
'use strict';
const Action = require('./Action');
const ButtonInteraction = require('../../structures/ButtonInteraction');
const CommandInteraction = require('../../structures/CommandInteraction');
const SelectMenuInteraction = require('../../structures/SelectMenuInteraction');
const { Events, InteractionTypes, MessageComponentTypes } = require('../../util/Constants');
const Structures = require('../../util/Structures');
let deprecationEmitted = false;
@@ -16,15 +18,15 @@ class InteractionCreateAction extends Action {
let InteractionType;
switch (data.type) {
case InteractionTypes.APPLICATION_COMMAND:
InteractionType = Structures.get('CommandInteraction');
InteractionType = CommandInteraction;
break;
case InteractionTypes.MESSAGE_COMPONENT:
switch (data.data.component_type) {
case MessageComponentTypes.BUTTON:
InteractionType = Structures.get('ButtonInteraction');
InteractionType = ButtonInteraction;
break;
case MessageComponentTypes.SELECT_MENU:
InteractionType = Structures.get('SelectMenuInteraction');
InteractionType = SelectMenuInteraction;
break;
default:
client.emit(

View File

@@ -1,15 +1,14 @@
'use strict';
const Action = require('./Action');
const VoiceState = require('../../structures/VoiceState');
const { Events } = require('../../util/Constants');
const Structures = require('../../util/Structures');
class VoiceStateUpdate extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.cache.get(data.guild_id);
if (guild) {
const VoiceState = Structures.get('VoiceState');
// Update the state
const oldState =
guild.voiceStates.cache.get(data.user_id)?._clone() ?? new VoiceState(guild, { user_id: data.user_id });

View File

@@ -26,7 +26,6 @@ module.exports = {
Options: require('./util/Options'),
Permissions: require('./util/Permissions'),
SnowflakeUtil: require('./util/SnowflakeUtil'),
Structures: require('./util/Structures'),
SystemChannelFlags: require('./util/SystemChannelFlags'),
ThreadMemberFlags: require('./util/ThreadMemberFlags'),
UserFlags: require('./util/UserFlags'),

View File

@@ -3,8 +3,6 @@
const BaseManager = require('./BaseManager');
const { Error } = require('../errors');
let Structures;
/**
* Manages the API methods of a data model along with a collection of instances.
* @extends {BaseManager}
@@ -14,8 +12,6 @@ class DataManager extends BaseManager {
constructor(client, holds) {
super(client);
if (!Structures) Structures = require('../util/Structures');
/**
* The data structure belonging to this manager.
* @name DataManager#holds
@@ -23,7 +19,7 @@ class DataManager extends BaseManager {
* @private
* @readonly
*/
Object.defineProperty(this, 'holds', { value: Structures.get(holds.name) ?? holds });
Object.defineProperty(this, 'holds', { value: holds });
}
/**

View File

@@ -1,6 +1,14 @@
'use strict';
const Base = require('./Base');
let CategoryChannel;
let DMChannel;
let NewsChannel;
let StageChannel;
let StoreChannel;
let TextChannel;
let ThreadChannel;
let VoiceChannel;
const { ChannelTypes, ThreadChannelTypes } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil');
@@ -120,11 +128,18 @@ class Channel extends Base {
}
static create(client, data, guild) {
const Structures = require('../util/Structures');
if (!CategoryChannel) CategoryChannel = require('./CategoryChannel');
if (!DMChannel) DMChannel = require('./DMChannel');
if (!NewsChannel) NewsChannel = require('./NewsChannel');
if (!StageChannel) StageChannel = require('./StageChannel');
if (!StoreChannel) StoreChannel = require('./StoreChannel');
if (!TextChannel) TextChannel = require('./TextChannel');
if (!ThreadChannel) ThreadChannel = require('./ThreadChannel');
if (!VoiceChannel) VoiceChannel = require('./VoiceChannel');
let channel;
if (!data.guild_id && !guild) {
if ((data.recipients && data.type !== ChannelTypes.GROUP) || data.type === ChannelTypes.DM) {
const DMChannel = Structures.get('DMChannel');
channel = new DMChannel(client, data);
} else if (data.type === ChannelTypes.GROUP) {
const PartialGroupDMChannel = require('./PartialGroupDMChannel');
@@ -136,39 +151,32 @@ class Channel extends Base {
if (guild) {
switch (data.type) {
case ChannelTypes.TEXT: {
const TextChannel = Structures.get('TextChannel');
channel = new TextChannel(guild, data);
break;
}
case ChannelTypes.VOICE: {
const VoiceChannel = Structures.get('VoiceChannel');
channel = new VoiceChannel(guild, data);
break;
}
case ChannelTypes.CATEGORY: {
const CategoryChannel = Structures.get('CategoryChannel');
channel = new CategoryChannel(guild, data);
break;
}
case ChannelTypes.NEWS: {
const NewsChannel = Structures.get('NewsChannel');
channel = new NewsChannel(guild, data);
break;
}
case ChannelTypes.STORE: {
const StoreChannel = Structures.get('StoreChannel');
channel = new StoreChannel(guild, data);
break;
}
case ChannelTypes.STAGE: {
const StageChannel = Structures.get('StageChannel');
channel = new StageChannel(guild, data);
break;
}
case ChannelTypes.NEWS_THREAD:
case ChannelTypes.PUBLIC_THREAD:
case ChannelTypes.PRIVATE_THREAD: {
const ThreadChannel = Structures.get('ThreadChannel');
channel = new ThreadChannel(guild, data);
channel.parent?.threads.cache.set(channel.id, channel);
break;

View File

@@ -1,13 +1,13 @@
'use strict';
const User = require('./User');
const DataResolver = require('../util/DataResolver');
const Structures = require('../util/Structures');
/**
* Represents the logged in client's Discord user.
* @extends {User}
*/
class ClientUser extends Structures.get('User') {
class ClientUser extends User {
constructor(client, data) {
super(client, data);
this._typing = new Map();

View File

@@ -1,11 +1,12 @@
'use strict';
const Base = require('./Base');
const { Presence } = require('./Presence');
const VoiceState = require('./VoiceState');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { Error } = require('../errors');
const GuildMemberRoleManager = require('../managers/GuildMemberRoleManager');
const Permissions = require('../util/Permissions');
let Structures;
/**
* Represents a member of a guild on Discord.
@@ -130,8 +131,6 @@ class GuildMember extends Base {
* @readonly
*/
get voice() {
if (!Structures) Structures = require('../util/Structures');
const VoiceState = Structures.get('VoiceState');
return this.guild.voiceStates.cache.get(this.id) ?? new VoiceState(this.guild, { user_id: this.id });
}
@@ -159,8 +158,6 @@ class GuildMember extends Base {
* @readonly
*/
get presence() {
if (!Structures) Structures = require('../util/Structures');
const Presence = Structures.get('Presence');
return (
this.guild.presences.cache.get(this.id) ??
new Presence(this.client, {

View File

@@ -1,13 +1,12 @@
'use strict';
const Base = require('./Base');
const { Presence } = require('./Presence');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { Error } = require('../errors');
const SnowflakeUtil = require('../util/SnowflakeUtil');
const UserFlags = require('../util/UserFlags');
let Structures;
/**
* Represents a user on Discord.
* @implements {TextBasedChannel}
@@ -153,8 +152,6 @@ class User extends Base {
for (const guild of this.client.guilds.cache.values()) {
if (guild.presences.cache.has(this.id)) return guild.presences.cache.get(this.id);
}
if (!Structures) Structures = require('../util/Structures');
const Presence = Structures.get('Presence');
return new Presence(this.client, { user: { id: this.id } });
}

View File

@@ -1,122 +0,0 @@
'use strict';
/**
* An extendable structure:
* * **`GuildEmoji`**
* * **`DMChannel`**
* * **`TextChannel`**
* * **`VoiceChannel`**
* * **`CategoryChannel`**
* * **`NewsChannel`**
* * **`StoreChannel`**
* * **`StageChannel`**
* * **`ThreadChannel`**
* * **`GuildMember`**
* * **`ThreadMember`**
* * **`Guild`**
* * **`Message`**
* * **`MessageReaction`**
* * **`Presence`**
* * **`ClientPresence`**
* * **`VoiceState`**
* * **`Role`**
* * **`User`**
* * **`CommandInteraction`**
* * **`ButtonInteraction`**
* * **`StageInstance`**
* * **`SelectMenuInteraction`**
* @typedef {string} ExtendableStructure
*/
/**
* Allows for the extension of built-in Discord.js structures that are instantiated by {@link BaseManager Managers}.
*/
class Structures extends null {
/**
* 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'),
StageChannel: require('../structures/StageChannel'),
ThreadChannel: require('../structures/ThreadChannel'),
GuildMember: require('../structures/GuildMember'),
ThreadMember: require('../structures/ThreadMember'),
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'),
CommandInteraction: require('../structures/CommandInteraction'),
ButtonInteraction: require('../structures/ButtonInteraction'),
SelectMenuInteraction: require('../structures/SelectMenuInteraction'),
StageInstance: require('../structures/StageInstance'),
};
module.exports = Structures;

37
typings/index.d.ts vendored
View File

@@ -1888,20 +1888,6 @@ declare module 'discord.js' {
public type: 'store';
}
export class Structures extends null {
private constructor();
public static get<K extends keyof Extendable>(structure: K): Extendable[K];
public static get(structure: string): (...args: any[]) => void;
public static extend<K extends keyof Extendable, T extends Extendable[K]>(
structure: K,
extender: (baseClass: Extendable[K]) => T,
): T;
public static extend<T extends (...args: any[]) => void>(
structure: string,
extender: (baseClass: typeof Function) => T,
): T;
}
export class SystemChannelFlags extends BitField<SystemChannelFlagsString> {
public static FLAGS: Record<SystemChannelFlagsString, number>;
public static resolve(bit?: BitFieldResolvable<SystemChannelFlagsString, number>): number;
@@ -3326,29 +3312,6 @@ declare module 'discord.js' {
type ExplicitContentFilterLevel = keyof typeof ExplicitContentFilterLevels;
interface Extendable {
GuildEmoji: typeof GuildEmoji;
DMChannel: typeof DMChannel;
TextChannel: typeof TextChannel;
VoiceChannel: typeof VoiceChannel;
CategoryChannel: typeof CategoryChannel;
NewsChannel: typeof NewsChannel;
StoreChannel: typeof StoreChannel;
ThreadChannel: typeof ThreadChannel;
GuildMember: typeof GuildMember;
ThreadMember: typeof ThreadMember;
Guild: typeof Guild;
Message: typeof Message;
MessageReaction: typeof MessageReaction;
Presence: typeof Presence;
VoiceState: typeof VoiceState;
Role: typeof Role;
User: typeof User;
CommandInteraction: typeof CommandInteraction;
ButtonInteraction: typeof ButtonInteraction;
SelectMenuInteraction: typeof SelectMenuInteraction;
}
interface FetchApplicationCommandOptions extends BaseFetchOptions {
guildID?: Snowflake;
}