@@ -10,19 +10,20 @@ module Bot.CustomCommand
1010 , timesCustomCommand
1111 ) where
1212
13+ import Bot.Expr
1314import Bot.Replies
14- import Bot.Variable
1515import Command
1616import Control.Monad
17- import Control.Monad.Trans.Class
1817import Control.Monad.Trans.Maybe
18+ import Data.Functor.Compose
1919import qualified Data.Map as M
2020import Data.Maybe
2121import Data.Proxy
2222import qualified Data.Text as T
2323import Data.Time
2424import Effect
2525import Entity
26+ import HyperNerd.Parser
2627import Property
2728import Reaction
2829import 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
173183expandCustomCommandVars ::
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
194210bumpCustomCommandTimes :: CustomCommand -> CustomCommand
195211bumpCustomCommandTimes customCommand =
@@ -199,19 +215,17 @@ replaceCustomCommandMessage :: T.Text -> CustomCommand -> CustomCommand
199215replaceCustomCommandMessage 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 . (,)
0 commit comments