xzeldon / vk-di

πŸ€– A TypeScript wrapper around vk-io with Dependency Injection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

vk-di

πŸ€– A TypeScript wrapper around vk-io

This repository is an example of a runtime dependency injection implementation, metadata reflection and the service provider pattern in TypeScript.

  • Decorators for declarative syntax πŸ›‹
  • Runtime Dependency injection support πŸ’‰
  • Modular structure for better organizaed projects πŸ—

Getting Started

Install dependencies

npm i

or

yarn install

Getting started

index.ts (entry point)

Your index.ts is the main entry point of your application. Your application consists of at least one "MainModule" and as many other sub-class modules as you want. You define them all here for runtime instantiation.

const app = new App({
    modules: [
        MainModule
    ],
    on_start() {
        console.log('Bot is up and running');
    }
});

app.launch();

main.module.ts

Your main modules's primary responsibility should be authenticating your bot. Pass a Client instance with Options object that contains the token field to the constructor and then call start() on the this.client property. The main module might also be a good place to add global on state change events.

import { MessageContext } from "vk-io";
import { Client, Module } from "@core/*";

export default class MainModule extends Module {
    constructor() {
        super(new Client({
            token: process.env.TOKEN
        }));

        this.client.start();
    }

    /**
     * The bot will be trigger to message '/ping'.
     * You can also use regular expressions instead of
     * strings.
     */

    @Command('ping', { prefix: '/' })
    on_ping(context: MessageContext) {
        context.send('pong!');
    }
}

An example of a fully working bot can be found in simple-bot package.

Inspired by

About

πŸ€– A TypeScript wrapper around vk-io with Dependency Injection

License:MIT License


Languages

Language:TypeScript 92.0%Language:JavaScript 7.6%Language:Shell 0.4%