Add bot commands

This commit is contained in:
Joscha 2018-02-17 18:09:02 +00:00
parent 6b80c0970e
commit fa33ef92d0
2 changed files with 87 additions and 16 deletions

View file

@ -4,6 +4,15 @@
-- | This module lets you create bots, although it only contains the bare minimum necessary. -- | This module lets you create bots, although it only contains the bare minimum necessary.
-- It defines the 'Bot' monad which takes care of a few things common to most bots. -- It defines the 'Bot' monad which takes care of a few things common to most bots.
-- --
-- The module is meant to be imported qualified, under a different name than all the
-- other EuphApi modules.
-- For example:
--
-- > import qualified EuphApi.Bot as B
-- > import qualified EuphApi.Connection as E
-- > import qualified EuphApi.Types as E
-- > import qualified EuphApi.Utils as E
--
-- = The 'Bot' monad -- = The 'Bot' monad
-- --
-- This monad takes care of -- This monad takes care of
@ -62,7 +71,6 @@ module EuphApi.Bot (
Bot Bot
, BotConfig(..) , BotConfig(..)
, runBot , runBot
-- * Bot commands
-- * Utilities -- * Utilities
, fork , fork
, defaultReconnectPolicy , defaultReconnectPolicy
@ -72,6 +80,15 @@ module EuphApi.Bot (
, getConnection , getConnection
, getConnectionInfo , getConnectionInfo
, getConnectTime , getConnectTime
-- * Bot commands
, stop
, send
, reply
, nick
, getMessage
, messageLog
, messageLogAfter
, who
) where ) where
import Control.Concurrent import Control.Concurrent
@ -85,6 +102,7 @@ import Data.Time
import qualified System.Log.Logger as L import qualified System.Log.Logger as L
import qualified EuphApi.Connection as E import qualified EuphApi.Connection as E
import qualified EuphApi.Types as E
-- logging functions -- logging functions
moduleName :: String moduleName :: String
@ -277,8 +295,8 @@ handleNickStuff (E.SnapshotEvent _ _ _ maybeNick) = do
con <- getConnection con <- getConnection
case maybeNick of case maybeNick of
Nothing -> fork $ liftIO $ E.nick con myNick Nothing -> fork $ liftIO $ E.nick con myNick
Just nick -> Just curNick ->
if nick == myNick if curNick == myNick
then return () then return ()
else fork $ liftIO $ E.nick con myNick else fork $ liftIO $ E.nick con myNick
handleNickStuff _ = return () handleNickStuff _ = return ()
@ -298,3 +316,57 @@ handlePasswordStuff _ = return ()
{- {-
- Commands - Commands
-} -}
-- | Stop the bot.
stop :: Bot b c ()
stop = do
stopping <- asks bStopping
con <- asks bConnection
liftIO $ do
atomically $ writeTVar stopping False
E.disconnect con
-- | Send a new message.
send :: T.Text -> Bot b c E.Message
send content = do
con <- asks bConnection
liftIO $ E.send con Nothing content
-- | Reply to a message.
reply :: E.Snowflake -> T.Text -> Bot b c E.Message
reply parentID content = do
con <- asks bConnection
liftIO $ E.send con (Just parentID) content
-- | Change the bot's nick.
nick :: T.Text -> Bot b c (T.Text, T.Text)
nick newNick = do
myNick <- asks bNick
con <- asks bConnection
liftIO $ do
atomically $ writeTVar myNick newNick
E.nick con newNick
-- | Request an untruncated message.
getMessage :: E.Snowflake -> Bot b c E.Message
getMessage messageID = do
con <- asks bConnection
liftIO $ E.getMessage con messageID
-- | Request the n most recent messages (similar to the 'E.SnapshotEvent').
messageLog :: Integer -> Bot b c ([E.Message], Maybe E.Snowflake)
messageLog n = do
con <- asks bConnection
liftIO $ E.messageLog con n Nothing
-- | Request the n messages preceding a certain message.
messageLogAfter :: E.Snowflake -> Integer -> Bot b c ([E.Message], Maybe E.Snowflake)
messageLogAfter messageID n = do
con <- asks bConnection
liftIO $ E.messageLog con n (Just messageID)
-- | Request a listing of all currently connected sessions.
who :: Bot b c [E.SessionView]
who = do
con <- asks bConnection
liftIO $ E.who con

View file

@ -5,19 +5,18 @@ import qualified System.Log.Handler as LH
import qualified System.Log.Handler.Simple as LH import qualified System.Log.Handler.Simple as LH
import qualified System.Log.Logger as L import qualified System.Log.Logger as L
import qualified EuphApi.Bot as E import qualified EuphApi.Bot as B
import qualified EuphApi.Connection as E
myBotConfig :: E.BotConfig () () myBotConfig :: B.BotConfig () ()
myBotConfig = E.BotConfig myBotConfig = B.BotConfig
{ E.botAddress = "euphoria.io" { B.botAddress = "euphoria.io"
, E.botRoom = "test" , B.botRoom = "test"
, E.botPassword = Nothing , B.botPassword = Nothing
, E.botNick = "EuphApiTestBot" , B.botNick = "EuphApiTestBot"
, E.botHandler = const $ return () , B.botHandler = const $ return ()
, E.botInfo = () , B.botInfo = ()
, E.botNewConnectionInfo = return () , B.botNewConnectionInfo = return ()
, E.botReconnectPolicy = E.defaultReconnectPolicy , B.botReconnectPolicy = B.defaultReconnectPolicy
} }
main = do main = do
@ -26,4 +25,4 @@ main = do
myFormattedHandler = LH.setFormatter myHandler myFormatter myFormattedHandler = LH.setFormatter myHandler myFormatter
L.updateGlobalLogger L.rootLoggerName (L.setHandlers [myFormattedHandler]) L.updateGlobalLogger L.rootLoggerName (L.setHandlers [myFormattedHandler])
L.updateGlobalLogger L.rootLoggerName (L.setLevel L.INFO) L.updateGlobalLogger L.rootLoggerName (L.setLevel L.INFO)
E.runBot myBotConfig B.runBot myBotConfig