npm package: @scorpion-kitty/core
Description: Scorpion Kitty discord bot framework
Author: JenTay
Latest version: 1.3.2

# Scorpion Kitty Core

[![npm version](https://badge.fury.io/js/%40scorpion-kitty%2Fcore.svg)](https://badge.fury.io/js/%40scorpion-kitty%2Fcore)
[![build status](https://gitlab.com/scorpion-kitty/scorpion-kitty-core/badges/develop/pipeline.svg)](https://gitlab.com/scorpion-kitty/scorpion-kitty-core/-/pipelines?page=1&scope=all&ref=develop)

This package contains the brain of the Scorpion Kitty Discord bot as it has the complexity of handling and registering the commands abstracted away in this internal framework.

The way this framework is supposed to be used and feel like is greatly inspired by [Angular](https://angular.io/) and [Nest.js](https://nestjs.com/).

This way to see a [more detailed documentation.](./docs/decorators.md)

# Usage

## Services

A simple service with one method looks like this:

```typescript
// main.service.ts
import { Injectable } from '@scorpion-kitty/core';

@Injectable()
export class MainService {
	async pingAsync(param: string): Promise<string> {
		return param;
	}
}
```

## Controllers

A controller with one command looks like this:

```typescript
// main.controller.ts
import {
	BotController,
	BotCommand,
	HasServerPermission,
	IsBotOwner,
	Message
} from '@scorpion-kitty/core';

import { MainService } from './main.service';

@BotController()
export class MainController {
	// The constructor will be called from the framework and filled with provided services,
	// so they are ready to use inside the controller
	constructor(private mainService: MainService) {}

	// This method will be called, when a messages with the first word '?test' or '?t' will be received
	// and only, if the author has the MANAGE_MESSAGES permission
	@BotCommand('test :param :text', { aliases: ['t'] })
	@HasServerPermission(
		['MANAGE_MESSAGES'],
		`You can't just go around and test stuff`
	)
	async command(
		@Msg() msg: Message,
		@Param('param') param: string,
		@Param('text') someText: string
	) {
		await msg.reply('I got your test-command');
		const result = await this.mainService.pingAsync('Hi');
		await msg.channel.send(
			`${param}, ${text} and the async service says ${result}`
		);
	}

	// This command will only be allowed to be used by the discord user who owns the bot
	@BotCommand('owner')
	@IsBotOwner('Do not touch me')
	async ownerCommand(@Msg() msg: Message) {
		await msg.reply('yes?');
	}
}
```

This will create a controller that contains a command registered as `?test` and `?t`.

## Modules

To register controllers, so the bot can start handling them, you need to create modules.

```typescript
// main.module.ts
import { BotModule } from '@scorpion-kitty/core';
import { MainService } from './main.service';
import { MainController } from './main.controller';

@BotModule({
	imports: [],
	providers: [MainService],
	controllers: [MainController]
})
export class MainModule {}
```

## Register the root module

One module is needed as root for the bot to be created. Furthermore, the bots Discord Bot Client Token from a registered Discord Application is needed to start the bot.

```typescript
// main.ts
import { ScorpionKittyFactory } from '@scorpion-kitty/core';

const token = process.env.TOKEN;

const bot = ScorpionKittyFactory.create(MainModule);
bot.login(token, { customPrefix: '!', logLevel: 'trace' });
```