Options
All
  • Public
  • Public/Protected
  • All
Menu

The main bot framework.

Type parameters

  • S = DefaultState

    If you utilize the internal state for the bot, you can define the type and pass a generic type on initialization of the bot. Example:

    interface BotState { ... }
    const botWithState = new Bot<BotState>({ prefix: ';;' })
    const botWithoutState = new Bot({ prefix: ';;' })

Hierarchy

  • Bot

Index

Constructors

constructor

Properties

Protected client

client: Client | undefined

Discord client to listen to messages on.

see

Discord's documentation

commands

commands: Record<string, Command<S>>

The commands that are defined on the bot. Not suggested to edit directly.

see

Bot.register

errorMessage

errorMessage: string

Generic error given to user if an unexpected error occurs during command operation

invalidCommandError

invalidCommandError: string

Error given to user when they try an unregistered command.

prefix

prefix: string

The prefix that marks a command for this bot. For example, prefix: "!" would mean that the bot would consider any message that starts with ! to be intended for it.

state

state: S

The current state of the bot. Do not edit this directly.

stateListeners

stateListeners: Set<StateListener<S>> = new Set()

Set of functions that will be called when the state is changed.

Methods

close

  • close(): Bot<S>

Private dispatchStateUpdate

  • dispatchStateUpdate(previousState: S, newState: S): void
  • Dispatches state update event.

    Parameters

    • previousState: S

      The previous state of the bot before the update.

    • newState: S

      The current, updated state of the bot.

    Returns void

help

  • help(commandPath: string, help: Help<S>): Bot<S>
  • Register a help message for a command. Must be called after the command is registered.

    Parameters

    • commandPath: string

      The path to the command.

    • help: Help<S>

      The help message.

    Returns Bot<S>

    The bot instance

listen

  • listen(client: Client): Bot<S>

onError

  • onError(error: Error, request: Request<S>): Promise<void>
  • Called when an unexpected error occurs when running a command. Replies with Bot.errorMessage.

    Parameters

    • error: Error

      Error received. Printed to the console.

    • request: Request<S>

      Request received.

    Returns Promise<void>

onHelp

  • onHelp(request: Request<S>): Promise<void>
  • Called when the bot either receives the general help command or the command-level help command (<prefix><command> --help). Responds with the defined help message.

    Parameters

    • request: Request<S>

      Request received.

    Returns Promise<void>

onInvalidCommand

  • onInvalidCommand(message: Message): Promise<void>
  • Called when a user attempts to run an unregistered command. Replies with Bot.invalidCommandError.

    Parameters

    • message: Message

      The message received.

    Returns Promise<void>

onStateUpdate

parse

  • parse(message: string): string[]
  • Parse message if it starts with the bot prefix.

    Parameters

    • message: string

    Returns string[]

    The message contents, split by spaces and with the prefix removed.

receive

  • receive(message: Message): Promise<false | void>
  • Called when a message is received. Interprets if it's intended for the bot and then calls the intended command (if registered).

    Parameters

    • message: Message

      The received message.

    Returns Promise<false | void>

    Returns Promise<false> if the message is clearly not intended for the bot.

register

  • register(options: Plugin<S>): Bot<S>
  • register(commandPath: string, action: Action<S>): Bot<S>
  • register(commandPath: string, action: Action<S>, help: Help<S>): Bot<S>
  • Register a command all in one place. Note: subcommands can only be defined after their parent commands are registered.

    Parameters

    • options: Plugin<S>

      Command definition.

    Returns Bot<S>

    The bot instance.

  • Register a command. Note: subcommands can only be defined after their parent commands are registered.

    Parameters

    • commandPath: string

      What needs to be called to trigger a command. Example: echo or roll advantage.

    • action: Action<S>

      What to do when the command is called.

    Returns Bot<S>

    The bot instance.

  • Register a command with a help message. Note: subcommands can only be defined after their parent commands are registered.

    Parameters

    • commandPath: string

      What needs to be called to trigger a command. Example: echo or roll advantage.

    • action: Action<S>

      What to do when the command is called.

    • help: Help<S>

      The help message.

    Returns Bot<S>

    The bot instance.

setState

  • Updates state of the bot. Triggers a state update event. See Bot.onStateUpdate.

    Type parameters

    • K: keyof S

    Parameters

    • state: PartialState<S, K>

      Either whole or partial state to merge into the current state.

    Returns Bot<S>

    This bot instance.

strictDive

  • strictDive(commands: string[]): Command<S>
  • Find the command at this exact command path. Throws an error if not found.

    Parameters

    • commands: string[]

      The command path to search for a command at.

    Returns Command<S>

    The command found at the command path.

weakDive

  • Try and find a command that matches a list of arguments. Dives deeper until it cannot recognize a registered command.

    Parameters

    • commands: string[]

      The arguments which might contain a command path.

    Returns CommandsThrough<S>

    The command (or not) and the path taken to get there.

Legend

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

Generated using TypeDoc