forked from LibreGaming/matchbot
Add function with tests to dispatch IRC commands
Signed-off-by: Sebastian Crane <seabass-labrax@gmx.com>
This commit is contained in:
parent
ac1a8d9c94
commit
5c0918f990
30
src/bot.clj
30
src/bot.clj
|
@ -40,3 +40,33 @@
|
||||||
" from the list of players for "
|
" from the list of players for "
|
||||||
game
|
game
|
||||||
"!"))
|
"!"))
|
||||||
|
|
||||||
|
(defn split-message [message]
|
||||||
|
(let [message-parts (str/split message #"\s")
|
||||||
|
command (if-let [x (first message-parts)] (str/lower-case x) "")
|
||||||
|
game (second message-parts)
|
||||||
|
game-keyword (when game (keyword (str/lower-case game)))]
|
||||||
|
{:command command
|
||||||
|
:game game
|
||||||
|
:game-keyword game-keyword}))
|
||||||
|
|
||||||
|
(defn dispatch-command [state sender message]
|
||||||
|
(let [{command :command
|
||||||
|
game :game
|
||||||
|
game-keyword :game-keyword} (split-message message)]
|
||||||
|
(when game
|
||||||
|
(condp = command
|
||||||
|
"!match"
|
||||||
|
(match-string @state game nil)
|
||||||
|
|
||||||
|
"!add"
|
||||||
|
(do (swap! state game/add-player-of-game game-keyword sender)
|
||||||
|
(add-player-string nil game sender))
|
||||||
|
|
||||||
|
"!list"
|
||||||
|
(list-players-string @state game nil)
|
||||||
|
|
||||||
|
"!remove"
|
||||||
|
(do (swap! state game/remove-player-of-game game-keyword sender)
|
||||||
|
(remove-player-string nil game sender))
|
||||||
|
nil))))
|
||||||
|
|
|
@ -24,3 +24,17 @@
|
||||||
(deftest remove-player-string-test
|
(deftest remove-player-string-test
|
||||||
(is (= "Removed abc from the list of players for hypothetical-shooter!"
|
(is (= "Removed abc from the list of players for hypothetical-shooter!"
|
||||||
(remove-player-string nil "hypothetical-shooter" "abc"))))
|
(remove-player-string nil "hypothetical-shooter" "abc"))))
|
||||||
|
|
||||||
|
(deftest split-message-test
|
||||||
|
(is (= {:command "!match" :game "Quasi-Rts" :game-keyword :quasi-rts}
|
||||||
|
(split-message "!match Quasi-Rts "))))
|
||||||
|
|
||||||
|
(deftest dispatch-command-test
|
||||||
|
(let [state-atom (atom test-state)]
|
||||||
|
(is (and
|
||||||
|
(= "Added 123 to the list of players for Imaginary-RPG!"
|
||||||
|
(dispatch-command state-atom "123" "!add Imaginary-RPG"))
|
||||||
|
(= {:games {:hypothetical-shooter #{"abc" "xyz" "123"}
|
||||||
|
:quasi-rts #{"abc" "123"}
|
||||||
|
:imaginary-rpg #{"xyz" "abc" "123"}}}
|
||||||
|
@state-atom)))))
|
||||||
|
|
Loading…
Reference in New Issue