Change naming scheme of cmd functions

Also add documentation for the existing command modules
This commit is contained in:
Joscha 2020-04-08 17:15:02 +00:00
parent eabfe0fd75
commit 30f00fda39
2 changed files with 64 additions and 15 deletions

View file

@ -1,5 +1,8 @@
-- | Bot commands based on the megaparsec library.
module Haboli.Euphoria.Command.Megaparsec module Haboli.Euphoria.Command.Megaparsec
( cmdMega ( cmdMega
, cmdMega'
) where ) where
import qualified Data.Text as T import qualified Data.Text as T
@ -9,7 +12,15 @@ import Haboli.Euphoria.Api
import Haboli.Euphoria.Client import Haboli.Euphoria.Client
import Haboli.Euphoria.Command import Haboli.Euphoria.Command
-- | Turn a megaparsec parser into a bot command. Applies the parser to the
-- content of the message. If the parser fails to parse the message content, the
-- command fails.
cmdMega :: Parsec e' T.Text a -> (Message -> a -> Client e ()) -> Command e cmdMega :: Parsec e' T.Text a -> (Message -> a -> Client e ()) -> Command e
cmdMega parser f msg = case parse parser "" $ msgContent msg of cmdMega parser f = cmdMega' parser $ \msg a -> True <$ f msg a
-- | A version of 'cmdMega' that allows the command function to decide whether
-- the command was successful or not.
cmdMega' :: Parsec e' T.Text a -> (Message -> a -> Client e Bool) -> Command e
cmdMega' parser f msg = case parse parser "" $ msgContent msg of
Left _ -> pure False Left _ -> pure False
Right a -> True <$ f msg a Right a -> f msg a

View file

@ -1,10 +1,20 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
-- | General and specific commands as described in the
-- [botrulez](https://github.com/jedevc/botrulez).
module Haboli.Euphoria.Command.Simple module Haboli.Euphoria.Command.Simple
( cmdGeneral (
-- * General commands
cmdGeneral
, cmdGeneral' , cmdGeneral'
, cmdGeneralArgs
, cmdGeneralArgs'
-- * Specific commands
, cmdSpecific , cmdSpecific
, cmdSpecific' , cmdSpecific'
, cmdSpecificArgs
, cmdSpecificArgs'
) where ) where
import Control.Monad import Control.Monad
@ -39,20 +49,48 @@ pCmdGeneral cmd = pCmd cmd *> space *> pUntilEof
pCmdSpecific :: T.Text -> T.Text -> Parser T.Text pCmdSpecific :: T.Text -> T.Text -> Parser T.Text
pCmdSpecific cmd name = pCmd cmd *> space1 *> pNick name *> space *> pUntilEof pCmdSpecific cmd name = pCmd cmd *> space1 *> pNick name *> space *> pUntilEof
pWithoutArgs :: Parser T.Text -> Parser () -- | @'cmdGeneral' cmd f' is a general command with no arguments in the form of
pWithoutArgs p = do -- @!cmd@.
args <- p
guard $ T.null args
cmdGeneral :: T.Text -> (Message -> Client e ()) -> Command e cmdGeneral :: T.Text -> (Message -> Client e ()) -> Command e
cmdGeneral cmd f = cmdMega (pWithoutArgs $ pCmdGeneral cmd) $ \msg _ -> f msg cmdGeneral cmd f = cmdGeneral' cmd $ \msg -> True <$ f msg
cmdGeneral' :: T.Text -> (Message -> T.Text -> Client e ()) -> Command e -- | A version of 'cmdGeneral' that allows the command function to decide
cmdGeneral' cmd = cmdMega $ pCmdGeneral cmd -- whether the command was successful or not.
cmdGeneral' :: T.Text -> (Message -> Client e Bool) -> Command e
cmdGeneral' cmd f = cmdGeneralArgs' cmd $ \msg args -> if T.null args
then f msg
else pure False
-- | @'cmdGeneralArgs' cmd f' is a general command with arguments in the form of
-- @!cmd args@. @f@ is called with the source message and the arguments as
-- 'T.Text'.
cmdGeneralArgs :: T.Text -> (Message -> T.Text -> Client e ()) -> Command e
cmdGeneralArgs cmd f = cmdGeneralArgs' cmd $ \msg args -> True <$ f msg args
-- | A version of 'cmdGeneralArgs' that allows the command function to decide
-- whether the command was successful or not.
cmdGeneralArgs' :: T.Text -> (Message -> T.Text -> Client e Bool) -> Command e
cmdGeneralArgs' cmd = cmdMega' $ pCmdGeneral cmd
-- | @'cmdSpecific' cmd nick f@ is a specific command with no arguments in the
-- form of @!cmd \@nick@.
cmdSpecific :: T.Text -> T.Text -> (Message -> Client e ()) -> Command e cmdSpecific :: T.Text -> T.Text -> (Message -> Client e ()) -> Command e
cmdSpecific cmd name f = cmdSpecific cmd name f = cmdSpecific' cmd name $ \msg -> True <$ f msg
cmdMega (pWithoutArgs $ pCmdSpecific cmd name) $ \msg _ -> f msg
cmdSpecific' :: T.Text -> T.Text -> (Message -> T.Text -> Client e ()) -> Command e -- | A version of 'cmdSpecific' that allows the command function to decide
cmdSpecific' cmd name = cmdMega $ pCmdSpecific cmd name -- whether the command was successful or not.
cmdSpecific' :: T.Text -> T.Text -> (Message -> Client e Bool) -> Command e
cmdSpecific' cmd name f = cmdSpecificArgs' cmd name $ \msg args -> if T.null args
then f msg
else pure False
-- | @'cmdSpecificArgs' cmd nick f@ is a specific command with arguments in the
-- form of @!cmd \@nick args@. @f@ is called with the source message and the
-- arguments as 'T.Text'.
cmdSpecificArgs :: T.Text -> T.Text -> (Message -> T.Text -> Client e ()) -> Command e
cmdSpecificArgs cmd name f = cmdSpecificArgs' cmd name $ \msg args -> True <$ f msg args
-- | A version of 'cmdSpecificArgs' that allows the command function to decide
-- whether the command was successful or not.
cmdSpecificArgs' :: T.Text -> T.Text -> (Message -> T.Text -> Client e Bool) -> Command e
cmdSpecificArgs' cmd name = cmdMega' $ pCmdSpecific cmd name