Safe Haskell | None |
---|---|
Language | Haskell2010 |
A DSL for generating commands and groups
Synopsis
- command :: forall ps c a m r. (Monad m, Member (Final m) r, TypedCommandC ps c a r, CommandContext m c a) => Text -> (c -> CommandForParsers ps r a) -> Sem (DSLState m c a r) (Command m c a)
- command' :: (Monad m, Member (Final m) r) => Text -> [ParameterInfo] -> (c -> Sem r (Either CommandError p)) -> ((c, p) -> Sem (Fail ': r) a) -> Sem (DSLState m c a r) (Command m c a)
- commandA :: forall ps c a m r. (Monad m, Member (Final m) r, TypedCommandC ps c a r, CommandContext m c a) => Text -> [Text] -> (c -> CommandForParsers ps r a) -> Sem (DSLState m c a r) (Command m c a)
- commandA' :: forall p c a m r. (Monad m, Member (Final m) r) => Text -> [Text] -> [ParameterInfo] -> (c -> Sem r (Either CommandError p)) -> ((c, p) -> Sem (Fail ': r) a) -> Sem (DSLState m c a r) (Command m c a)
- hide :: Member (Tagged "hidden" (Reader Bool)) r => Sem r x -> Sem r x
- help :: Member (Reader (c -> Text)) r => (c -> Text) -> Sem r a -> Sem r a
- requires :: [Check m c] -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x
- requires' :: (Monad m, Member (Final m) r) => Text -> (c -> Sem r (Maybe Text)) -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x
- requiresPure :: Monad m => [(Text, c -> Maybe Text)] -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x
- group :: (Monad m, Member (Final m) r) => Text -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x
- group' :: Member (Final m) r => Text -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x
- groupA :: forall x c m a r. (Monad m, Member (Final m) r) => Text -> [Text] -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x
- groupA' :: forall x c m a r. Member (Final m) r => Text -> [Text] -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x
- type DSLState m c a r = LocalWriter (HashMap Text (Command m c a, AliasType)) ': (LocalWriter (HashMap Text (Group m c a, AliasType)) ': (Reader (Maybe (Group m c a)) ': (Tagged "hidden" (Reader Bool) ': (Reader (c -> Text) ': (Tagged "original-help" (Reader (c -> Text)) ': (Reader [Check m c] ': (Reader (CommandHandler m c a) ': (Fixpoint ': r))))))))
- raiseDSL :: Sem r x -> Sem (DSLState m c a r) x
- fetchHandler :: Sem (DSLState m c a r) (CommandHandler m c a)
Commands DSL
This module provides a way of constructing bot commands in a declarative way.
The main component of this is the command
function, which takes a
type-level list of command parameters, the name, and the callback and
produces a command. There are also the alternatives command'
, commandA
and commandA'
, for when you want to handle parsing of the input yourself,
and/or want aliases of the command.
The functions: hide
, help
, requires
, and group
can be used to change
attributes of any commands declared inside the monadic action passed to them,
for example:
hide
$
docommand
@'[] "test" \ctx ->pure
()
In the above block, any command declared inside hide
will have it's
'hidden' flag set and will not be shown by the default help command:
helpCommand
The helpCommand
function can be used to create a
help command for the commands DSL action it is used in, read it's doc page
for more information on how it functions.
The buildCommands
function is used to
construct a CommandHandler
which can then be used with
processCommands
or
handleCommands
to process a command.
:: forall ps c a m r. (Monad m, Member (Final m) r, TypedCommandC ps c a r, CommandContext m c a) | |
=> Text | The name of the command |
-> (c -> CommandForParsers ps r a) | The callback for this command |
-> Sem (DSLState m c a r) (Command m c a) |
Given the name of a command and a callback, and a type level list of the parameters, build and register a command.
The parent group, visibility, checks, and command help are drawn from the reader context.
Command parameters are parsed by first invoking
parse
for the first
Parser
, then running the next parser on the
remaining input, and so on.
Examples
Building a command that adds two numbers.
command
@'[Named
"a"Int
,Named
"b"Int
] "add" $ \ctx a b ->pure
$
Right
(a+
b)
:: (Monad m, Member (Final m) r) | |
=> Text | The name of the command |
-> [ParameterInfo] | The command's parameter metadata |
-> (c -> Sem r (Either CommandError p)) | The parser for this command |
-> ((c, p) -> Sem (Fail ': r) a) | The callback for this command |
-> Sem (DSLState m c a r) (Command m c a) |
Given the command name and parameter names, parser
and callback
for a
command in the Sem
monad, build a command by transforming the Polysemy
actions into m
actions. Then register the command.
The parent group, visibility, checks, and command help are drawn from the reader context.
:: forall ps c a m r. (Monad m, Member (Final m) r, TypedCommandC ps c a r, CommandContext m c a) | |
=> Text | The name of the command |
-> [Text] | The aliases for the command |
-> (c -> CommandForParsers ps r a) | The callback for this command |
-> Sem (DSLState m c a r) (Command m c a) |
Given the name and aliases of a command and a callback, and a type level list of the parameters, build and register a command.
The parent group, visibility, checks, and command help are drawn from the reader context.
Examples
Building a command that adds two numbers.
commandA
@'[Named
"a"Int
,Named
"b"Int
] "add" [] $ \ctx a b ->pure
$
Right
(a+
b)
:: forall p c a m r. (Monad m, Member (Final m) r) | |
=> Text | The name of the command |
-> [Text] | The aliases for the command |
-> [ParameterInfo] | The command's parameter metadata |
-> (c -> Sem r (Either CommandError p)) | The parser for this command |
-> ((c, p) -> Sem (Fail ': r) a) | The callback for this command |
-> Sem (DSLState m c a r) (Command m c a) |
Given the command name, aliases, and parameter names, parser
and callback
for a command in the Sem
monad, build a command by transforming the
Polysemy actions into m
actions. Then register the command.
The parent group, visibility, checks, and command help are drawn from the reader context.
hide :: Member (Tagged "hidden" (Reader Bool)) r => Sem r x -> Sem r x Source #
Set the visibility of any groups or commands registered inside the given action to hidden.
requires :: [Check m c] -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x Source #
Add to the list of checks for any commands registered inside the given action.
:: (Monad m, Member (Final m) r) | |
=> Text | The name of the check |
-> (c -> Sem r (Maybe Text)) | The callback for the check |
-> Sem (DSLState m c a r) x | |
-> Sem (DSLState m c a r) x |
Construct a check and add it to the list of checks for any commands registered inside the given action.
Refer to Check
for more info on checks.
requiresPure :: Monad m => [(Text, c -> Maybe Text)] -> Sem (DSLState m c a r) x -> Sem (DSLState m c a r) x Source #
:: (Monad m, Member (Final m) r) | |
=> Text | The name of the group |
-> Sem (DSLState m c a r) x | |
-> Sem (DSLState m c a r) x |
Construct a group and place any commands registered in the given action into the new group.
This also resets the help
function back to it's original value, use
group'
if you don't want that (i.e. your help function is context aware).
:: Member (Final m) r | |
=> Text | The name of the group |
-> Sem (DSLState m c a r) x | |
-> Sem (DSLState m c a r) x |
Construct a group and place any commands registered in the given action into the new group.
The parent group, visibility, checks, and command help are drawn from the reader context.
Unlike help
this doesn't reset the help
function back to it's original
value.
:: forall x c m a r. (Monad m, Member (Final m) r) | |
=> Text | The name of the group |
-> [Text] | The aliases of the group |
-> Sem (DSLState m c a r) x | |
-> Sem (DSLState m c a r) x |
Construct a group with aliases and place any commands registered in the given action into the new group.
The parent group, visibility, checks, and command help are drawn from the reader context.
This also resets the help
function back to it's original value, use
group'
if you don't want that (i.e. your help function is context aware).
:: forall x c m a r. Member (Final m) r | |
=> Text | The name of the group |
-> [Text] | The aliases of the group |
-> Sem (DSLState m c a r) x | |
-> Sem (DSLState m c a r) x |
Construct a group with aliases and place any commands registered in the given action into the new group.
The parent group, visibility, checks, and command help are drawn from the reader context.
Unlike help
this doesn't reset the help
function back to it's original
value.
type DSLState m c a r = LocalWriter (HashMap Text (Command m c a, AliasType)) ': (LocalWriter (HashMap Text (Group m c a, AliasType)) ': (Reader (Maybe (Group m c a)) ': (Tagged "hidden" (Reader Bool) ': (Reader (c -> Text) ': (Tagged "original-help" (Reader (c -> Text)) ': (Reader [Check m c] ': (Reader (CommandHandler m c a) ': (Fixpoint ': r)))))))) Source #
fetchHandler :: Sem (DSLState m c a r) (CommandHandler m c a) Source #
Retrieve the final command handler for this block