https://github.com/gramiojs/autoload
Autoload commands plugin for GramIO
https://github.com/gramiojs/autoload
autoload gramio gramio-plugin telegram telegram-api telegram-bot-api
Last synced: about 1 year ago
JSON representation
Autoload commands plugin for GramIO
- Host: GitHub
- URL: https://github.com/gramiojs/autoload
- Owner: gramiojs
- License: mit
- Created: 2024-03-10T20:26:32.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-01T19:09:34.000Z (over 1 year ago)
- Last Synced: 2025-03-25T06:36:33.217Z (about 1 year ago)
- Topics: autoload, gramio, gramio-plugin, telegram, telegram-api, telegram-bot-api
- Language: TypeScript
- Homepage: https://gramio.netlify.app/plugins/official/autoload.html
- Size: 108 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @gramio/autoload
[](https://www.npmjs.org/package/@gramio/autoload)
[](https://jsr.io/@gramio/autoload)
[](https://jsr.io/@gramio/autoload)
Autoload commands plugin for GramIO with [`Bun.build`](#bun-build-usage) support.
## Usage
> [full example](https://github.com/gramiojs/autoload/tree/main/example)
> [!IMPORTANT]
> Please read about [Lazy-load plugins](https://gramio.dev/plugins/official/autoload.html)
## 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(await 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!"));
```
## Options
| Key | Type | Default | Description |
| ----------------- | -------------------------------------------------------------------------------------------------- | -------------------------- | ------------------------------------------------------------------------- |
| pattern? | string \| string[] | "\*\*\/\*.{ts,js,cjs,mjs}" | [Glob patterns]() |
| path? | string | "./commands" | Path to the folder |
| import? | string \| (file: any) => string | "default" | Import a specific `export` from a file |
| failGlob? | boolean | true | Throws an error if no matches are found |
| skipImportErrors? | boolean | false | Skip imports where needed `export` not defined |
| onLoad? | (params: { absolute: string; relative: string }) => unknown | | Hook that is called when loading a file |
| onFinish? | (paths: { absolute: string; relative: string }[]) => unknown; | | Hook that is called after loading all files |
| fdir? | [Options](https://github.com/thecodrr/fdir/blob/HEAD/documentation.md#method-chaining-alternative) | | Options to configure [fdir](https://github.com/thecodrr/fdir) |
| picomatch? | [PicomatchOptions](https://github.com/micromatch/picomatch?tab=readme-ov-file#picomatch-options) | | Options to configure [picomatch](https://www.npmjs.com/package/picomatch) |
### [Bun build](https://bun.sh/docs/bundler) usage
You can use this plugin with [`Bun.build`](https://bun.sh/docs/bundler), thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!
```ts
// @filename: build.ts
import { autoload } from "esbuild-plugin-autoload"; // default import also supported
await Bun.build({
entrypoints: ["src/index.ts"],
target: "bun",
outdir: "out",
plugins: [autoload("./src/commands")],
}).then(console.log);
```
Then, build it with `bun build.ts` and run with `bun out/index.ts`.
### [Bun compile](https://bun.sh/docs/bundler/executables) usage
You can bundle and then compile it into a [single executable binary file](https://bun.sh/docs/bundler/executables)
```ts
import { autoload } from "esbuild-plugin-autoload"; // default import also supported
await Bun.build({
entrypoints: ["src/index.ts"],
target: "bun",
outdir: "out",
plugins: [autoload("./src/commands")],
}).then(console.log);
await Bun.$`bun build --compile out/index.js`;
```
> [!WARNING]
> You cannot use it in `bun build --compile` mode without extra step ([Feature issue](https://github.com/oven-sh/bun/issues/11895))
[Read more](https://github.com/kravetsone/esbuild-plugin-autoload)