UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

126 lines (124 loc) 5.47 kB
import { PluginHook } from "./types/Enums.mjs"; import { Events as Events$1 } from "./types/Events.mjs"; import { acquire } from "./utils/application-commands/ApplicationCommandRegistries.mjs"; import { PluginManager } from "./plugins/PluginManager.mjs"; 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 { 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 { 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"; import { Store, StoreRegistry, container } from "@sapphire/pieces"; import { Client } from "discord.js"; //#region src/lib/SapphireClient.ts container.applicationCommandRegistries = { acquire }; /** * The base {@link Client} extension that makes Sapphire work. When building a Discord bot with the framework, the developer * must either use this class, or extend it. * * Sapphire also automatically detects the folders to scan for pieces, please read {@link StoreRegistry.registerPath} * for reference. This method is called at the start of the {@link SapphireClient.login} method. * * @see {@link SapphireClientOptions} for all options available to the Sapphire Client. You can also provide all of discord.js' [ClientOptions](https://discord.js.org/docs/packages/discord.js/main/ClientOptions:Interface) * * @since 1.0.0 * @example * ```typescript * const client = new SapphireClient({ * presence: { * activity: { * name: 'for commands!', * type: 'LISTENING' * } * } * }); * * client.login(process.env.DISCORD_TOKEN) * .catch(console.error); * ``` * * @example * ```typescript * // Automatically scan from a specific directory, e.g. the main * // file is at `/home/me/bot/index.js` and all your pieces are at * // `/home/me/bot/pieces` (e.g. `/home/me/bot/pieces/commands/MyCommand.js`): * const client = new SapphireClient({ * baseUserDirectory: join(__dirname, 'pieces'), * // More options... * }); * ``` * * @example * ```typescript * // Opt-out automatic scanning: * const client = new SapphireClient({ * baseUserDirectory: null, * // More options... * }); * ``` */ var SapphireClient = class SapphireClient extends Client { constructor(options) { super(options); this.id = null; container.client = this; for (const plugin of SapphireClient.plugins.values(PluginHook.PreGenericsInitialization)) { plugin.hook.call(this, options); this.emit(Events$1.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$1.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$1.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$1.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$1.PluginLoaded, plugin.type, plugin.name); } return login; } static use(plugin) { this.plugins.use(plugin); return this; } }; SapphireClient.plugins = new PluginManager(); //#endregion export { SapphireClient }; //# sourceMappingURL=SapphireClient.mjs.map