calamity-0.1.14.7: 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.

Equations

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

buildCommand :: forall ps r. (Member (Final IO) r, TypedCommandC ps r) => NonEmpty Text -> Maybe Group -> [Check] -> (Context -> Text) -> (Context -> CommandForParsers ps r) -> Sem r Command Source #

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 . reason r $ CreateGuildBan guild uid
       void $ tell ctx ("Banned user `" <> showt uid <> "` with reason: " <> r)
     Nothing -> void $ tell Text ctx "Can only ban users from guilds."

buildCommand' :: Member (Final IO) r => NonEmpty Text -> Maybe Group -> [Check] -> [Text] -> (Context -> Text) -> (Context -> Sem r (Either CommandError a)) -> ((Context, a) -> Sem (Fail ': r) ()) -> Sem r Command Source #

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.