Skip to content

Commit a80df0f

Browse files
committed
(#598) make dispatchCommand a Reaction
1 parent 14a4a1a commit a80df0f

3 files changed

Lines changed: 37 additions & 35 deletions

File tree

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: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import Property
2727
import Reaction
2828
import Text.InterpolatedString.QM
2929
import Transport
30+
-- import HyperNerd.Parser
31+
-- import Bot.Expr
3032

3133
data CustomCommand = CustomCommand
3234
{ customCommandName :: T.Text
@@ -199,19 +201,19 @@ replaceCustomCommandMessage :: T.Text -> CustomCommand -> CustomCommand
199201
replaceCustomCommandMessage message customCommand =
200202
customCommand {customCommandMessage = message}
201203

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
204+
dispatchCustomCommand :: Reaction Message (Command T.Text)
205+
dispatchCustomCommand = Reaction f
206+
where
207+
f Message { messageContent = Command {commandName = cmd, commandArgs = args}
208+
, messageSender = sender
209+
} = do
210+
customCommand <-
211+
runMaybeT
212+
(entityPayload <$>
213+
((fmap bumpCustomCommandTimes <$> customCommandByName cmd) >>=
214+
MaybeT . updateEntityById) >>=
215+
lift . expandCustomCommandVars sender args)
216+
maybe
217+
(return ())
218+
(say (senderChannel sender) . customCommandMessage)
219+
customCommand

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) $

0 commit comments

Comments
 (0)