Add proper !uptime printing

This commit is contained in:
Joscha 2018-02-19 22:38:26 +00:00
parent 8024285e2e
commit b2aee83b67
3 changed files with 49 additions and 9 deletions

View file

@ -341,7 +341,7 @@ handleOwnViewStuff (E.HelloEvent view _ _) = do
liftIO $ atomically $ writeTVar var (Just view)
handleOwnViewStuff (E.SnapshotEvent _ _ _ (Just curNick)) = do
var <- asks bOwnView
liftIO $ debugM $ "SnapshotEvent reported a nick. This should not happen in a bot."
liftIO $ debugM "SnapshotEvent reported a nick. This should not happen in a bot."
liftIO $ atomically $ changeOwnNick var curNick
handleOwnViewStuff _ = return ()

View file

@ -8,6 +8,10 @@ module EuphApi.Utils (
, atMention
, mentionReduce
, similar
-- * Time manipulation
, printUTCTime
, printNominalDiffTime
, printUptime
-- * Commands
, Command
, CommandName
@ -31,6 +35,7 @@ import Data.Function
import Data.Void
import qualified Data.Text as T
import Data.Time
import qualified Text.Megaparsec as P
import qualified Text.Megaparsec.Char as P
@ -64,6 +69,35 @@ mentionReduce = T.map toLower . mention
similar :: T.Text -> T.Text -> Bool
similar = (==) `on` mentionReduce
{-
- Time manipulation
-}
printUTCTime :: UTCTime -> String
printUTCTime = formatTime defaultTimeLocale "%F %T %Z"
printNominalDiffTime :: NominalDiffTime -> String
printNominalDiffTime n =
let nr = abs $ round n :: Integer
(w, wr) = nr `quotRem` (60 * 60 * 24 * 7)
(d, dr) = wr `quotRem` (60 * 60 * 24 )
(h, hr) = dr `quotRem` (60 * 60 )
(m, s ) = hr `quotRem` 60
ws = if w /= 0 then show w ++ "w " else ""
ds = if d /= 0 then show d ++ "d " else ""
hs = if h /= 0 then show h ++ "h " else ""
ms = if m /= 0 then show m ++ "m " else ""
ss = show s ++ "s"
sign = if n < 0 then "-" else ""
in sign ++ ws ++ ds ++ hs ++ ms ++ ss
printUptime :: UTCTime -> UTCTime -> String
printUptime start now =
let diff = diffUTCTime now start
upSince = printUTCTime start
upFor = printNominalDiffTime diff
in "/me has been up since " ++ upSince ++ " (" ++ upFor ++ ")."
{-
- Commands
-}

View file

@ -11,8 +11,10 @@ module EuphApi.Utils.Botrulez
) where
import Control.Monad
import Control.Monad.IO.Class
import qualified Data.Text as T
import Data.Time
import qualified EuphApi.Bot as B
import qualified EuphApi.Types as E
@ -34,13 +36,17 @@ generalHelpCommand :: T.Text -> E.Command b c
generalHelpCommand helpText = E.command "help" $ \msg ->
void $ B.reply (E.msgID msg) helpText
uptime :: E.Message -> B.Bot b c ()
uptime msg = do
startTime <- B.getStartTime
curTime <- liftIO getCurrentTime
void $ B.reply (E.msgID msg) (T.pack $ E.printUptime startTime curTime)
uptimeCommand :: E.Command b c
uptimeCommand = E.specificCommand "uptime" $ \msg ->
void $ B.reply (E.msgID msg) "uptime placeholder"
uptimeCommand = E.specificCommand "uptime" uptime
generalUptimeCommand :: E.Command b c
generalUptimeCommand = E.command "uptime" $ \msg ->
void $ B.reply (E.msgID msg) "uptime placeholder"
generalUptimeCommand = E.command "uptime" uptime
killCommand :: E.Command b c
killCommand = E.specificCommand "kill" $ \msg -> do