| 1 |
-- | This module is for throwing dice for e.g. RPGs. (\@dice 3d6+2) |
| 2 |
|
| 3 |
-- Original version copyright Einar Karttunen <[email protected]> 2005-04-06. |
| 4 |
-- Massive rewrite circa 2008-10-20 copyright James Cook <[email protected]> |
| 5 |
module Lambdabot.Plugin.Novelty.Dice (dicePlugin) where |
| 6 |
|
| 7 |
import Lambdabot.Plugin |
| 8 |
import Lambdabot.Util |
| 9 |
|
| 10 |
import Data.List |
| 11 |
import Data.Random.Dice (rollEm) |
| 12 |
|
| 13 |
type Dice = ModuleT () LB |
| 14 |
|
| 15 |
dicePlugin :: Module () |
| 16 |
dicePlugin = newModule |
| 17 |
{ moduleCmds = return |
| 18 |
[ (command "dice") |
| 19 |
{ aliases = ["roll"] |
| 20 |
, help = say "@dice <expr>. Throw random dice. <expr> is of the form 3d6+2." |
| 21 |
, process = doDice True |
| 22 |
} |
| 23 |
] |
| 24 |
, contextual = doDice False |
| 25 |
} |
| 26 |
|
| 27 |
doDice :: Bool -> String -> Cmd Dice () |
| 28 |
doDice printErrs text = do |
| 29 |
user <- showNick =<< getSender |
| 30 |
result <- io (rollEm text) |
| 31 |
case result of |
| 32 |
Left err -> if printErrs |
| 33 |
then say (trimError err) |
| 34 |
else return () |
| 35 |
Right str -> |
| 36 |
say (limitStr 75 (user ++ ": " ++ str)) |
| 37 |
|
| 38 |
where |
| 39 |
trimError = concat . intersperse ": " . tail . lines . show |