Splitting Code

Larger projects with clients that include a lot of handlers assigned might need to split the handler codes into different files.

This can be done levaraging the Composer class.

Consider that your project’s structure is:

handlers/
    start.ts
    hello.ts
main.ts

In each file that resides in handlers/ you could create and export an instance of Composer:

File: handlers/start.ts

import { Composer } from "https://deno.land/x/mtkruto@0.1.800/mod.ts";

const composer = new Composer();

export default composer;

composer.command("start", async (ctx) => {
    await ctx.reply("Hey, you just started me!");
});

File: handlers/hello.ts

import { Composer } from "https://deno.land/x/mtkruto@0.1.800/mod.ts";

const composer = new Composer();

export default composer;

composer.command("hello", async (ctx) => {
    await ctx.reply("Hello, world!");
});

Then, you can assign their handlers to a client instance:

File: main.ts

/* ... */
import start from "./handlers/start.ts";
import hello from "./handlers/hello.ts";

const client = new Client(/* ... */);

client.use(start);
client.use(hello);

/* ... */

If you wanted to access the client instance inside the handlers for whatever reason, it is available in the context object, so you can access it like ctx.client.