From 90da3e52e3088c5898d3b8705a081fa14124e1f6 Mon Sep 17 00:00:00 2001 From: Bongo50 Date: Sat, 20 Sep 2025 23:36:18 +0100 Subject: [PATCH 1/2] Making the timeout for the dice stats command an environmental variable --- .env.example | 1 + README.md | 1 + src/Tablebot/Plugins/Roll/Plugin.hs | 7 ++++--- 3 files changed, 6 insertions(+), 3 deletions(-) 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..58421ff 100644 --- a/src/Tablebot/Plugins/Roll/Plugin.hs +++ b/src/Tablebot/Plugins/Roll/Plugin.hs @@ -23,6 +23,7 @@ import Discord.Interactions import Discord.Internal.Rest.Channel (ChannelRequest (..), MessageDetailedOpts (..)) import Discord.Types (ActionRow (..), Button (..), Message (..), User (..), UserId, mkButton, mkEmoji) import System.Timeout (timeout) +import System.Environment (lookupEnv) import Tablebot.Internal.Cache (getFontMap) import Tablebot.Internal.Handler.Command (parseValue) import Tablebot.Plugins.Roll.Dice @@ -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" []) From bef80dc34aad0e5a1c7163dfc1199d7c71724594 Mon Sep 17 00:00:00 2001 From: Bongo50 Date: Sat, 20 Sep 2025 23:37:36 +0100 Subject: [PATCH 2/2] Ormolu --- src/Tablebot/Plugins/Roll/Plugin.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tablebot/Plugins/Roll/Plugin.hs b/src/Tablebot/Plugins/Roll/Plugin.hs index 58421ff..273cc27 100644 --- a/src/Tablebot/Plugins/Roll/Plugin.hs +++ b/src/Tablebot/Plugins/Roll/Plugin.hs @@ -22,8 +22,8 @@ import Discord.Interactions ) import Discord.Internal.Rest.Channel (ChannelRequest (..), MessageDetailedOpts (..)) import Discord.Types (ActionRow (..), Button (..), Message (..), User (..), UserId, mkButton, mkEmoji) -import System.Timeout (timeout) import System.Environment (lookupEnv) +import System.Timeout (timeout) import Tablebot.Internal.Cache (getFontMap) import Tablebot.Internal.Handler.Command (parseValue) import Tablebot.Plugins.Roll.Dice