discordial
v1.0.5
Published
A Discord framework using IoC architecture.
Downloads
8
Readme
Discordial
Discordial is a framework for building Discord bots. It makes use of the advantages of TypeScript and combines elements of OOP (Object Oriented Programming) and FP (Functional Programming).
Discordial is built uisin the IoC (Inversion of Control) design pattern, and focus on modularity and ease of use. Adding functionality to your bot is as easy as downloading a packaging and writing a single line of code.
Example
Discordial works by registering plugins during your Discordial configuration. Therefore, adding more functionality is as easy as registering more plugins.
import { Discordial } from "discordial";
import { PrefixPlugin } from "./prefix/prefix.plugin";
const token = "your discord bot token";
const discordial = new Discordial(token, {
plugins: [PrefixPlugin] // <= PrefixPlugin is now registered.
});
Now let's take a look at what PrefixPlugin
looks like. A Discordial Plugin is the controller which maps discord events to functions using the @BindEvent()
decorator. A Discordial Plugin takes the @DiscordialPlugin()
decorator at the top.
// src/prefix/prefix.plugin.ts
import { Message } from "discord.js";
import { DiscordialPlugin, BindEvent, DiscordEvents, Inject, Message, SyntheticMessage } from "discordial";
import { PrefixService } from "./prefix.service";
@DiscordialPlugin()
export class PrefixPlugin {
constructor(
// Discordial will instantiate and manage your services for you.
@Inject(PrefixService) private readonly prefixService: PrefixService,
) {}
// `replyWithPong()` will be called on every new message.
@BindEvent(DiscordEvents.MESSAGE)
public replyWithPong(
// You can inject synthetic objects with the help of decorators:
@Message() syntheticMessage: SyntheticMessage,
// Or simply get the default parameters from the listener:
message: Message,
) {
this.prefixService.replyWithPong(syntheticMessage);
}
}
Services can be used to separate concerns and create scalable applications. To declare a service, use the @Injectable()
decorator. Let's take a look at our PrefixService
.
import { Injectable, SyntheticMessage } from "discordial";
@Injectable()
export class PrefixService {
private readonly _prefix = "!";
private isValid(message: SyntheticMessage, command: string): boolean {
if (!message.content.startsWith(this._prefix + command))
return false;
return !message.author.bot;
}
public replyWithPong(message: SyntheticMessage): void {
if (!this.isValid(message, "ping"))
return;
message.reply("pong");
}
}
Features
Built using the IoC design pattern.
Shares similarities with AngularJS and NestJS. If you have experience with any of those frameworks, Discordial becomes that much easier to use.
Synthetic objects: Discordial offers custom interfaces for manipulating the event parameters with improved functionality. Synthetic objects can also be replaced by awesome community made creations. ~~Well, not yet... but that's the plan! :)~~