1.8.1 • Published 3 years ago

@mich4l/discord-base v1.8.1

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

NOT MANTAINED! Logo

Discord bot TypeScript base

Simple TypeScript abstraction for creating Discord bots (using discord.js and typedi)

Installation

Important: Node.js 16.6.0 or newer is required.

npm i @mich4l/discord-base

Concepts

concepts

Types of objects:

  • Command Handler - it's giving you access to Message object and array of command arguments (strings)
  • Event Handler - it's giving you access to object from event listener (for example: Message, GuildEmoji, GuildChannel...)
  • Service - you can use it for busisness logic of your bot

Code example

index.ts

import { DiscordBot } from '@mich4l/discord-base';
import { ExampleCommand } from './commands/exampleCommand';
import { token } from './config.json'

const discordBot = new DiscordBot({
    token,
    activity: 'CS:GO',
});

discordBot.addCommand('example', ExampleCommand);

exampleCommand.ts

import { Handler, CommandHandler } from '@mich4l/discord-base';
import { Message } from 'discord.js';
import { ExampleService } from '../services/exampleService';

@Handler()
export class ExampleCommand implements CommandHandler {
    constructor(
        private readonly exampleService: ExampleService
    ) {}

    handle(msg: Message, args: string[]) {
        const message = this.exampleService.getMessage();

        msg.reply(message);
    }
}

exampleService.ts

import { Service } from '@mich4l/discord-base';

@Service()
export class ExampleService {
    getMessage() {
        return 'Hello world';
    }
}

Events

You may want react not only to commands - there is also Events concept

messageDeleted.ts:

import { EventHandler, Handler } from '@mich4l/discord-base';
import { ExampleService } from '../services/exampleService';

@Handler()
class MessageDeletedEvent implements EventHandler {
    constructor(
        private readonly exampleService: ExampleService
    ) {}

    handle(msg: Message) {
        const message = this.exampleService.getMessage();
        msg.channel.send(message);
    }
}

index.ts:

// some code...
discordBot.addEvent('messageDelete', MessageDeletedEvent)

React to every message that is not command:

@Handler()
class MessageDeletedEvent implements EventHandler {
    constructor(
        private readonly exampleService: ExampleService
    ) {}

    handle(msg: Message) {
        const message = this.exampleService.getMessage();
        msg.channel.send(message);
    }
}

discordBot.addAnyMessageHandler(MessageDeletedEvent)

or react to literally every message (commands too):

@Handler()
class MessageDeletedEvent implements EventHandler {
    constructor(
        private readonly exampleService: ExampleService
    ) {}

    handle(msg: Message) {
        const message = this.exampleService.getMessage();
        msg.channel.send(message);
    }
}

discordBot.addEvent('messageCreate'. MessageDeletedEvent)

Configuration example

Notice: option avatarURL changes bot's avatar every time when app starts.

const discordBot = new DiscordBot({
    token,
    activity: 'CS:GO', // default: undefined
    name: 'New bot name', // default: undefined
    avatarURL: '<PHOTO_URL>', // default: undefined
    prefix: '!',
    ignoreBots: true // ignoring commands from other bots
    // ... and more from discord.js ClientOptions
});

DiscordBot instance methods example

const discordBot = new DiscordBot();

discordBot
.addCommand('help', HelpCommand)
.addCommandForRoles('log', LogCommand, ['Mod', 'Pro'])
.addGenericHandler(HelpCommand) // by default bot is ignoring not registered commands (you can return !help command or just return error message);
.addAnyMessageHandler(AnyMessage) // handling every message (commands are exception)
.removeCommand('help')
.removeAnyMessageHandler()
.clearCommands() // removes all commands
.addEvent('messageDelete', MessageDeleted)
.setupClient(client => {
    console.log(client); // access to client object for more complex operations (read discord.js docs)
});
discordBot.destroy() // logout bot;
1.8.1

3 years ago

1.5.5

3 years ago

1.5.4

3 years ago

1.8.0

3 years ago

1.5.3

3 years ago

1.7.0

3 years ago

1.5.2

3 years ago

1.6.0

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.10

3 years ago

1.3.11

3 years ago

1.3.12

3 years ago

1.2.10

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.1.7

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago