Add proper !uptime printing
This commit is contained in:
parent
8024285e2e
commit
b2aee83b67
3 changed files with 49 additions and 9 deletions
|
|
@ -341,7 +341,7 @@ handleOwnViewStuff (E.HelloEvent view _ _) = do
|
||||||
liftIO $ atomically $ writeTVar var (Just view)
|
liftIO $ atomically $ writeTVar var (Just view)
|
||||||
handleOwnViewStuff (E.SnapshotEvent _ _ _ (Just curNick)) = do
|
handleOwnViewStuff (E.SnapshotEvent _ _ _ (Just curNick)) = do
|
||||||
var <- asks bOwnView
|
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
|
liftIO $ atomically $ changeOwnNick var curNick
|
||||||
handleOwnViewStuff _ = return ()
|
handleOwnViewStuff _ = return ()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,10 @@ module EuphApi.Utils (
|
||||||
, atMention
|
, atMention
|
||||||
, mentionReduce
|
, mentionReduce
|
||||||
, similar
|
, similar
|
||||||
|
-- * Time manipulation
|
||||||
|
, printUTCTime
|
||||||
|
, printNominalDiffTime
|
||||||
|
, printUptime
|
||||||
-- * Commands
|
-- * Commands
|
||||||
, Command
|
, Command
|
||||||
, CommandName
|
, CommandName
|
||||||
|
|
@ -31,6 +35,7 @@ import Data.Function
|
||||||
import Data.Void
|
import Data.Void
|
||||||
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import Data.Time
|
||||||
import qualified Text.Megaparsec as P
|
import qualified Text.Megaparsec as P
|
||||||
import qualified Text.Megaparsec.Char 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 :: T.Text -> T.Text -> Bool
|
||||||
similar = (==) `on` mentionReduce
|
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
|
- Commands
|
||||||
-}
|
-}
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,14 @@ module EuphApi.Utils.Botrulez
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
|
import Control.Monad.IO.Class
|
||||||
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import Data.Time
|
||||||
|
|
||||||
import qualified EuphApi.Bot as B
|
import qualified EuphApi.Bot as B
|
||||||
import qualified EuphApi.Types as E
|
import qualified EuphApi.Types as E
|
||||||
import qualified EuphApi.Utils as E
|
import qualified EuphApi.Utils as E
|
||||||
|
|
||||||
pingCommand :: E.Command b c
|
pingCommand :: E.Command b c
|
||||||
pingCommand = E.specificCommand "ping" $ \msg ->
|
pingCommand = E.specificCommand "ping" $ \msg ->
|
||||||
|
|
@ -34,13 +36,17 @@ generalHelpCommand :: T.Text -> E.Command b c
|
||||||
generalHelpCommand helpText = E.command "help" $ \msg ->
|
generalHelpCommand helpText = E.command "help" $ \msg ->
|
||||||
void $ B.reply (E.msgID msg) helpText
|
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.Command b c
|
||||||
uptimeCommand = E.specificCommand "uptime" $ \msg ->
|
uptimeCommand = E.specificCommand "uptime" uptime
|
||||||
void $ B.reply (E.msgID msg) "uptime placeholder"
|
|
||||||
|
|
||||||
generalUptimeCommand :: E.Command b c
|
generalUptimeCommand :: E.Command b c
|
||||||
generalUptimeCommand = E.command "uptime" $ \msg ->
|
generalUptimeCommand = E.command "uptime" uptime
|
||||||
void $ B.reply (E.msgID msg) "uptime placeholder"
|
|
||||||
|
|
||||||
killCommand :: E.Command b c
|
killCommand :: E.Command b c
|
||||||
killCommand = E.specificCommand "kill" $ \msg -> do
|
killCommand = E.specificCommand "kill" $ \msg -> do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue