1.0.15 • Published 4 years ago

shori v1.0.15

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

Shori

Simple Typescript discord.js framework providing decorators and DI


Usage

Start by extending the ShoriClient, like this:

class ExampleClient extends ShoriClient {

  constructor(shoriOptions: ShoriOptions, clientOptions: ClientOptions) {
    super(shoriOptions, clientOptions);
  }

  public getPrefix(id: string): Promise<string> {
    // should make a database request here
    // returning undefined will make it use the default prefix
    return undefined;
  }
  
}

Commands

Commands have to extend the CommandBase class and have the Command Decorator

@Command('test')
class TestCommand extends CommandBase {

  // shori provide dependency injection with tsyringe
  // on how to provide a service, read down below in the Service chapter
  constructor(private fileService: FileService) {super();}

  public async exec(message: Message, args: string[]): Promise<any> {
    message.reply( `hi, this works. My Prefix: ${this.client.prefix}` );
  }

}

Services

A Service is a class that provides methods and actions for your project, for example a class that provides methods to interact with the filesystem, you dont want that logic inside your commands.

@Service()
export class FileService {

  public async init(): Promise<void> {
    // run things that your service needs to fully work,
    // this method will be called and awaited before the client starts
    // e.g. creating database connections, ensuring folders etc...
    await this.doStuff();
  }
}

The @Service() decorator will make the class a singleton, and thus can be injected into commands (or any other knows class in the shori context)

Its important to know that every Service must be provided in the service array in the shori client options.

shori will then call every services init method and wait for its Promise to resolve before the client starts the connection to discord

Events

Events simply have to be decorated with an Event Decorator

import {Client, Event} from 'shori';

export class SomeClass {

  constructor(
    // shori will inject the client  
    @Client() private client: ExampleClient
  ) {}

  @Event('ready')
  ready(): void {
    console.log('client is ready');
    console.log(this.client.channels.size);
  }

}

Warning: \ shori will register every class you use @Command , @Event or @Service on as a singleton, do not create instances yourself, but use DI or container.resolve from tsyringe

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.11

4 years ago

1.0.12

4 years ago

1.0.9

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago