diff --git a/.env.example b/.env.example index 2d641d2..b5fb462 100644 --- a/.env.example +++ b/.env.example @@ -9,4 +9,5 @@ SUPERUSER_GROUP=147258369147258369 SERVER_ID=314159265358979323 ALLOW_GIT_UPDATE=False EMOJI_SERVERS=[121213131414151516] +STATS_TIMEOUT=20 # NOTE: YOU MUST HAVE A NEWLINE AT THE END OF THE FILE diff --git a/README.md b/README.md index e8ee3b9..55a5a23 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Create a `.env` file containing the following keys. Consult `.env.example` if yo * `SERVER_ID` (optional) - either `global` or the id of the server the bot will mainly be deployed in. Application commands will be registered here. If absent, application commands won't be registered. * `EMOJI_SERVERS` (optional) - a list of server IDs that the bot will search for emoji within. +* `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. * `ALLOW_GIT_UPDATE` (optional) - a `true` or `false` value that determines whether the bot can automatically load data from the repository. **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! diff --git a/src/Tablebot/Plugins/Roll/Plugin.hs b/src/Tablebot/Plugins/Roll/Plugin.hs index 54b023d..273cc27 100644 --- a/src/Tablebot/Plugins/Roll/Plugin.hs +++ b/src/Tablebot/Plugins/Roll/Plugin.hs @@ -22,6 +22,7 @@ import Discord.Interactions ) import Discord.Internal.Rest.Channel (ChannelRequest (..), MessageDetailedOpts (..)) import Discord.Types (ActionRow (..), Button (..), Message (..), User (..), UserId, mkButton, mkEmoji) +import System.Environment (lookupEnv) import System.Timeout (timeout) import Tablebot.Internal.Cache (getFontMap) import Tablebot.Internal.Handler.Command (parseValue) @@ -36,6 +37,7 @@ import Tablebot.Utility.Parser import Tablebot.Utility.SmartParser import Text.Megaparsec import Text.RawString.QQ (r) +import Text.Read (readMaybe) -- | The basic execution function for rolling dice. Both the expression and message are -- optional. If the expression is not given, then the default roll is used. @@ -223,9 +225,6 @@ gencharHelp = statsCommand :: Command statsCommand = Command "stats" statsCommandParser [] where - oneSecond = 1000000 - tenSeconds = 10 * oneSecond - timeoutTime = tenSeconds statsCommandParser :: Parser (Message -> DatabaseDiscord ()) statsCommandParser = do firstE <- pars @@ -233,6 +232,8 @@ statsCommand = Command "stats" statsCommandParser [] return $ statsCommand' (firstE : restEs) statsCommand' :: [Expr] -> Message -> DatabaseDiscord () statsCommand' es m = do + let oneSecond = 1000000 + timeoutTime <- liftIO $ (oneSecond *) . fromMaybe 10 . readMaybe . fromMaybe "10" <$> lookupEnv "STATS_TIMEOUT" mrange' <- liftIO $ timeout timeoutTime $ mapM (\e -> rangeExpr e >>= \re -> re `seq` return (re, parseShow e)) es case mrange' of Nothing -> throwBot (EvaluationException "Timed out calculating statistics" [])