├── .gitignore ├── info.rkt ├── README.md └── main.rkt /.gitignore: -------------------------------------------------------------------------------- 1 | compiled/ 2 | deploy/ 3 | deploy.tar.gz 4 | dicebot 5 | -------------------------------------------------------------------------------- /info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | (define collection "dicebot") 3 | (define deps '("base" 4 | "https://github.com/braidchat/braidbot.git#v2.0")) 5 | (define build-deps '()) 6 | (define pkg-desc "Roll dice") 7 | (define version "0.0") 8 | (define pkg-authors '(james)) 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dicebot 2 | 3 | A Braid bot for rolling dice. 4 | 5 | Uses the [braidbot](https://github.com/braidchat/braidbot) framework; see that page for installation instructions. 6 | 7 | Expects to be added with the name "roll". 8 | 9 | Knows commands of the form `2d6+4`, where the number of dice to roll is optional (assumed to be one) and the bonus is optional (assumed to be zero) & can be positive or negative. 10 | -------------------------------------------------------------------------------- /main.rkt: -------------------------------------------------------------------------------- 1 | #lang braidbot/insta 2 | 3 | (require braidbot/util) 4 | 5 | (define bot-id (or (getenv "BOT_ID") "5a693a60-008c-4e96-b26a-1ff8df38de9f")) 6 | (define bot-token (or (getenv "BOT_TOKEN") "bBjo9Jl1jW244In_9hpquG70rUeY1O2kY8ewFPH9")) 7 | (define braid-api-url (or (getenv "BRAID_API_URL") "http://localhost:5557")) 8 | (define braid-frontend-url (or (getenv "BRAID_FRONTEND_URL") "http://localhost:5555")) 9 | 10 | (listen-port 9192) 11 | 12 | (define (parse-roll roll-txt) 13 | (if-let [matches (regexp-match #rx"^([0-9]*)d([0-9]+)([-+][0-9]+)?$" roll-txt)] 14 | (let ([ndice (~> matches cadr string->number (or 1))] 15 | [nsides (~> matches caddr string->number)] 16 | [bonus (or (some~> matches cadddr string->number) 0)]) 17 | (+ (for/sum ([_ (range ndice)]) 18 | (+ 1 (random nsides))) 19 | bonus)) 20 | #f)) 21 | 22 | (define (act-on-message msg) 23 | (let ([roll-req (~> (hash-ref msg '#:content) 24 | (string-replace "/roll " "" #:all? #f))]) 25 | (~>> 26 | (if-let [result (parse-roll roll-req)] 27 | (format "~s = ~v" roll-req result) 28 | (~> (list 29 | (format "Couldn't parse request ~v" roll-req) 30 | "Try something like `/roll d6`, `/roll 3d4`, `/roll 2d20+3`") 31 | (string-join "\n"))) 32 | (reply-to msg 33 | #:bot-id bot-id 34 | #:bot-token bot-token 35 | #:braid-url braid-api-url)))) 36 | --------------------------------------------------------------------------------