calamity-0.1.28.4: A library for writing discord bots in haskell
Safe HaskellNone
LanguageHaskell2010

Calamity.Commands.Utils

Description

Command handler utilities

Synopsis

Documentation

addCommands :: (BotC r, Member ParsePrefix r) => Sem (DSLState r) a -> Sem r (Sem r (), CommandHandler, a) Source #

Construct commands and groups from a command DSL, then registers an event handler on the bot that manages running those commands.

Returns an action to remove the event handler, and the CommandHandler that was constructed.

Command Resolution

To determine if a command was invoked, and if so which command was invoked, the following happens:

  1. parsePrefix is invoked, if no prefix is found: stop here.
  2. The input is read a word at a time until a matching command is found, fire the "command-not-found" event if not.
  3. A Context is built, and the command invoked.

Custom Events

This will fire the following events:

  1. "command-error" (Context, CommandError)

    Fired when a command returns an error.

  2. "command-not-found" (Message, [Text])

    Fired when a valid prefix is used, but the command is not found.

  3. "command-invoked" Context

    Fired when a command is successfully invoked.

buildCommands :: forall r a. Member (Final IO) r => Sem (DSLState r) a -> Sem r (CommandHandler, a) Source #

Run a command DSL, returning the constructed CommandHandler

buildContext :: BotC r => Message -> Text -> Command -> Text -> Sem r (Maybe Context) Source #

Attempt to build the context for a command

handleCommands Source #

Arguments

:: (BotC r, Member ParsePrefix r) 
=> CommandHandler 
-> Message

The message that invoked the command

-> Text

The prefix used

-> Text

The command string, without a prefix

-> Sem r (Either CmdInvokeFailReason Context) 

Manages parsing messages and handling commands for a CommandHandler.

Returns Right if the command succeeded in parsing and running, Left with the reason otherwise.

findCommand :: CommandHandler -> Text -> Either [Text] (Command, Text) Source #

Attempt to find what command was used.

On error: returns the path of existing groups that were found, so "group0 group1 group2 notacommand" will error with Left ["group0", "group1", "group2"]

On success: returns the command that was invoked, and the remaining text after it.

This function isn't greedy, if you have a group and a command at the same level, this will find the command first and ignore the group.