A progressive Node.js framework for building efficient and scalable Discord.js applications.
Description
Cordwork is a framework for building Discord bots like NestJS. It will help make the project more cohesive and massive.
Getting Started
Install
$ npm install @cordwork/core
tsconfig.json
...
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
...
main.ts
import { CordWork } from '@cordwork/core';
import { App } from './app.module';
async function bootstrap() {
const client = await new CordWork()
.SetToken(process.env.BOT_TOKEN || '')
.AttachApp(App)
.Launch();
}
bootstrap();
- To check out the example.
Module
Cordwork is a collection of modules. The program starts from the top-level module. Can add modules to modules.
app.module.ts
import { Module } from '@cordwork/core';
@Module({
imports?: Type<any>[],
events?: Type<any>[],
commands?: Type<any>[],
guilds?: Guild[],
components?: Type<any>[],
extensions?: Type<any>[],
})
export class App {
}
Controller
Controller is an object that manages message components(Button, Slect Menus, etc...), event handlers, and slash commands.
Slash Commands
import { DiscordCommand } from '@coredwork/core';
@DiscordCommand({
name: 'ping',
description: 'The test command.',
guilds: ['my guild name'],
})
export class CommandController {
async listener(interaction): Promise<void> {
interaction.reply('Pong!');
}
Event Handler
import { Inject, DiscordEvent, CordWorkClient } from '@cordwork/core';
@DiscordEvent('ready')
export class ReadyController {
constructor(
@Inject(CordWorkClient) private client: CordWorkClient
) {}
async listener(): Promise<void> {
console.log(`Logged in as ${this.client?.user?.tag}!`);
const application = this.client.application;
if ( application ) {
console.log('client id', application.id);
}
}
}
Message Component
import { MessageButton } from 'discord.js';
import { Inject, DiscordComponent, CordWorkClient } from '@cordwork/core';
@DiscordComponent()
export class ComponentClass {
constructor(
@Inject(CordWorkClient) private client: CordWorkClient,
) {}
create(): MessageButton {
return new MessageButton()
.setCustomId('mybutton')
.setLabel(`LABEL ${this.client?.user?.tag}`)
.setStyle('PRIMARY');
}
listener(interaction): void {
interaction.reply('button click');
}
}