Skip to content

Commit a57eb40

Browse files
authored
Merge pull request #812 from tsoding/598
(#598) Reimplement dispatchCustomCommand with Reaction API
2 parents 14a4a1a + 1768972 commit a57eb40

File tree

4 files changed

+73
-51
lines changed

4 files changed

+73
-51
lines changed

src/Bot.hs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ dispatchRedirect :: Effect () -> Message (Command T.Text) -> Effect ()
668668
dispatchRedirect effect cmd = do
669669
effectOutput <-
670670
T.strip . T.concat . concatMap (\x -> [" ", x]) <$> listen effect
671-
dispatchCommand $
671+
runReaction dispatchCommand $
672672
getCompose ((\x -> T.concat [x, effectOutput]) <$> Compose cmd)
673673

674674
-- TODO(#414): there is not cooldown for pipes
@@ -699,18 +699,18 @@ dispatchPipe = Reaction dispatchPipe'
699699
pipeLimit = 10
700700
plebPipeLimit = 2
701701

702-
dispatchCommand :: Message (Command T.Text) -> Effect ()
703-
dispatchCommand message = do
704-
dispatchBuiltinCommand message
705-
dispatchCustomCommand message
702+
dispatchCommand :: Reaction Message (Command T.Text)
703+
dispatchCommand = dispatchBuiltinCommand <> dispatchCustomCommand
706704

707-
dispatchBuiltinCommand :: Message (Command T.Text) -> Effect ()
708-
dispatchBuiltinCommand message@Message { messageSender = _
709-
, messageContent = Command { commandName = name
710-
, commandArgs = args
711-
}
712-
} =
713-
maybe
714-
(return ())
715-
(\bc -> runReaction (bcReaction bc) $ fmap (const args) message)
716-
(M.lookup name builtinCommands)
705+
dispatchBuiltinCommand :: Reaction Message (Command T.Text)
706+
dispatchBuiltinCommand = Reaction f
707+
where
708+
f message@Message { messageSender = _
709+
, messageContent = Command { commandName = name
710+
, commandArgs = args
711+
}
712+
} =
713+
maybe
714+
(return ())
715+
(\bc -> runReaction (bcReaction bc) $ fmap (const args) message)
716+
(M.lookup name builtinCommands)

src/Bot/CustomCommand.hs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ module Bot.CustomCommand
1010
, timesCustomCommand
1111
) where
1212

13+
import Bot.Expr
1314
import Bot.Replies
14-
import Bot.Variable
1515
import Command
1616
import Control.Monad
17-
import Control.Monad.Trans.Class
1817
import Control.Monad.Trans.Maybe
18+
import Data.Functor.Compose
1919
import qualified Data.Map as M
2020
import Data.Maybe
2121
import Data.Proxy
2222
import qualified Data.Text as T
2323
import Data.Time
2424
import Effect
2525
import Entity
26+
import HyperNerd.Parser
2627
import Property
2728
import Reaction
2829
import Text.InterpolatedString.QM
@@ -169,27 +170,42 @@ updateCustomCommand builtinCommands =
169170
(Nothing, Nothing) ->
170171
replyToSender sender [qms|Command '{name}' does not exist|]
171172

173+
evalExpr :: M.Map T.Text T.Text -> Expr -> T.Text
174+
evalExpr _ (TextExpr t) = t
175+
evalExpr vars (FunCallExpr "or" args) =
176+
fromMaybe "" $ listToMaybe $ dropWhile T.null $ map (evalExpr vars) args
177+
evalExpr vars (FunCallExpr funame _) = fromMaybe "" $ M.lookup funame vars
178+
179+
expandVars :: M.Map T.Text T.Text -> [Expr] -> T.Text
180+
expandVars vars = T.concat . map (evalExpr vars)
181+
172182
-- TODO(#598): reimplement expandCustomCommandVars with Bot.Expr when it's ready
173183
expandCustomCommandVars ::
174-
Sender -> T.Text -> CustomCommand -> Effect CustomCommand
175-
expandCustomCommandVars sender args customCommand = do
184+
Message (Command T.Text, Entity CustomCommand)
185+
-> Effect (Either String CustomCommand)
186+
expandCustomCommandVars Message { messageSender = sender
187+
, messageContent = (Command {commandArgs = args}, Entity {entityPayload = customCommand})
188+
} = do
176189
timestamp <- now
177190
let day = utctDay timestamp
178191
let (yearNum, monthNum, dayNum) = toGregorian day
179-
let message = customCommandMessage customCommand
192+
let code = runParser exprs $ customCommandMessage customCommand
180193
let times = customCommandTimes customCommand
181194
let vars =
182-
[ ("%times", [qms|{times}|])
183-
, ("%year", [qms|{yearNum}|])
184-
, ("%month", [qms|{monthNum}|])
185-
, ("%day", [qms|{dayNum}|])
186-
, ("%date", [qms|{showGregorian day}|])
187-
, ("%sender", mentionSender sender)
188-
, ("%1", args)
189-
]
190-
expandedMessage <-
191-
expandVariables $ foldl (flip $ uncurry T.replace) message vars
192-
return $ customCommand {customCommandMessage = expandedMessage}
195+
M.fromList
196+
[ ("times", [qms|{times}|])
197+
, ("year", [qms|{yearNum}|])
198+
, ("month", [qms|{monthNum}|])
199+
, ("day", [qms|{dayNum}|])
200+
, ("date", [qms|{showGregorian day}|])
201+
, ("sender", mentionSender sender)
202+
, ("1", args)
203+
]
204+
case code of
205+
Left msg -> return $ Left (show msg)
206+
Right (_, code') ->
207+
return $
208+
Right customCommand {customCommandMessage = expandVars vars code'}
193209

194210
bumpCustomCommandTimes :: CustomCommand -> CustomCommand
195211
bumpCustomCommandTimes customCommand =
@@ -199,19 +215,17 @@ replaceCustomCommandMessage :: T.Text -> CustomCommand -> CustomCommand
199215
replaceCustomCommandMessage message customCommand =
200216
customCommand {customCommandMessage = message}
201217

202-
dispatchCustomCommand :: Message (Command T.Text) -> Effect ()
203-
dispatchCustomCommand Message { messageContent = Command { commandName = cmd
204-
, commandArgs = args
205-
}
206-
, messageSender = sender
207-
} = do
208-
customCommand <-
209-
runMaybeT
210-
(entityPayload <$>
211-
((fmap bumpCustomCommandTimes <$> customCommandByName cmd) >>=
212-
MaybeT . updateEntityById) >>=
213-
lift . expandCustomCommandVars sender args)
214-
maybe
215-
(return ())
216-
(say (senderChannel sender) . customCommandMessage)
217-
customCommand
218+
dispatchCustomCommand :: Reaction Message (Command T.Text)
219+
dispatchCustomCommand =
220+
liftFst (runMaybeT . customCommandByName . commandName) $
221+
cmapR f $
222+
ignoreNothing $
223+
transR Compose $
224+
liftR (updateEntityById . fmap bumpCustomCommandTimes) $
225+
ignoreNothing $
226+
transR getCompose $
227+
dupLiftR expandCustomCommandVars $
228+
replyLeft $ cmapR customCommandMessage sayMessage
229+
where
230+
f :: Functor m => (a, m b) -> m (a, b)
231+
f = uncurry $ fmap . (,)

src/Bot/Periodic.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ getPeriodicCommandByName name =
8484
Take 1 $ Filter (PropertyEquals "name" (PropertyText name)) All
8585

8686
startPeriodicTimer ::
87-
(Message (Command T.Text) -> Effect ()) -> Channel -> Int -> Effect ()
87+
Reaction Message (Command T.Text) -> Channel -> Int -> Effect ()
8888
startPeriodicTimer dispatchCommand channel eid =
8989
periodicEffect' (Just channel) $ do
9090
pt' <- getEntityById Proxy eid
@@ -99,15 +99,15 @@ startPeriodicTimer dispatchCommand channel eid =
9999
when (periodicTimerEnabled pt) $
100100
maybe
101101
(return ())
102-
(dispatchCommand .
102+
(runReaction dispatchCommand .
103103
Message (mrbotka {senderChannel = channel}) False .
104104
periodicCommand . entityPayload)
105105
pc'
106106
return $ Just $ fromIntegral $ periodicTimerPeriod pt)
107107
pt'
108108

109109
startPeriodicCommands ::
110-
Channel -> (Message (Command T.Text) -> Effect ()) -> Effect ()
110+
Channel -> Reaction Message (Command T.Text) -> Effect ()
111111
startPeriodicCommands channel dispatchCommand = do
112112
eids <- (entityId <$>) <$> selectEntities (Proxy :: Proxy PeriodicTimer) All
113113
for_ eids (startPeriodicTimer dispatchCommand channel)
@@ -171,7 +171,7 @@ statusPeriodicTimerCommand =
171171
Reaction replyMessage
172172

173173
addPeriodicTimerCommand ::
174-
(Message (Command T.Text) -> Effect ()) -> Reaction Message Int
174+
Reaction Message (Command T.Text) -> Reaction Message Int
175175
addPeriodicTimerCommand dispatchCommand =
176176
cmapR (PeriodicTimer False) $
177177
liftR (createEntity Proxy) $

src/Reaction.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE TupleSections #-}
2+
13
module Reaction where
24

35
import Data.Functor
@@ -74,3 +76,9 @@ ifR predicate thenReaction elseReaction =
7476
if predicate $ extract x
7577
then runReaction thenReaction x
7678
else runReaction elseReaction x
79+
80+
liftFst :: Comonad w => (a -> Effect b) -> Reaction w (a, b) -> Reaction w a
81+
liftFst f r =
82+
Reaction $ \m -> do
83+
b <- f $ extract m
84+
runReaction r ((, b) <$> m)

0 commit comments

Comments
 (0)