Skip to content

gramiojs/autoload

Repository files navigation

@gramio/autoload

npm JSR JSR Score

Autoload commands plugin for GramIO with Bun.build support.

Usage

full example

Important

Please read about Lazy-load plugins

Register the plugin

// 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

// 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 Options to configure fdir
picomatch? PicomatchOptions Options to configure picomatch

Bun build usage

You can use this plugin with Bun.build, thanks to esbuild-plugin-autoload!

// @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.

You can bundle and then compile it into a single executable binary file

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)

Read more