UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

97 lines (95 loc) 4.42 kB
import { __name } from '../chunk-PAWJFY3S.mjs'; import { container, Store } from '@sapphire/pieces'; import { Client } from 'discord.js'; import { loadApplicationCommandRegistriesListeners } from '../optional-listeners/application-command-registries-listeners/_load.mjs'; import { loadErrorListeners } from '../optional-listeners/error-listeners/_load.mjs'; import { loadMessageCommandListeners } from '../optional-listeners/message-command-listeners/_load.mjs'; import { PluginManager } from './plugins/PluginManager.mjs'; import { ArgumentStore } from './structures/ArgumentStore.mjs'; import { CommandStore } from './structures/CommandStore.mjs'; import { InteractionHandlerStore } from './structures/InteractionHandlerStore.mjs'; import { ListenerStore } from './structures/ListenerStore.mjs'; import { PreconditionStore } from './structures/PreconditionStore.mjs'; import { PluginHook } from './types/Enums.mjs'; import { Events } from './types/Events.mjs'; import { acquire } from './utils/application-commands/ApplicationCommandRegistries.mjs'; import { LogLevel } from './utils/logger/ILogger.mjs'; import { Logger } from './utils/logger/Logger.mjs'; import '../arguments/_load.mjs'; import '../listeners/_load.mjs'; import '../preconditions/_load.mjs'; container.applicationCommandRegistries = { acquire }; var _SapphireClient = class _SapphireClient extends Client { constructor(options) { super(options); /** * The client's ID, used for the user prefix. * @since 1.0.0 */ this.id = null; container.client = this; for (const plugin of _SapphireClient.plugins.values(PluginHook.PreGenericsInitialization)) { plugin.hook.call(this, options); this.emit(Events.PluginLoaded, plugin.type, plugin.name); } this.logger = options.logger?.instance ?? new Logger(options.logger?.level ?? LogLevel.Info); container.logger = this.logger; if (options.enableLoaderTraceLoggings ?? container.logger.has(LogLevel.Trace)) { Store.logger = container.logger.trace.bind(container.logger); } this.stores = container.stores; this.fetchPrefix = options.fetchPrefix ?? (() => this.options.defaultPrefix ?? null); this.disableMentionPrefix = options.disableMentionPrefix; for (const plugin of _SapphireClient.plugins.values(PluginHook.PreInitialization)) { plugin.hook.call(this, options); this.emit(Events.PluginLoaded, plugin.type, plugin.name); } this.id = options.id ?? null; this.stores.register(new ArgumentStore()).register(new CommandStore()).register(new InteractionHandlerStore()).register(new ListenerStore()).register(new PreconditionStore()); if (options.loadApplicationCommandRegistriesStatusListeners !== false) { loadApplicationCommandRegistriesListeners(); } if (options.loadDefaultErrorListeners !== false) { loadErrorListeners(); } if (options.loadMessageCommandListeners === true) { loadMessageCommandListeners(); } for (const plugin of _SapphireClient.plugins.values(PluginHook.PostInitialization)) { plugin.hook.call(this, options); this.emit(Events.PluginLoaded, plugin.type, plugin.name); } } /** * Loads all pieces, then logs the client in, establishing a websocket connection to Discord. * @since 1.0.0 * @param token Token of the account to log in with. * @return Token of the account used. */ async login(token) { if (this.options.baseUserDirectory !== null) { this.stores.registerPath(this.options.baseUserDirectory); } for (const plugin of _SapphireClient.plugins.values(PluginHook.PreLogin)) { await plugin.hook.call(this, this.options); this.emit(Events.PluginLoaded, plugin.type, plugin.name); } await Promise.all([...this.stores.values()].map((store) => store.loadAll())); const login = await super.login(token); for (const plugin of _SapphireClient.plugins.values(PluginHook.PostLogin)) { await plugin.hook.call(this, this.options); this.emit(Events.PluginLoaded, plugin.type, plugin.name); } return login; } static use(plugin) { this.plugins.use(plugin); return this; } }; __name(_SapphireClient, "SapphireClient"); _SapphireClient.plugins = new PluginManager(); var SapphireClient = _SapphireClient; export { SapphireClient }; //# sourceMappingURL=SapphireClient.mjs.map //# sourceMappingURL=SapphireClient.mjs.map