Skip to content
Open
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
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[clojail "1.0.6"]]
[clojail "1.0.6"]
[org.clojure/core.match "0.3.0-alpha5"]]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer it if you didn't pull in core.match for a simple feature like this.

:main ^:skip-aot clojurebot.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
34 changes: 26 additions & 8 deletions src/clojurebot/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[clojail.testers :as testers]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.core.match :refer [match]]
[clojurebot.irc :as irc]
[clojure.string :as string]))

Expand Down Expand Up @@ -103,16 +104,33 @@
(str "Error: No symbol for " sym-name))
"Error: No arguments provided")))

(reg-commend
(defn- parse-number-of-dice
[n]
(if (nil? n) [:ok 1]
(try
(let [result (Integer/parseInt n)]
(cond

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just return the string directly and not bother with fancy error handling.

(< result 0) [:error "You can't roll a negative amount of dice, man"]
(= result 0) [:error "You want to roll zero dice? Okay, you rolled nothing..."]
:else [:ok result]))
(catch java.lang.NumberFormatException _
[:error (str n " is not a number! Please give me a number of dice to roll.")])
(catch java.lang.ClassCastException _
[:error (str "I'm expecting a number-string as input, but you've given me " n ", with " (type n))]))))

(reg-command
"d20"
(fn [n]
(if (< n 0)
"You can't roll a negative amount of dice, man"
(str "You rolled a "
(->> #(+ (rand-int 20) 1)
(repeatedly)
(take n)
(string/join " and a "))))))
(match (parse-number-of-dice n)
[:error err] err
[:ok x] (let [nums (->> #(+ (rand-int 20) 1)
(repeatedly)
(take x))]
(str "You rolled "
(string/join " and " nums)
(if (> x 1)
(str ", totalling " (reduce + 0 nums)))))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Entry point
Expand Down