Skip to content

Commit 302b5bd

Browse files
committed
Update custom commands in help after they changed
1 parent 6fa1497 commit 302b5bd

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

src/Bot/CustomCommand.hs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ module Bot.CustomCommand
88
, updateCustomCommand
99
, showCustomCommand
1010
, timesCustomCommand
11-
, CustomCommand(..)
1211
) where
1312

13+
import Bot.CustomCommandType
1414
import Bot.Expr
1515
import Bot.Flip
16+
import Bot.Help
1617
import Bot.Replies
1718
import Command
1819
import Control.Monad
@@ -32,35 +33,16 @@ import Reaction
3233
import Text.InterpolatedString.QM
3334
import Transport
3435

35-
data CustomCommand = CustomCommand
36-
{ customCommandName :: T.Text
37-
, customCommandMessage :: T.Text
38-
, customCommandTimes :: Int
39-
}
40-
41-
instance IsEntity CustomCommand where
42-
nameOfEntity _ = "CustomCommand"
43-
toProperties customCommand =
44-
M.fromList
45-
[ ("name", PropertyText $ customCommandName customCommand)
46-
, ("message", PropertyText $ customCommandMessage customCommand)
47-
, ("times", PropertyInt $ customCommandTimes customCommand)
48-
]
49-
fromProperties properties =
50-
CustomCommand <$> extractProperty "name" properties <*>
51-
extractProperty "message" properties <*>
52-
pure (fromMaybe 0 $ extractProperty "times" properties)
53-
5436
customCommandByName :: T.Text -> MaybeT Effect (Entity CustomCommand)
5537
customCommandByName name =
5638
MaybeT $
5739
fmap listToMaybe $
5840
selectEntities Proxy $ Filter (PropertyEquals "name" $ PropertyText name) All
5941

60-
-- TODO(#815): CRUD custom command should update help page now they're listed there as well.
6142
addCustomCommand :: CommandTable -> Reaction Message (T.Text, T.Text)
6243
addCustomCommand builtinCommands =
63-
Reaction $ \Message {messageSender = sender, messageContent = (name, message)} -> do
44+
Reaction $ \mesg@Message {messageSender = sender, messageContent = (name, message)} -> do
45+
runReaction refreshHelpGistId mesg
6446
customCommand <- runMaybeT $ customCommandByName name
6547
let builtinCommand = M.lookup name builtinCommands
6648
case (customCommand, builtinCommand) of
@@ -85,7 +67,8 @@ addCustomCommand builtinCommands =
8567

8668
deleteCustomCommand :: CommandTable -> Reaction Message T.Text
8769
deleteCustomCommand builtinCommands =
88-
Reaction $ \Message {messageSender = sender, messageContent = name} -> do
70+
Reaction $ \mesg@Message {messageSender = sender, messageContent = name} -> do
71+
runReaction refreshHelpGistId mesg
8972
customCommand <- runMaybeT $ customCommandByName name
9073
let builtinCommand = M.lookup name builtinCommands
9174
case (customCommand, builtinCommand) of
@@ -155,7 +138,8 @@ timesCustomCommand builtinCommands =
155138

156139
updateCustomCommand :: CommandTable -> Reaction Message (T.Text, T.Text)
157140
updateCustomCommand builtinCommands =
158-
Reaction $ \Message {messageSender = sender, messageContent = (name, message)} -> do
141+
Reaction $ \mesg@Message {messageSender = sender, messageContent = (name, message)} -> do
142+
runReaction refreshHelpGistId mesg
159143
customCommand <- runMaybeT $ customCommandByName name
160144
let builtinCommand = M.lookup name builtinCommands
161145
case (customCommand, builtinCommand) of

src/Bot/CustomCommandType.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE QuasiQuotes #-}
3+
4+
-- | Moved out of CustomCommand to break dependency cycle:
5+
-- Help depends on this type, but custom commands needs
6+
-- to refresh Help and therefore also this type.
7+
module Bot.CustomCommandType
8+
(CustomCommand(..)
9+
) where
10+
11+
import qualified Data.Map as M
12+
import Data.Maybe
13+
import qualified Data.Text as T
14+
import Entity
15+
import Property
16+
17+
data CustomCommand = CustomCommand
18+
{ customCommandName :: T.Text
19+
, customCommandMessage :: T.Text
20+
, customCommandTimes :: Int
21+
}
22+
23+
instance IsEntity CustomCommand where
24+
nameOfEntity _ = "CustomCommand"
25+
toProperties customCommand =
26+
M.fromList
27+
[ ("name", PropertyText $ customCommandName customCommand)
28+
, ("message", PropertyText $ customCommandMessage customCommand)
29+
, ("times", PropertyInt $ customCommandTimes customCommand)
30+
]
31+
fromProperties properties =
32+
CustomCommand <$> extractProperty "name" properties <*>
33+
extractProperty "message" properties <*>
34+
pure (fromMaybe 0 $ extractProperty "times" properties)

src/Bot/Help.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Bot.Help
88
, startRefreshHelpGistTimer
99
) where
1010

11-
import Bot.CustomCommand
11+
import Bot.CustomCommandType
1212
import Bot.GitHub
1313
import Bot.Replies
1414
import Command

0 commit comments

Comments
 (0)