Skip to content

How to Write a Plugin

It happens that you are missing something... And plugins can help you with this!

Example

index.ts
ts
import { Plugin, Bot } from "gramio";

export class PluginError extends Error {
    wow: "type" | "safe" = "type";
}

const plugin = new Plugin("gramio-example")
    .error("PLUGIN", PluginError)
    .derive(() => {
        return {
            some: ["derived", "props"] as const,
        };
    });

const bot = new Bot(process.env.BOT_TOKEN as string)
    .extend(plugin)
    .onError(({ context, kind, error }) => {
        if (context.is("message") && kind === "PLUGIN") {
            console.log(error.wow);
        }
    })
    .use((context) => {
        console.log(context.some);
    });

Scaffolding the plugin

This command will help you create a plugin with GramIO the easiest way.

sh
npm create gramio-plugin ./plugin
sh
yarn create gramio-plugin ./plugin
sh
pnpm create gramio-plugin ./plugin
sh
bun create gramio-plugin ./plugin

Supported Environment

Linters

Storage

Others