Skip to content

Syth-1/command-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

command-loader

Command-Loader is an agnostic bot library, designed to make creating text bots easier, be it for games or other services. Command-Loader will automatically handle the number of arguments required by your commands and convert the input to the necessary types.

Installation

Note

For ideal installation in your project, it is recommended to use submodules.

  • Installing a new submodule:
    git submodule add https://github.com/Syth-1/command-loader.git
    git submodule update --init

    # updating existing submodules to newest commit:
    git submodule update --remote
  • Installing dependencies & running the example:
    cd command-loader
    bun install
    bun run example/app.ts

Features

  • Auto args handling
  • Reloading
  • Subcommands

Usage/Examples

Ensure the following is added to tsconfig.json:

"experimentalDecorators":  true,
"emitDecoratorMetadata": true,

main script:

import { CommandProcessor, BaseGlobals, BaseContext } from './command-loader/bot/process_command'
import { readdir } from 'node:fs/promises'
import path from 'path'

const moduleFolder = "modules"
const prefix = '/'

async function getModuleFiles(folder : string) {
    return (await readdir(`./${folder}`))
        .map(file => import.meta.resolve(`./${path.join(folder, file)}`)
    )
}

async function main() {
    const commandProcessor = new CommandProcessor(
        BaseContext,
        BaseGlobals
    )

    await commandProcessor.moduleLoader.scheduleEvent(
        "load", 
        await getModuleFiles(moduleFolder), 
        error => {
            if (error.length > 0)
                console.log(error)
            else
                console.log("loaded files!")
        }
    )

    for await (const line of console)
        commandProcessor.processCommands(prefix, line)

}

main()

within modules/script_module.ts:

import { Commands, Listener } from "../command-loader/bot/commands"
import { type BaseContext as Context } from '../command-loader/bot/process_command'

export class Module {

    @Commands.command()
    async hello(ctx : Context) {
        console.log("hello world")
    }

    @Commands.command()
    async add(ctx : Context, num1 : number, num2 : number) { 
        console.log(num1 + num2)
    }

    @Listener.error
    async globalError(err : Error) { 
        console.log("an error occured!", err.message)
    }
}

About

A bot library written in Typescript using bun

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published