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

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