Skip to content

Commit d97c61d

Browse files
committed
(#598) Reimplement dispatchCustomCommand with Reaction API
1 parent a80df0f commit d97c61d

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

src/Bot/CustomCommand.hs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE OverloadedStrings #-}
22
{-# LANGUAGE QuasiQuotes #-}
3+
{-# LANGUAGE TupleSections #-}
34

45
module Bot.CustomCommand
56
( addCustomCommand
@@ -14,7 +15,6 @@ import Bot.Replies
1415
import Bot.Variable
1516
import Command
1617
import Control.Monad
17-
import Control.Monad.Trans.Class
1818
import Control.Monad.Trans.Maybe
1919
import qualified Data.Map as M
2020
import Data.Maybe
@@ -27,8 +27,7 @@ import Property
2727
import Reaction
2828
import Text.InterpolatedString.QM
2929
import Transport
30-
-- import HyperNerd.Parser
31-
-- import Bot.Expr
30+
import Data.Functor.Compose
3231

3332
data CustomCommand = CustomCommand
3433
{ customCommandName :: T.Text
@@ -173,8 +172,10 @@ updateCustomCommand builtinCommands =
173172

174173
-- TODO(#598): reimplement expandCustomCommandVars with Bot.Expr when it's ready
175174
expandCustomCommandVars ::
176-
Sender -> T.Text -> CustomCommand -> Effect CustomCommand
177-
expandCustomCommandVars sender args customCommand = do
175+
Message (Command T.Text, Entity CustomCommand) -> Effect CustomCommand
176+
expandCustomCommandVars Message { messageSender = sender
177+
, messageContent = (Command {commandArgs = args}, Entity {entityPayload = customCommand})
178+
} = do
178179
timestamp <- now
179180
let day = utctDay timestamp
180181
let (yearNum, monthNum, dayNum) = toGregorian day
@@ -202,18 +203,15 @@ replaceCustomCommandMessage message customCommand =
202203
customCommand {customCommandMessage = message}
203204

204205
dispatchCustomCommand :: Reaction Message (Command T.Text)
205-
dispatchCustomCommand = Reaction f
206+
dispatchCustomCommand =
207+
liftFst (runMaybeT . customCommandByName . commandName) $
208+
cmapR f $
209+
ignoreNothing $
210+
transR Compose $
211+
liftR (updateEntityById . fmap bumpCustomCommandTimes) $
212+
ignoreNothing $
213+
transR getCompose $
214+
dupLiftR expandCustomCommandVars $ cmapR customCommandMessage $ sayMessage
206215
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
216+
f :: Functor m => (a, m b) -> m (a, b)
217+
f = uncurry $ fmap . (,)

src/Reaction.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE TupleSections #-}
12
module Reaction where
23

34
import Data.Functor
@@ -74,3 +75,9 @@ ifR predicate thenReaction elseReaction =
7475
if predicate $ extract x
7576
then runReaction thenReaction x
7677
else runReaction elseReaction x
78+
79+
liftFst :: Comonad w => (a -> Effect b) -> Reaction w (a, b) -> Reaction w a
80+
liftFst f r =
81+
Reaction $ \m -> do
82+
b <- f $ extract m
83+
runReaction r ((, b) <$> m)

0 commit comments

Comments
 (0)