matchbot/src/bot.clj

87 lines
2.3 KiB
Clojure

;; SPDX-License-Identifier: Apache-2.0
;; SPDX-FileCopyrightText: 2022 Sebastian Crane <seabass-labrax@gmx.com>
(ns bot
(:require [clojure.string :as str]
[game]
[irclj.core]))
(defn keywordise-game [game]
(when (string? game)
(keyword (str/lower-case game))))
(defn sorted-players-of-game [state game]
(->> game
(keywordise-game)
(game/get-players-of-game state)
(sort)))
(defn match-string [& {:keys [state game]}]
(str "Anyone ready for "
game
"? "
(str/join " " (sorted-players-of-game state game))))
(defn list-players-string [& {:keys [state game]}]
(let [players (str/join (map #(str " _" % "_")
(sorted-players-of-game state game)))]
(str "Players of "
game
":"
players)))
(defn add-player-string [& {:keys [game player]}]
(str "Added "
player
" to the list of players for "
game
"!"))
(defn remove-player-string [& {:keys [game player]}]
(str "Removed "
player
" from the list of players for "
game
"!"))
(defn list-games-string [& {:keys [state]}]
(str "Games with a list of players: "
(str/join
", "
(sort (map name (game/get-games state))))))
(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 (keywordise-game 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)]
(if game
(condp = command
"!match"
(match-string :state @state :game game)
"!add"
(do (swap! state game/add-player-of-game game-keyword sender)
(add-player-string :game game :player sender))
"!players"
(list-players-string :state @state :game game)
"!remove"
(do (swap! state game/remove-player-of-game game-keyword sender)
(remove-player-string :game game :player sender))
nil)
(condp = command
"!list"
(list-games-string :state @state)
nil))))