1.1.12 • Published 2 years ago

@cordwork/core v1.1.12

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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();

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');
  }
}
1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago