| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Calamity.Commands.CommandUtils
Description
Command utilities
Synopsis
- 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)
- type family CommandForParsers (ps :: [Type]) r where ...
- buildCommand :: forall ps r. (Member (Final IO) r, TypedCommandC ps r) => NonEmpty Text -> Maybe Group -> Bool -> [Check] -> (Context -> Text) -> (Context -> CommandForParsers ps r) -> Sem r Command
- buildCommand' :: Member (Final IO) r => NonEmpty Text -> Maybe Group -> Bool -> [Check] -> [Text] -> (Context -> Text) -> (Context -> Sem r (Either CommandError a)) -> ((Context, a) -> Sem (Fail ': r) ()) -> Sem r Command
- buildParser :: Member (Final IO) r => Text -> (Context -> Sem r (Either CommandError a)) -> Sem r (Context -> IO (Either CommandError a))
- buildCallback :: Member (Final IO) r => ((Context, a) -> Sem (Fail ': r) ()) -> Sem r ((Context, a) -> IO (Maybe Text))
- runCommand :: Member (Embed IO) r => Context -> Command -> Sem r (Either CommandError ())
- invokeCommand :: Member (Embed IO) r => Context -> Command -> Sem r (Either CommandError ())
- groupPath :: Group -> [Text]
- commandPath :: Command -> [Text]
- commandParams :: Command -> Text
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,SnowflakeUser,Named"something"Text] r ~ (Text->SnowflakeUser->Text->Semr (Fail': r) ())
Equations
| CommandForParsers '[] r = Sem (Fail ': r) () | |
| CommandForParsers (x ': xs) r = ParserResult x -> CommandForParsers xs r |
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" (SnowflakeUser),Named"reason" (KleeneStarConcatText)] "ban"Nothing[] (const"Ban a user") $ \ctx uid r -> case (ctx^.#guild) ofJustguild -> dovoid.invoke$CreateGuildBanguild uid (CreateGuildBanDataNothing$Justr)void$tellctx ("Banned user `"<>showtuid<>"` with reason: "<>r)Nothing->void$tellTextctx "Can only ban users from guilds."
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 |
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.
commandPath :: Command -> [Text] Source #
commandParams :: Command -> Text Source #
Format a command's parameters