-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rkt
More file actions
35 lines (30 loc) · 1.3 KB
/
main.rkt
File metadata and controls
35 lines (30 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#lang braidbot/insta
(require braidbot/util)
(define bot-id (or (getenv "BOT_ID") "5a693a60-008c-4e96-b26a-1ff8df38de9f"))
(define bot-token (or (getenv "BOT_TOKEN") "bBjo9Jl1jW244In_9hpquG70rUeY1O2kY8ewFPH9"))
(define braid-api-url (or (getenv "BRAID_API_URL") "http://localhost:5557"))
(define braid-frontend-url (or (getenv "BRAID_FRONTEND_URL") "http://localhost:5555"))
(listen-port 9192)
(define (parse-roll roll-txt)
(if-let [matches (regexp-match #rx"^([0-9]*)d([0-9]+)([-+][0-9]+)?$" roll-txt)]
(let ([ndice (~> matches cadr string->number (or 1))]
[nsides (~> matches caddr string->number)]
[bonus (or (some~> matches cadddr string->number) 0)])
(+ (for/sum ([_ (range ndice)])
(+ 1 (random nsides)))
bonus))
#f))
(define (act-on-message msg)
(let ([roll-req (~> (hash-ref msg '#:content)
(string-replace "/roll " "" #:all? #f))])
(~>>
(if-let [result (parse-roll roll-req)]
(format "~s = ~v" roll-req result)
(~> (list
(format "Couldn't parse request ~v" roll-req)
"Try something like `/roll d6`, `/roll 3d4`, `/roll 2d20+3`")
(string-join "\n")))
(reply-to msg
#:bot-id bot-id
#:bot-token bot-token
#:braid-url braid-api-url))))