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

Calamity.Commands.CommandUtils

Description

Command utilities

Synopsis

Documentation

type TypedCommandC ps r = (ApplyTupRes (ParserResult (ListToTup ps)) (CommandSemType r) ~ CommandForParsers ps r, Parser (ListToTup ps) r, ApplyTup (ParserResult (ListToTup ps)) (CommandSemType r), ParamNamesForParsers ps r) Source #

Some constraints used for making parameter typed commands work

type family CommandForParsers (ps :: [Type]) r where ... Source #

Transform a type level list of types implementing the Parser typeclass into the type a command callback matching those parameters should be.

As an example:

CommandForParsers [ Text, Snowflake User, Named "something" Text ] r ~
  (Text -> Snowflake User -> Text -> Sem r (Fail ': r) ())

Equations

CommandForParsers '[] r = Sem (Fail ': r) () 
CommandForParsers (x ': xs) r = ParserResult x -> CommandForParsers xs r 

buildCommand Source #

Arguments

:: forall ps r. (Member (Final IO) r, TypedCommandC ps r) 
=> NonEmpty Text

The name (and aliases) of the command

-> Maybe Group

The parent group of the command

-> Bool

If the command is hidden

-> [Check]

The checks for the command

-> (Context -> Text)

The help generator for this command

-> (Context -> CommandForParsers ps r)

The callback foor this command

-> Sem r Command 

Given the properties of a Command, a callback, and a type level list of the parameters, build a command by constructing a parser and wiring it up to the callback.

Examples

Building a command that bans a user by id.

buildCommand '[Named "user" (Snowflake User), Named "reason" (KleeneStarConcat Text)]
   "ban" Nothing [] (const "Ban a user") $ \ctx uid r -> case (ctx ^. #guild) of
     Just guild -> do
       void . invoke $ CreateGuildBan guild uid (CreateGuildBanData Nothing $ Just r)
       void $ tell ctx ("Banned user `" <> showt uid <> "` with reason: " <> r)
     Nothing -> void $ tell Text ctx "Can only ban users from guilds."

buildCommand' Source #

Arguments

:: Member (Final IO) r 
=> NonEmpty Text

The name (and aliases) of the command

-> Maybe Group

The parent group of the command

-> Bool

If the command is hidden

-> [Check]

The checks for the command

-> [Text]

The names of the command's parameters

-> (Context -> Text)

The help generator for this command

-> (Context -> Sem r (Either CommandError a))

The parser for this command

-> ((Context, a) -> Sem (Fail ': r) ())

The callback for this command

-> Sem r Command 

Given the properties of a Command with the parser and callback in the Sem monad, build a command by transforming the Polysemy actions into IO actions.

buildParser :: Member (Final IO) r => Text -> (Context -> Sem r (Either CommandError a)) -> Sem r (Context -> IO (Either CommandError a)) Source #

Given the name of the command the parser is for and a parser function in the Sem monad, build a parser by transforming the Polysemy action into an IO action.

buildCallback :: Member (Final IO) r => ((Context, a) -> Sem (Fail ': r) ()) -> Sem r ((Context, a) -> IO (Maybe Text)) Source #

Given a callback for a command in the Sem monad, build a command callback by transforming the Polysemy action into an IO action.

runCommand :: Member (Embed IO) r => Context -> Command -> Sem r (Either CommandError ()) Source #

Given an invokation Context, run a command. This does not perform the command's checks.

invokeCommand :: Member (Embed IO) r => Context -> Command -> Sem r (Either CommandError ()) Source #

Given an invokation Context, first run all of the command's checks, then run the command if they all pass.

commandParams :: Command -> Text Source #

Format a command's parameters