Skip to content

Commit fd9137a

Browse files
authored
Merge pull request #152 from Bongo50/stats-timeout
Making the timeout for the dice stats command an environmental variable
2 parents d9e6dc5 + bef80dc commit fd9137a

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ SUPERUSER_GROUP=147258369147258369
99
SERVER_ID=314159265358979323
1010
ALLOW_GIT_UPDATE=False
1111
EMOJI_SERVERS=[121213131414151516]
12+
STATS_TIMEOUT=20
1213
# NOTE: YOU MUST HAVE A NEWLINE AT THE END OF THE FILE

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Create a `.env` file containing the following keys. Consult `.env.example` if yo
2626
* `SERVER_ID` (optional) - either `global` or the id of the server the bot will mainly be deployed in. Application commands will be
2727
registered here. If absent, application commands won't be registered.
2828
* `EMOJI_SERVERS` (optional) - a list of server IDs that the bot will search for emoji within.
29+
* `STATS_TIMEOUT` (optional) - an integer value that determines the maximum number of seconds that the bot will perform dice stats calculations for before timing out.
2930
* `ALLOW_GIT_UPDATE` (optional) - a `true` or `false` value that determines whether the bot can automatically load data from the repository.
3031
**Warning!** Be very careful with setting this to true; if you haven't set up permissions properly on your repo and your discord servers then things can go wrong!
3132

src/Tablebot/Plugins/Roll/Plugin.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Discord.Interactions
2424
)
2525
import Discord.Internal.Rest.Channel (ChannelRequest (..), MessageDetailedOpts (..))
2626
import Discord.Types (ActionRow (..), Button (..), Message (..), User (..), UserId, mkButton, mkEmoji)
27+
import System.Environment (lookupEnv)
2728
import System.Timeout (timeout)
2829
import Tablebot.Internal.Cache (getFontMap)
2930
import Tablebot.Internal.Handler.Command (parseValue)
@@ -38,6 +39,7 @@ import Tablebot.Utility.Parser
3839
import Tablebot.Utility.SmartParser
3940
import Text.Megaparsec
4041
import Text.RawString.QQ (r)
42+
import Text.Read (readMaybe)
4143

4244
-- | The basic execution function for rolling dice. Both the expression and message are
4345
-- optional. If the expression is not given, then the default roll is used.
@@ -224,16 +226,15 @@ gencharHelp =
224226
statsCommand :: Command
225227
statsCommand = Command "stats" statsCommandParser []
226228
where
227-
oneSecond = 1000000
228-
tenSeconds = 10 * oneSecond
229-
timeoutTime = tenSeconds
230229
statsCommandParser :: Parser (Message -> DatabaseDiscord ())
231230
statsCommandParser = do
232231
firstE <- pars
233232
restEs <- many (skipSpace *> pars) <* eof
234233
return $ statsCommand' (firstE : restEs)
235234
statsCommand' :: [Expr] -> Message -> DatabaseDiscord ()
236235
statsCommand' es m = do
236+
let oneSecond = 1000000
237+
timeoutTime <- liftIO $ (oneSecond *) . fromMaybe 10 . readMaybe . fromMaybe "10" <$> lookupEnv "STATS_TIMEOUT"
237238
mrange' <- liftIO $ timeout timeoutTime $ mapM (\e -> rangeExpr e >>= \re -> re `seq` return (re, parseShow e)) es
238239
case mrange' of
239240
Nothing -> throwBot (EvaluationException "Timed out calculating statistics" [])

0 commit comments

Comments
 (0)