Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
7 changes: 4 additions & 3 deletions src/Tablebot/Plugins/Roll/Plugin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -223,16 +225,15 @@ gencharHelp =
statsCommand :: Command
statsCommand = Command "stats" statsCommandParser []
where
oneSecond = 1000000
tenSeconds = 10 * oneSecond
timeoutTime = tenSeconds
statsCommandParser :: Parser (Message -> DatabaseDiscord ())
statsCommandParser = do
firstE <- pars
restEs <- many (skipSpace *> pars) <* eof
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" [])
Expand Down
Loading