Fix example bot and revert argument order of respondingToCommands

This commit is contained in:
Joscha 2020-04-08 21:53:41 +00:00
parent 0e0596765e
commit eafa00cc2a
2 changed files with 38 additions and 12 deletions

View file

@ -25,7 +25,7 @@ runCommands (c:cs) msg = do
then pure True then pure True
else runCommands cs msg else runCommands cs msg
-- | @'respondingToCommands' getEvent getCommands@ runs a list of 'Command's on -- | @'respondingToCommands' getCommands getEvent@ runs a list of 'Command's on
-- all 'EventSend's. It passes through all events unmodified. -- all 'EventSend's. It passes through all events unmodified.
-- --
-- The @getEvent@ action is used to obtain the next 'Event'. The @getCommands@ -- The @getEvent@ action is used to obtain the next 'Event'. The @getCommands@
@ -35,9 +35,9 @@ runCommands (c:cs) msg = do
-- This utility function is meant to be wrapped directly or indirectly around -- This utility function is meant to be wrapped directly or indirectly around
-- 'nextEvent': -- 'nextEvent':
-- --
-- > event <- respondingToCommands nextEvent commands -- > event <- respondingToCommands commands nextEvent
respondingToCommands :: Client e Event -> Client e [Command e] -> Client e Event respondingToCommands :: Client e [Command e] -> Client e Event -> Client e Event
respondingToCommands getEvent getCommands = do respondingToCommands getCommands getEvent = do
event <- getEvent event <- getEvent
commands <- getCommands commands <- getCommands
case event of case event of

View file

@ -10,6 +10,8 @@ module Haboli.Euphoria.ExampleBot
import Control.Concurrent import Control.Concurrent
import Control.Monad import Control.Monad
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.Text as T import qualified Data.Text as T
import Data.Time import Data.Time
@ -36,7 +38,15 @@ exampleBot mPasswd = do
botMain stateVar botMain stateVar
botMain :: MVar BotState -> Client T.Text () botMain :: MVar BotState -> Client T.Text ()
botMain stateVar = forever $ respondingToPing $ respondingToCommands nextEvent $ do botMain stateVar = forever $ do
event <- respondingToCommands (getCommands stateVar) $
respondingToPing nextEvent
-- Update the listing
liftIO $ modifyMVar_ stateVar $ \state ->
pure state{botListing = updateFromEvent event $ botListing state}
getCommands :: MVar BotState -> Client e [Command T.Text]
getCommands stateVar = do
state <- liftIO $ readMVar stateVar state <- liftIO $ readMVar stateVar
let name = svNick $ self $ botListing state let name = svNick $ self $ botListing state
pure pure
@ -46,11 +56,27 @@ botMain stateVar = forever $ respondingToPing $ respondingToCommands nextEvent $
, botrulezUptimeSpecific name $ botStartTime state , botrulezUptimeSpecific name $ botStartTime state
, botrulezKillSpecific name , botrulezKillSpecific name
, cmdSpecific "hug" name $ \msg -> void $ reply msg "/me hugs back" , cmdSpecific "hug" name $ \msg -> void $ reply msg "/me hugs back"
, cmdGeneral "hello" $ \msg -> , cmdHello
void $ reply msg $ "Hi there, " <> nickMention (svNick $ msgSender msg) <> "!" , cmdNick stateVar name
, cmdSpecificArgs "nick" name $ \msg args -> do , cmdWho stateVar
s <- liftIO $ takeMVar stateVar
listing' <- preferNick args $ botListing s
liftIO $ putMVar stateVar s{botListing = listing'}
void $ reply msg "Is this better?"
] ]
cmdHello :: Command e
cmdHello = cmdGeneral "hello" $ \msg -> do
let mention = nickMention $ svNick $ msgSender msg
void $ reply msg $ "Hi there, @" <> mention <> "!"
cmdNick :: MVar BotState -> T.Text -> Command e
cmdNick stateVar name = cmdSpecificArgs "nick" name $ \msg args -> do
-- Update the listing while updating the nick
state <- liftIO $ takeMVar stateVar
listing' <- preferNick args $ botListing state
liftIO $ putMVar stateVar state{botListing = listing'}
void $ reply msg "Is this better?"
cmdWho :: MVar BotState -> Command e
cmdWho stateVar = cmdGeneral "who" $ \msg -> do
state <- liftIO $ readMVar stateVar
let people = others $ botListing state
nicks = sort $ map svNick $ Map.elems people
void $ reply msg $ T.intercalate "\n" nicks