Change how the example bot handles state

This commit is contained in:
Joscha 2020-04-08 19:23:27 +00:00
parent 15cd6724d2
commit d2d07eb15a
2 changed files with 22 additions and 20 deletions

View file

@ -32,10 +32,11 @@ runCommands (c:cs) msg = do
-- 'nextEvent': -- 'nextEvent':
-- --
-- > event <- respondingToCommands commands nextEvent -- > event <- respondingToCommands commands nextEvent
respondingToCommands :: [Command e] -> Client e Event -> Client e Event respondingToCommands :: Client e Event -> Client e [Command e] -> Client e Event
respondingToCommands cmds holdingEvent = do respondingToCommands getEvent getCommands = do
event <- holdingEvent event <- getEvent
commands <- getCommands
case event of case event of
EventSend e -> void $ runCommands cmds $ sendMessage e EventSend e -> void $ runCommands commands $ sendMessage e
_ -> pure () _ -> pure ()
pure event pure event

View file

@ -7,11 +7,10 @@ module Haboli.Euphoria.ExampleBot
( exampleBot ( exampleBot
) where ) where
import Control.Concurrent
import Control.Monad import Control.Monad
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Control.Monad.Trans.Class import qualified Data.Text as T
import Control.Monad.Trans.State
import qualified Data.Text as T
import Data.Time import Data.Time
import Haboli.Euphoria import Haboli.Euphoria
@ -22,8 +21,6 @@ data BotState = BotState
, botListing :: Listing , botListing :: Listing
} deriving (Show) } deriving (Show)
type Bot = StateT BotState (Client T.Text)
-- | A small example bot. Takes a room password as its first argument. You can -- | A small example bot. Takes a room password as its first argument. You can
-- run this bot in [&test](https://euphoria.io/room/test) like this: -- run this bot in [&test](https://euphoria.io/room/test) like this:
-- --
@ -35,21 +32,25 @@ exampleBot mPasswd = do
respondingToBounce mPasswd $ respondingToBounce mPasswd $
respondingToPing nextEvent respondingToPing nextEvent
listing <- preferNick "ExampleBot" $ newListing initialEvents listing <- preferNick "ExampleBot" $ newListing initialEvents
void $ runStateT botMain $ BotState startTime listing stateVar <- liftIO $ newMVar $ BotState startTime listing
botMain stateVar
botMain :: Bot () botMain :: MVar BotState -> Client T.Text ()
botMain = forever $ do botMain stateVar = forever $ respondingToPing $ respondingToCommands nextEvent $ do
s <- get state <- liftIO $ readMVar stateVar
let name = svNick $ self $ botListing s let name = svNick $ self $ botListing state
lift $ respondingToCommands pure
[ botrulezPingGeneral [ botrulezPingGeneral
, botrulezPingSpecific name , botrulezPingSpecific name
, botrulezHelpSpecific name "I am an example bot for https://github.com/Garmelon/haboli/." , botrulezHelpSpecific name "I am an example bot for https://github.com/Garmelon/haboli/."
, botrulezUptimeSpecific name $ botStartTime s , botrulezUptimeSpecific name $ botStartTime state
, botrulezKillSpecific name , botrulezKillSpecific name
, cmdSpecific "hug" name $ \msg -> void $ reply msg "/me hugs back"
, cmdGeneral "hello" $ \msg -> , cmdGeneral "hello" $ \msg ->
void $ reply msg $ "Hi there, " <> nickMention (svNick $ msgSender msg) <> "!" void $ reply msg $ "Hi there, " <> nickMention (svNick $ msgSender msg) <> "!"
, cmdSpecific "hug" name $ \msg -> , cmdSpecificArgs "nick" name $ \msg args -> do
void $ reply msg "/me hugs back" s <- liftIO $ takeMVar stateVar
] $ respondingToPing nextEvent listing' <- preferNick args $ botListing s
liftIO $ putMVar stateVar s{botListing = listing'}
void $ reply msg "Is this better?"
]