From d2d07eb15aa050ba500fda92f901e19ccd3c6e52 Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 8 Apr 2020 19:23:27 +0000 Subject: [PATCH] Change how the example bot handles state --- src/Haboli/Euphoria/Command.hs | 9 +++++---- src/Haboli/Euphoria/ExampleBot.hs | 33 ++++++++++++++++--------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/Haboli/Euphoria/Command.hs b/src/Haboli/Euphoria/Command.hs index fd0e27c..33685d6 100644 --- a/src/Haboli/Euphoria/Command.hs +++ b/src/Haboli/Euphoria/Command.hs @@ -32,10 +32,11 @@ runCommands (c:cs) msg = do -- 'nextEvent': -- -- > event <- respondingToCommands commands nextEvent -respondingToCommands :: [Command e] -> Client e Event -> Client e Event -respondingToCommands cmds holdingEvent = do - event <- holdingEvent +respondingToCommands :: Client e Event -> Client e [Command e] -> Client e Event +respondingToCommands getEvent getCommands = do + event <- getEvent + commands <- getCommands case event of - EventSend e -> void $ runCommands cmds $ sendMessage e + EventSend e -> void $ runCommands commands $ sendMessage e _ -> pure () pure event diff --git a/src/Haboli/Euphoria/ExampleBot.hs b/src/Haboli/Euphoria/ExampleBot.hs index cc69862..28fcbd8 100644 --- a/src/Haboli/Euphoria/ExampleBot.hs +++ b/src/Haboli/Euphoria/ExampleBot.hs @@ -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?" + ]