Options
All
  • Public
  • Public/Protected
  • All
Menu

Discorb

A flexible framework for creating a Discord bot in Typescript.

Installation

yarn add discorb discord.js

What do I need to get started?

  • To create a Discord application
  • To create a bot user attached to your application
  • To have the bot's secret token (suggested to add to the environment and loaded via dotenv)
  • A vision for the future

Simple example

import { Bot } from 'discorb'
import { Client } from 'discord.js'

const bot = new Bot({ prefix: ';;' })
  // ;;ping
  // ;;ping This will also be returned
  .register('ping', (request) => {
    request.message.reply(
      request.args.length > 0
        ? `Pong! (with args: ${request.args.map((a) => `\`${a}\``).join(' ')})`
        : 'Pong!'
    )
  })
  // ;;ping --help
  .help(
    'ping',
    `Responds with "Pong!" as well as returning any additional arguments.`
  )
  // ;;ping pong
  .register('ping pong', (request) => {
    request.message.reply('Ping pong!')
  })
  // ;;ping pong --help
  .help('ping pong', 'Just says ping pong! Simple as that!')

if (process.env.DISCORD_LOGIN == undefined) {
  throw new Error('Please provide a DISCORD_LOGIN env variable')
} else {
  const client = new Client()
  client.login(process.env.DISCORD_LOGIN)
  bot.listen(client)
}

process.on('exit', () => {
  bot.close()
})

For more, take a look at the examples directory.

Documentation

View documentation here.

License

This project uses the following license: Hippocratic v2.1.

Index

Type aliases

Action

Action<S>: (this: Bot<S>, request: Request<S>) => void | Promise<void>

Callback function for commands.

Type parameters

  • S = DefaultState

    Bot state type

Type declaration

    • (this: Bot<S>, request: Request<S>): void | Promise<void>
    • Parameters

      • this: Bot<S>

        Function is bound to the bot instance. Not an actual param.

      • request: Request<S>

        The request received that triggered the command.

      Returns void | Promise<void>

DefaultState

DefaultState: Record<string | number | symbol, unknown>

Help

Help<S>: HelpMessage | ((this: Bot<S>) => HelpMessage | Promise<HelpMessage>)

Message or message generator to be displayed when <prefix><command> --help or <prefix>help is called

Type parameters

  • S = DefaultState

    Bot state type

HelpMessage

HelpMessage: APIMessageContentResolvable | (MessageOptions & { split?: false }) | MessageAdditions

Message displayed when <prefix><command> --help or <prefix>help is called

PartialState

PartialState<S, K>: Pick<S, K> | S

For use with Bot.setState. Either the entire state or a subset of it.

Type parameters

  • S

    Bot state type

  • K: keyof S

    Keys of the bot state

StateListener

StateListener<S>: (this: Bot<S>, previousState: S, nextState: S) => void | Promise<void>

Listener for state changes.

Type parameters

  • S = DefaultState

    Bot state type

Type declaration

    • (this: Bot<S>, previousState: S, nextState: S): void | Promise<void>
    • Parameters

      • this: Bot<S>

        Function is bound to the bot instance. Not an actual param.

      • previousState: S

        The previous state of the bot before the update.

      • nextState: S

        The current, updated state of the bot.

      Returns void | Promise<void>

Legend

  • Constructor
  • Property
  • Method
  • Property
  • Protected property
  • Private method

Generated using TypeDoc