Skip to content
Recipes

Hot Reload

During development, Seyfert allows you to reload commands, events, components, and language files without restarting the bot.

This recipe reloads handler files inside a running process. To replace whole workers with freshly loaded code in production, see Rolling worker hot reload.

Reloading Commands

// Reload all commands
await client.commands.reloadAll();

// Reload a specific command by name
await client.commands.reload('ping');

From Seyfert 6.0.0, a reload reads the file through onFile, the same hook load() uses. Every command the file exports is replaced in place, so a file that exports several commands gets all of them back. A file that fails to import, or that no longer exports a command, keeps its previous commands.

The directory of an @AutoLoad command is not scanned again, so a reloaded parent command comes back without its auto-loaded subcommands.

Reloading Events

await client.events.reloadAll();

Reloading Components

await client.components.reloadAll();

Components and modals follow the same per-file rules as commands.

Reload Reports

reloadAll() resolves without a value. From Seyfert 6.0.0, commands.reloadFiles() and components.reloadFiles() run the same reload and return what happened to each file:

const { reloaded, failed } = await client.commands.reloadFiles(false);

client.logger.info(`Reloaded ${reloaded.length} command file(s)`);

for (const { path, error } of failed) {
    client.logger.warn('Kept the previous commands for', path, error);
}

Both methods take a stopIfFail argument, which defaults to true:

stopIfFailA file fails to import or exports nothing loadable
trueThe reload stops at that file and throws RELOAD_FAILED. The report is in error.metadata and the original error in error.cause.
falseThe file is added to failed, a warning is logged, and the reload continues with the next file.

In both cases the failed file keeps the handlers it had before.

try {
    await client.components.reloadAll();
} catch (error) {
    if (!SeyfertError.is(error, 'RELOAD_FAILED')) throw error;

    const { failed } = error.metadata as unknown as HandlerReloadResult;
    client.logger.error('Reload stopped at', failed[0]?.path, error.cause);
}

Reloading Language Files

// Reload all languages
await client.langs.reloadAll();

// Reload a specific locale
await client.langs.reload('es');

Reload Command

A practical pattern is to create a developer-only command for hot reloading:

import {
    Command,
    Declare,
    Options,
    createStringOption,
    type CommandContext,
} from 'seyfert';

const options = {
    target: createStringOption({
        description: 'What to reload',
        required: true,
        choices: [
            { name: 'Commands', value: 'commands' },
            { name: 'Events', value: 'events' },
            { name: 'Components', value: 'components' },
            { name: 'Languages', value: 'langs' },
            { name: 'All', value: 'all' },
        ] as const,
    }),
};

@Declare({
    name: 'reload',
    description: 'Reload bot modules',
})
@Options(options)
export default class ReloadCommand extends Command {
    async run(ctx: CommandContext<typeof options>) {
        const target = ctx.options.target;
        const failed: string[] = [];

        if (target === 'all' || target === 'commands') {
            const report = await ctx.client.commands.reloadFiles(false);
            failed.push(...report.failed.map(file => file.path));
        }
        if (target === 'all' || target === 'events') await ctx.client.events?.reloadAll();
        if (target === 'all' || target === 'components') {
            const report = await ctx.client.components.reloadFiles(false);
            failed.push(...report.failed.map(file => file.path));
        }
        if (target === 'all' || target === 'langs') await ctx.client.langs?.reloadAll();

        await ctx.editOrReply({
            content: failed.length
                ? `Reloaded **${target}**, but ${failed.length} file(s) kept their previous handlers.`
                : `Reloaded **${target}** successfully.`,
        });
    }
}

reloadFiles(false) keeps going when a file is broken, so one bad edit does not stop the rest of the reload. The paths in failed are also logged by the handler.

Automatic Reload with @slipher/watcher

Manually restarting after every change gets old fast. @slipher/watcher keeps the gateway alive while it watches your source files, rebuilds on change, and restarts the worker.

pnpm add -D @slipher/watcher
src/watcher.ts
import { join } from 'node:path';
import { Watcher } from '@slipher/watcher';

const watcher = new Watcher({
    // This is the compiled WorkerClient entry, not the watcher entry below.
    filePath: join(process.cwd(), 'dist', 'index.js'),
    srcPath: join(process.cwd(), 'src'),
    transpileCommand: 'tsc --project tsconfig.json',
});

await watcher.spawnShards();
package.json
{
    "scripts": {
        "watch": "pnpm build && node dist/watcher.js"
    }
}
pnpm watch

Watcher is a library entry point, not a CLI. It extends Seyfert's ShardManager: the watcher process owns the gateway connection, forwards payloads to the compiled worker entry, and replaces that worker after any event reported by Chokidar under srcPath.

transpileCommand is optional and runs synchronously from the current working directory before each replacement. The package logs a build failure but still attempts to start filePath, so keep the initial pnpm build in the script and make sure failed builds cannot leave a partial output in that location.