Skip to content

Commit 047ee9f

Browse files
committed
(#598) Implement expandVars in terms of Bot.Expr
1 parent d97c61d commit 047ee9f

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

src/Bot/CustomCommand.hs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module Bot.CustomCommand
1212
) where
1313

1414
import Bot.Replies
15-
import Bot.Variable
1615
import Command
1716
import Control.Monad
1817
import Control.Monad.Trans.Maybe
@@ -28,6 +27,8 @@ import Reaction
2827
import Text.InterpolatedString.QM
2928
import Transport
3029
import Data.Functor.Compose
30+
import HyperNerd.Parser
31+
import Bot.Expr
3132

3233
data 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
174181
expandCustomCommandVars ::
175-
Message (Command T.Text, Entity CustomCommand) -> Effect CustomCommand
182+
Message (Command T.Text, Entity CustomCommand) -> Effect (Either String CustomCommand)
176183
expandCustomCommandVars 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

197207
bumpCustomCommandTimes :: CustomCommand -> CustomCommand
198208
bumpCustomCommandTimes 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

Comments
 (0)