matchbot/src/bot.clj

79 lines
2.1 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 sorted-players-of-game [state game]
(->> game
(str/lower-case)
(keyword)
(game/get-players-of-game state)
(sort)))
(defn match-string [state game _]
(str "Anyone ready for "
game
"? "
(str/join " " (sorted-players-of-game state game))))
(defn list-players-string [state game _]
(let [players (str/join (map #(str " _" % "_")
(sorted-players-of-game state game)))]
(str "Players of "
game
":"
players)))
(defn add-player-string [_ game player]
(str "Added "
player
" to the list of players for "
game
"!"))
(defn remove-player-string [_ game player]
(str "Removed "
player
" from the list of players for "
game
"!"))
(defn list-games-string [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 (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))
"!players"
(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))))