11{-# LANGUAGE OverloadedStrings #-}
22{-# LANGUAGE QuasiQuotes #-}
3+ {-# LANGUAGE TupleSections #-}
34
45module Bot.CustomCommand
56 ( addCustomCommand
@@ -14,7 +15,6 @@ import Bot.Replies
1415import Bot.Variable
1516import Command
1617import Control.Monad
17- import Control.Monad.Trans.Class
1818import Control.Monad.Trans.Maybe
1919import qualified Data.Map as M
2020import Data.Maybe
@@ -27,8 +27,7 @@ import Property
2727import Reaction
2828import Text.InterpolatedString.QM
2929import Transport
30- -- import HyperNerd.Parser
31- -- import Bot.Expr
30+ import Data.Functor.Compose
3231
3332data 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
175174expandCustomCommandVars ::
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
204205dispatchCustomCommand :: 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 . (,)
0 commit comments