@@ -12,7 +12,6 @@ module Bot.CustomCommand
1212 ) where
1313
1414import Bot.Replies
15- import Bot.Variable
1615import Command
1716import Control.Monad
1817import Control.Monad.Trans.Maybe
@@ -28,6 +27,8 @@ import Reaction
2827import Text.InterpolatedString.QM
2928import Transport
3029import Data.Functor.Compose
30+ import HyperNerd.Parser
31+ import Bot.Expr
3132
3233data CustomCommand = CustomCommand
3334 { customCommandName :: T. Text
@@ -170,29 +171,38 @@ updateCustomCommand builtinCommands =
170171 (Nothing , Nothing ) ->
171172 replyToSender sender [qms |Command '{name}' does not exist|]
172173
174+ expandVars :: M. Map T. Text T. Text -> [Expr ] -> T. Text
175+ expandVars _ [] = " "
176+ expandVars vars (TextExpr t: rest) = t <> expandVars vars rest
177+ expandVars vars (FunCallExpr funame _: rest) =
178+ (fromMaybe " " $ M. lookup funame vars) <> expandVars vars rest
179+
173180-- TODO(#598): reimplement expandCustomCommandVars with Bot.Expr when it's ready
174181expandCustomCommandVars ::
175- Message (Command T. Text , Entity CustomCommand ) -> Effect CustomCommand
182+ Message (Command T. Text , Entity CustomCommand ) -> Effect ( Either String CustomCommand )
176183expandCustomCommandVars Message { messageSender = sender
177184 , messageContent = (Command {commandArgs = args}, Entity {entityPayload = customCommand})
178185 } = do
179186 timestamp <- now
180187 let day = utctDay timestamp
181188 let (yearNum, monthNum, dayNum) = toGregorian day
182- let message = customCommandMessage customCommand
189+ let code = runParser exprs $ customCommandMessage customCommand
183190 let times = customCommandTimes customCommand
184191 let vars =
185- [ (" %times" , [qms |{times}|])
186- , (" %year" , [qms |{yearNum}|])
187- , (" %month" , [qms |{monthNum}|])
188- , (" %day" , [qms |{dayNum}|])
189- , (" %date" , [qms |{showGregorian day}|])
190- , (" %sender" , mentionSender sender)
191- , (" %1" , args)
192- ]
193- expandedMessage <-
194- expandVariables $ foldl (flip $ uncurry T. replace) message vars
195- return $ customCommand {customCommandMessage = expandedMessage}
192+ M. fromList
193+ [ (" times" , [qms |{times}|])
194+ , (" year" , [qms |{yearNum}|])
195+ , (" month" , [qms |{monthNum}|])
196+ , (" day" , [qms |{dayNum}|])
197+ , (" date" , [qms |{showGregorian day}|])
198+ , (" sender" , mentionSender sender)
199+ , (" 1" , args)
200+ ]
201+ case code of
202+ Left msg -> return $ Left (show msg)
203+ Right (_, code') -> do
204+ return $
205+ Right customCommand {customCommandMessage = expandVars vars code'}
196206
197207bumpCustomCommandTimes :: CustomCommand -> CustomCommand
198208bumpCustomCommandTimes customCommand =
@@ -211,7 +221,8 @@ dispatchCustomCommand =
211221 liftR (updateEntityById . fmap bumpCustomCommandTimes) $
212222 ignoreNothing $
213223 transR getCompose $
214- dupLiftR expandCustomCommandVars $ cmapR customCommandMessage $ sayMessage
224+ dupLiftR expandCustomCommandVars $
225+ replyLeft $ cmapR customCommandMessage $ sayMessage
215226 where
216227 f :: Functor m => (a , m b ) -> m (a , b )
217228 f = uncurry $ fmap . (,)
0 commit comments