UNPKG

@gramio/autoload

Version:

Autoload commands plugin for GramIO

84 lines (80 loc) 2.4 kB
import { Options } from 'fdir'; import { Plugin } from 'gramio'; import { PicomatchOptions } from 'picomatch'; type SoftString<T extends string> = T | (string & {}); /** Params that used in {@link AutoloadOptions.onLoad | onLoad} and {@link AutoloadOptions.onFinish | onFinish} hooks */ interface AutoloadOptionsPathParams { absolute: string; relative: string; } /** Options for {@link autoload} plugin with options for {@link Options | fdir} and {@link PicomatchOptions | picomatch}*/ interface AutoloadOptions { /** Configure `fdir` options */ fdir?: Options; /** Configure `picomatch` options */ picomatch?: PicomatchOptions; /** * import a specific `export` from a file * @example import first export * ```ts * import: (file) => Object.keys(file).at(0) || "default", * ``` * @default "default" */ import?: SoftString<"default"> | ((file: any) => string); /** * [Glob patterns](<https://en.wikipedia.org/wiki/Glob_(programming)>) * @default "**\/*.{ts,js,cjs,mjs}" * */ patterns?: string | string[]; /** * Throws an error if no matches are found. * @default true */ failGlob?: boolean; /** * Skip imports where needed `export` not defined * @default false */ skipImportErrors?: boolean; /** * The path to the folder * @default "./commands" * */ path?: string; /** The hook that is called when loading a file */ onLoad?: (path: AutoloadOptionsPathParams) => unknown; /** The hook that is called after loading all files */ onFinish?: (paths: AutoloadOptionsPathParams[]) => unknown; } /** * Autoload commands plugin for GramIO. * @example * ## Register the plugin * * ```ts * // index.ts * import { Bot } from "gramio"; * import { autoload } from "@gramio/autoload"; * * const bot = new Bot(process.env.TOKEN as string) * .extend(autoload()) * .onStart(console.log); * * bot.start(); * * export type BotType = typeof bot; * ``` * * ## Create command * * ```ts * // commands/command.ts * import type { BotType } from ".."; * * export default (bot: BotType) => * bot.command("start", (context) => context.send("hello!")); * ``` */ declare function autoload(options?: AutoloadOptions): Promise<Plugin>; export { type AutoloadOptions, type AutoloadOptionsPathParams, autoload };