Copy ExampleBot over as InfoBot

This commit is contained in:
Joscha 2020-04-09 11:47:22 +00:00
parent 675e88a105
commit 5292fd9883
6 changed files with 110 additions and 11 deletions

View file

@ -0,0 +1,80 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Haboli.Bots.InfoBot
( infoBot
) where
import Control.Concurrent
import Control.Monad
import Control.Monad.IO.Class
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import Data.Time
import Lens.Micro.Platform
import Haboli.Euphoria
import Haboli.Euphoria.Botrulez
data BotState = BotState
{ _botStartTime :: UTCTime
, _botListing :: Listing
} deriving (Show)
makeLenses ''BotState
-- | 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:
--
-- > runClient defaultConfig $ exampleBot Nothing
infoBot :: Maybe T.Text -> Client T.Text ()
infoBot mPasswd = do
startTime <- liftIO getCurrentTime
initialEvents <- untilConnected $
respondingToBounce mPasswd $
respondingToPing nextEvent
let initialState = BotState startTime $ newListing initialEvents
stateVar <- liftIO $ newMVar initialState
preferNickVia botListing stateVar "InfoBot"
botMain stateVar
botMain :: MVar BotState -> Client T.Text ()
botMain stateVar = forever $ do
event <- respondingToCommands (getCommands stateVar) $
respondingToPing nextEvent
updateFromEventVia botListing stateVar event
getCommands :: MVar BotState -> Client e [Command T.Text]
getCommands stateVar = do
state <- liftIO $ readMVar stateVar
let name = state ^. botListing . lsSelfL . svNickL
pure
[ botrulezPingGeneral
, botrulezPingSpecific name
, botrulezHelpSpecific name
"I am an example bot for https://github.com/Garmelon/haboli/."
, botrulezUptimeSpecific name $ state ^. botStartTime
, botrulezKillSpecific name
, cmdSpecific "hug" name $ \msg -> void $ reply msg "/me hugs back"
, cmdHello
, cmdNick stateVar name
, cmdWho stateVar
]
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
preferNickVia botListing stateVar args
void $ reply msg "Is this better?"
cmdWho :: MVar BotState -> Command e
cmdWho stateVar = cmdGeneral "who" $ \msg -> do
state <- liftIO $ readMVar stateVar
let people = state ^. botListing . lsOthersL
nicks = sort $ map svNick $ Map.elems people
void $ reply msg $ T.intercalate "\n" nicks