1
1
module Main where
2
2
3
+ import Control.Arrow (right )
4
+ import Data.Text qualified as T
3
5
import Discord
4
6
import Discord.Interactions
5
7
import Discord.Internal.Rest.ApplicationCommands
6
8
import Discord.Types
7
-
8
- import Data.Text qualified as T
9
-
10
- import Control.Arrow (right )
11
9
import Flow ((.>) )
12
10
import Parser (parseRoll )
13
11
import RefTable (RefTable , maybeMakeRef , maybeUnRef , newRefTable )
12
+ import Response (Response , followUp , mkInteractionHandler , rc , rc_ , respond )
14
13
import Sample (rollIO )
15
14
import Stats (genReport )
16
- import Response (Response , respond , followUp , mkInteractionHandler , rc , rc_ )
17
15
18
16
main :: IO ()
19
17
main = do
@@ -34,35 +32,36 @@ main = do
34
32
35
33
coms :: [CreateApplicationCommand ]
36
34
coms =
37
- [ simpleCommand " r" " roll some dice" $ Just $
38
- OptionsValues
39
- [ exprOption
40
- , OptionValueInteger
41
- { optionValueName = " times"
42
- , optionValueLocalizedName = Nothing
43
- , optionValueDescription = " time number of times to roll it"
44
- , optionValueLocalizedDescription = Nothing
45
- , optionValueRequired = False
46
- , optionValueIntegerChoices = Left False
47
- , optionValueIntegerMinVal = Just 1
48
- , optionValueIntegerMaxVal = Nothing
49
- }
50
- ]
35
+ [ simpleCommand " r" " roll some dice" $
36
+ Just $
37
+ OptionsValues
38
+ [ exprOption
39
+ , OptionValueInteger
40
+ { optionValueName = " times"
41
+ , optionValueLocalizedName = Nothing
42
+ , optionValueDescription = " time number of times to roll it"
43
+ , optionValueLocalizedDescription = Nothing
44
+ , optionValueRequired = False
45
+ , optionValueIntegerChoices = Left False
46
+ , optionValueIntegerMinVal = Just 1
47
+ , optionValueIntegerMaxVal = Nothing
48
+ }
49
+ ]
51
50
, simpleCommand " stats" " get stats info" $
52
- Just $
53
- OptionsValues
54
- [ exprOption
55
- , OptionValueInteger
56
- { optionValueName = " result"
57
- , optionValueLocalizedName = Nothing
58
- , optionValueDescription = " the result"
59
- , optionValueLocalizedDescription = Nothing
60
- , optionValueRequired = True
61
- , optionValueIntegerChoices = Left False
62
- , optionValueIntegerMinVal = Nothing
63
- , optionValueIntegerMaxVal = Nothing
64
- }
65
- ]
51
+ Just $
52
+ OptionsValues
53
+ [ exprOption
54
+ , OptionValueInteger
55
+ { optionValueName = " result"
56
+ , optionValueLocalizedName = Nothing
57
+ , optionValueDescription = " the result"
58
+ , optionValueLocalizedDescription = Nothing
59
+ , optionValueRequired = True
60
+ , optionValueIntegerChoices = Left False
61
+ , optionValueIntegerMinVal = Nothing
62
+ , optionValueIntegerMaxVal = Nothing
63
+ }
64
+ ]
66
65
, simpleCommand " help" " send help text" Nothing
67
66
]
68
67
@@ -71,73 +70,80 @@ handler rt = \case
71
70
Ready _ _ _ _ _ _ (PartialApplication i _) -> do
72
71
putStrLn " ready"
73
72
oldComs <- rc $ GetGlobalApplicationCommands i
74
- let removedComs
75
- = Prelude. filter
76
- (\ c -> applicationCommandName c `notElem` (createName <$> coms))
77
- oldComs
73
+ let removedComs =
74
+ Prelude. filter
75
+ (\ c -> applicationCommandName c `notElem` (createName <$> coms))
76
+ oldComs
78
77
forM_ removedComs $ rc_ . DeleteGlobalApplicationCommand i . applicationCommandId
79
78
forM_ coms $ rc . CreateGlobalApplicationCommand i
80
79
putStrLn " commands registered"
81
80
InteractionCreate interaction ->
82
81
mkInteractionHandler interaction $
83
82
case interaction of
84
83
( InteractionApplicationCommand
85
- { applicationCommandData =
86
- ApplicationCommandDataChatInput
87
- { applicationCommandDataName = name
88
- , optionsData = options
89
- }
90
- }
84
+ { applicationCommandData =
85
+ ApplicationCommandDataChatInput
86
+ { applicationCommandDataName = name
87
+ , optionsData = options
88
+ }
89
+ }
91
90
) ->
92
91
case name of
93
- " help" ->
94
- respond
95
- $ interactionResponseBasic
96
- $ " /help prints this\n "
97
- <> " /r rolls an expression\n "
98
- <> helpText
99
- " stats" ->
100
- case options of
101
- (Just (OptionsDataValues
102
- [ OptionDataValueString _ (Right expr)
103
- , OptionDataValueInteger _ (Right result)
104
- ]
105
- )) -> stats (fromInteger result) expr
106
- _ -> putStrLn $ " Bad options for stats:" <> show options
107
- " r" ->
108
- case options of
109
- (Just (OptionsDataValues
110
- [ OptionDataValueString _ (Right expr)]
111
- )) -> rollExpr rt Nothing expr
112
- (Just (OptionsDataValues
113
- [ OptionDataValueString _ (Right expr)
114
- , OptionDataValueInteger _ (Right times)
115
- ]
116
- )) -> rollExpr rt (Just $ fromInteger times) expr
117
- _ -> putStrLn $ " Bad options for r: " <> show options
118
- com -> putStrLn $ " bad command: " <> show com
119
- ( InteractionComponent {componentData = ButtonData button }
120
- ) -> maybeUnRef rt button >>= \ case
121
- (T. stripPrefix " roll:" -> Just expr) -> do
122
- rollExpr rt Nothing expr
123
- (T. stripPrefix " rollt:" -> Just rest) -> do
124
- let (times', T. tail -> expr) = T. breakOn " :" rest
125
- case readMaybe $ toString times' of
126
- Just times -> do
127
- rollExpr rt (Just times) expr
128
- Nothing -> die " failed to parse times in rollt"
129
- (T. stripPrefix " logs:" -> Just logs) -> do
130
- respond $ interactionResponseBasic logs
131
- (T. stripPrefix " stats:" -> Just rest) -> do
132
- let (res', T. tail -> expr) = T. breakOn " ," rest
133
- res <- case readMaybe $ toString res' of
134
- Nothing -> die " failed to read res in stats"
135
- Just res -> pure res
136
- stats res expr
137
- (T. stripPrefix " err:" -> Just msg) ->
138
- respond $ interactionResponseBasic
139
- msg
140
- _ -> die $ toString $ " unexpected button data:" <> button
92
+ " help" ->
93
+ respond $
94
+ interactionResponseBasic $
95
+ " /help prints this\n "
96
+ <> " /r rolls an expression\n "
97
+ <> helpText
98
+ " stats" ->
99
+ case options of
100
+ ( Just
101
+ ( OptionsDataValues
102
+ [ OptionDataValueString _ (Right expr)
103
+ , OptionDataValueInteger _ (Right result)
104
+ ]
105
+ )
106
+ ) -> stats (fromInteger result) expr
107
+ _ -> putStrLn $ " Bad options for stats:" <> show options
108
+ " r" ->
109
+ case options of
110
+ ( Just
111
+ ( OptionsDataValues
112
+ [OptionDataValueString _ (Right expr)]
113
+ )
114
+ ) -> rollExpr rt Nothing expr
115
+ ( Just
116
+ ( OptionsDataValues
117
+ [ OptionDataValueString _ (Right expr)
118
+ , OptionDataValueInteger _ (Right times)
119
+ ]
120
+ )
121
+ ) -> rollExpr rt (Just $ fromInteger times) expr
122
+ _ -> putStrLn $ " Bad options for r: " <> show options
123
+ com -> putStrLn $ " bad command: " <> show com
124
+ (InteractionComponent {componentData = ButtonData button}) ->
125
+ maybeUnRef rt button >>= \ case
126
+ (T. stripPrefix " roll:" -> Just expr) -> do
127
+ rollExpr rt Nothing expr
128
+ (T. stripPrefix " rollt:" -> Just rest) -> do
129
+ let (times', T. tail -> expr) = T. breakOn " :" rest
130
+ case readMaybe $ toString times' of
131
+ Just times -> do
132
+ rollExpr rt (Just times) expr
133
+ Nothing -> die " failed to parse times in rollt"
134
+ (T. stripPrefix " logs:" -> Just logs) -> do
135
+ respond $ interactionResponseBasic logs
136
+ (T. stripPrefix " stats:" -> Just rest) -> do
137
+ let (res', T. tail -> expr) = T. breakOn " ," rest
138
+ res <- case readMaybe $ toString res' of
139
+ Nothing -> die " failed to read res in stats"
140
+ Just res -> pure res
141
+ stats res expr
142
+ (T. stripPrefix " err:" -> Just msg) ->
143
+ respond $
144
+ interactionResponseBasic
145
+ msg
146
+ _ -> die $ toString $ " unexpected button data:" <> button
141
147
i -> do
142
148
putStrLn " unhandled interaction"
143
149
print i
@@ -167,8 +173,9 @@ rollExpr :: RefTable -> Maybe Int -> Text -> Response ()
167
173
rollExpr rt times expr =
168
174
case parseRoll expr of
169
175
Left _ ->
170
- respond $ interactionResponseBasic
171
- $ " Failed to parse: " <> expr <> " \n\n " <> helpText
176
+ respond $
177
+ interactionResponseBasic $
178
+ " Failed to parse: " <> expr <> " \n\n " <> helpText
172
179
Right roll -> do
173
180
(res' :: Either Text (Text , Text )) <- case times of
174
181
Nothing -> rollIO roll <&> right (first (show @ Text ))
@@ -181,24 +188,30 @@ rollExpr rt times expr =
181
188
case times of
182
189
Nothing -> " roll:"
183
190
Just t -> " rollt:" <> show t <> " :"
184
- buttons <- mapM (uncurry $ genButton rt)
185
- [(" Reroll" ,rollPrefix <> expr)
186
- ,(" How?" ," logs:" <> (if logs == " " then " It was a constant." else logs))
187
- ,(" Stats" ," stats:" <> res <> " ," <> expr)
188
- ]
189
- respond
190
- $ InteractionResponseChannelMessage
191
- $ InteractionResponseMessage
192
- { interactionResponseMessageTTS = Nothing
193
- , interactionResponseMessageContent =
194
- Just $ expr <> " = **" <> res <> " **"
195
- , interactionResponseMessageEmbeds = Nothing
196
- , interactionResponseMessageAllowedMentions = Nothing
197
- , interactionResponseMessageFlags = Nothing
198
- , interactionResponseMessageComponents =
199
- Just [ ActionRowButtons buttons ]
200
- , interactionResponseMessageAttachments = Nothing
201
- }
191
+ buttons <-
192
+ mapM
193
+ (uncurry $ genButton rt)
194
+ [ (" Reroll" , rollPrefix <> expr)
195
+ ,
196
+ ( " How?"
197
+ , " logs:"
198
+ <> (if logs == " " then " It was a constant." else logs)
199
+ )
200
+ , (" Stats" , " stats:" <> res <> " ," <> expr)
201
+ ]
202
+ respond $
203
+ InteractionResponseChannelMessage $
204
+ InteractionResponseMessage
205
+ { interactionResponseMessageTTS = Nothing
206
+ , interactionResponseMessageContent =
207
+ Just $ expr <> " = **" <> res <> " **"
208
+ , interactionResponseMessageEmbeds = Nothing
209
+ , interactionResponseMessageAllowedMentions = Nothing
210
+ , interactionResponseMessageFlags = Nothing
211
+ , interactionResponseMessageComponents =
212
+ Just [ActionRowButtons buttons]
213
+ , interactionResponseMessageAttachments = Nothing
214
+ }
202
215
203
216
stats :: Int -> Text -> Response ()
204
217
stats res expr = do
@@ -232,16 +245,16 @@ genButton rt label msg = do
232
245
pure $ simpleButton msg' label
233
246
234
247
simpleCommand :: Text -> Text -> Maybe Options -> CreateApplicationCommand
235
- simpleCommand name desc opts=
248
+ simpleCommand name desc opts =
236
249
CreateApplicationCommandChatInput
237
- { createName = name
238
- , createLocalizedName = Nothing
239
- , createDescription = desc
240
- , createLocalizedDescription = Nothing
241
- , createOptions = opts
242
- , createDefaultMemberPermissions = Nothing
243
- , createDMPermission = Nothing
244
- }
250
+ { createName = name
251
+ , createLocalizedName = Nothing
252
+ , createDescription = desc
253
+ , createLocalizedDescription = Nothing
254
+ , createOptions = opts
255
+ , createDefaultMemberPermissions = Nothing
256
+ , createDMPermission = Nothing
257
+ }
245
258
246
259
exprOption :: OptionValue
247
260
exprOption =
0 commit comments