Skip to content

Use command registry instead of switch statement. #4

@abic

Description

@abic

Currently src/index.ts has a big switch statement for known commands. This won't scale as additional commands are added and command requests could come from different sources (e.g. Facebook messenger bots, web interface, etc).

The pattern here should be that we define a Command interface that different commands can implement and provide a registry function to add that command to the CommandRegistry. The following is a very rough interface and example.

interface McCommand {
    run(args: string[]): Promise<void>;
}

class EchoCommand implements McCommand {
    constructor(bot: mineflayer.Bot, mcmanus: McManus) {
        ...
    }

    run(args: string[]): Promise<void> {
        this.bot.chat(args.join(" "));
        return new Promise();
    }
}

registryCommand(EchoCommand);

We should support dependency injection so the right dependencies get passed to the constructor of the commands and we can support per command configs, client interfaces such as a sqlite connection, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions