Skip to content

Commit 602a2a5

Browse files
committed
clean up
1 parent f4044c8 commit 602a2a5

File tree

7 files changed

+28
-54
lines changed

7 files changed

+28
-54
lines changed

app/Main.hs

+8-10
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,25 @@ main :: IO ()
3131
main = do
3232
setLocaleEncoding utf8
3333
--hSetBuffering stdout NoBuffering
34-
options <- execParser clOptionsParser
35-
config <- parseConfigFile $ cfgFile options
36-
b <- connect (T.unpack $ ircServer config) (ircPort config)
34+
config <- parseConfigFile . cfgFile =<< execParser clOptionsParser
35+
b <- connect (T.unpack $ ircServer config) (ircPort config)
3736
let db = Database (dbFile config)
38-
s <- getStdGen
39-
catFacts <- loadFacts $ factsFile config
40-
let opt = Options b config db catFacts
37+
s <- getStdGen
38+
opt <- Options b config db <$> loadFacts (factsFile config)
4139
bracket (pure opt) disconnect (loop s)
4240
where
43-
disconnect options = do
41+
disconnect opts = do
4442
putStrLn "disconnecting"
45-
hClose . botSocket . bot $ options
43+
hClose . botSocket . bot $ opts
4644
loop :: StdGen -> Options -> IO ()
47-
loop s options = do
45+
loop s opts = do
4846
_ <-
4947
flip runStateT messageQueue
5048
. flip runStateT simpleCommands
5149
. flip runStateT M.empty
5250
. flip runStateT s
5351
. flip runStateT M.empty
54-
. flip runReaderT options
52+
. flip runReaderT opts
5553
$ runApp run
5654
return ()
5755

src/Bot/Catfacts.hs

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import Bot.Random
55

66
import Control.Monad.Reader
77
import qualified Data.Array as A
8-
import qualified Data.Text as T (lines, null, pack, strip, unpack)
8+
import qualified Data.Text as T
99
import qualified Data.Text.IO as T (readFile)
10-
import System.IO hiding (hGetContents)
11-
import System.IO.Strict (hGetContents)
1210

1311
loadFacts :: StringType -> IO (A.Array Int StringType)
14-
loadFacts path = do
12+
loadFacts pth = do
1513
facts <- filter (not . T.null) . map T.strip . T.lines <$> T.readFile
16-
(T.unpack path)
14+
(T.unpack pth)
1715
return . A.listArray (0, pred . length $ facts) $ facts
1816

1917
randomFact :: (MonadIO m, RandomGenerator m, OptionsConfig m) => m StringType

src/Bot/Irc.hs

-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@ import Conc
66
import Bot
77
import Bot.Irc.Send
88
import Command.Commands
9-
import Conc
10-
import qualified Conc
119
import Control.Monad.Reader
12-
import Data.Function ((&))
1310
import qualified Data.Text as T
14-
(Text, drop, dropWhile, isPrefixOf, pack, strip, unpack, words)
1511
import qualified Data.Text.IO as T (hGetLine)
16-
import GHC.IO.Handle (Handle)
17-
import System.IO.Unsafe
1812

1913
clean :: StringType -> StringType
2014
clean = T.strip . T.drop 1 . T.dropWhile (/= ':') . T.drop 1

src/Bot/Irc/Send.hs

-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@ module Bot.Irc.Send
1111
where
1212

1313
import Bot
14-
import Bot.Options.Parse
15-
import Conc
1614

17-
import Control.Concurrent
18-
import Control.Concurrent.Async
1915
import Control.Monad.IO.Class
2016
import Control.Monad.Reader
2117
import System.Exit
22-
import System.IO
2318

2419
import qualified Data.Text as T
2520
import qualified Data.Text.IO as T

src/Command.hs

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ class (Monad m) =>
1515
putOnCooldown :: Command a -> User -> m ()
1616

1717
checkUserOnCooldown :: Command m -> User -> POSIXTime -> App Bool
18-
checkUserOnCooldown command user time =
18+
checkUserOnCooldown command usr time =
1919
if requireUserCooldown . options $ command
2020
then do
2121
userCooldowns <- userLift get
22-
case (> time) <$> M.lookup user userCooldowns of
22+
case (> time) <$> M.lookup usr userCooldowns of
2323
Nothing -> return False
2424
Just r -> return r
2525
else return False
2626

2727
checkCommandOnCooldown :: Command m -> User -> POSIXTime -> App Bool
28-
checkCommandOnCooldown command user time =
28+
checkCommandOnCooldown command usr time =
2929
if requireGlobalCooldown . options $ command
3030
then do
3131
commandCooldowns <- commandLift get
@@ -35,17 +35,17 @@ checkCommandOnCooldown command user time =
3535
else return False
3636

3737
instance CommandCooldownHandler App where
38-
isOnCooldown command user time = do
39-
u <- checkUserOnCooldown command user time
40-
c <- checkCommandOnCooldown command user time
38+
isOnCooldown command usr time = do
39+
u <- checkUserOnCooldown command usr time
40+
c <- checkCommandOnCooldown command usr time
4141
return $ not (u && c)
42-
putOnCooldown command user = do
42+
putOnCooldown command usr = do
4343
time <- liftIO getPOSIXTime
4444
commandLift $ modify $ M.insert
4545
(name command)
4646
((time +) $ globalCooldown . options $ command)
4747
userLift $ modify $ M.insert
48-
user
48+
usr
4949
((time +) $ userCooldown . options $ command)
5050

5151
class (Monad m) =>

src/Command/Commands.hs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{-# LANGUAGE TupleSections #-}
2-
{-# LANGUAGE DeriveGeneric #-}
3-
{-# LANGUAGE DeriveDataTypeable #-}
4-
{-# LANGUAGE Arrows #-}
52
{-# LANGUAGE OverloadedStrings #-}
63

74
module Command.Commands where
@@ -27,17 +24,17 @@ import Control.Monad.IO.Class
2724
import qualified Data.HashMap.Strict as M
2825

2926
runCommandM :: Message -> ConcM App ()
30-
runCommandM message@(Message text user) = do
27+
runCommandM message@(Message text usr) = do
3128
c <- pureM $ do
3229
cmd <- getCommand message
3330
case cmd of
3431
Nothing -> return Nothing
3532
Just command -> do
3633
time <- liftIO getPOSIXTime
37-
onCooldown <- isOnCooldown command user time
34+
onCooldown <- isOnCooldown command usr time
3835
if onCooldown
3936
then do
40-
putOnCooldown command user
37+
putOnCooldown command usr
4138
return $ Just $ action command message
4239
else return Nothing
4340
case c of
@@ -64,12 +61,12 @@ commandList =
6461
"facts"
6562
["!fact", "!f", "forsenScoots", "OMGScoots"]
6663
(CommandOptions 2 2 True True (const True))
67-
(randomFactCommand)
64+
randomFactCommand
6865
, Command
6966
"real facts"
7067
["!realfact", "!rf"]
7168
(CommandOptions 2 2 True True (const True))
72-
(randomFactCommand)
69+
randomFactCommand
7370
, Command
7471
"dubious facts"
7572
["!dubiousfact", "!df"]

src/MessageQueue.hs

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
{-# LANGUAGE Arrows #-}
2-
31
module MessageQueue where
42

53
import Bot
64
import Bot.Irc.Send
75
import Conc
86
import Control.Concurrent
97
import Control.Monad.State.Strict
10-
import Data.Function ((&))
118
import Data.Time.Clock.POSIX
129
import Queue
13-
import qualified Data.Text.IO as T
14-
import qualified Data.Text as T
15-
import Control.Monad.Reader
16-
import GHC.IO.Handle (Handle)
17-
import Control.Arrow
1810

1911
messageLift :: StateT MessageQueue IO a -> App a
2012
messageLift = App . lift . lift . lift . lift . lift
@@ -26,14 +18,14 @@ queueMessage message = messageLift
2618
messageDispensingLoopM :: ConcM App ()
2719
messageDispensingLoopM = do
2820
_ <- pureM $ do
29-
MessageQueue time queue <- messageLift get
30-
if isEmpty queue
21+
MessageQueue time q <- messageLift get
22+
if isEmpty q
3123
then return ()
3224
else do
3325
currentTime <- liftIO getPOSIXTime
3426
when (currentTime > time) $ do
35-
let (Just message, q) = pop queue
36-
messageLift . put $ MessageQueue (currentTime + 2) q
27+
let (Just message, q_new) = pop q
28+
messageLift . put $ MessageQueue (currentTime + 2) q_new
3729
privmsg message
3830
liftIO $ print $ currentTime > time
3931
_ <- taskM $ threadDelay 100000

0 commit comments

Comments
 (0)