init
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
(ns wiktionary-tf2.main
|
||||
(:require [clojure.java.io :as io]
|
||||
[babashka.fs :as fs]
|
||||
[babashka.process :as p]
|
||||
[clj-rcon.core :as rcon]
|
||||
[clojure.tools.logging :as l]
|
||||
[clojure.string :as str]))
|
||||
|
||||
(def words-file "words")
|
||||
|
||||
(def ^:dynamic *rcon*)
|
||||
|
||||
(defn lookup-word [word]
|
||||
(->> (p/shell {:out :string}
|
||||
"grep"
|
||||
(format "^%s\t" word)
|
||||
words-file)
|
||||
:out
|
||||
str/split-lines
|
||||
(map #(nth (re-matches #"([^\t]*)\t(.*)\n?" %)
|
||||
2 nil))
|
||||
(filter #(not (empty? %))) ; remove nil and ""
|
||||
first))
|
||||
|
||||
(defn parse-chat-message [x]
|
||||
(when-let [[_ author body] (re-matches #"(.*?) : (.*)" x)]
|
||||
{:author author :body body}))
|
||||
|
||||
(defn say-word [word]
|
||||
(->> (format "%s: %s" word (or (lookup-word word)
|
||||
"«no entry found ☹»"))
|
||||
(take 128)
|
||||
(apply str)
|
||||
(format "say \"%s\"")
|
||||
(rcon/exec @*rcon*)))
|
||||
|
||||
(defn try-bracketed-words [s]
|
||||
(when-let [word (some-> (re-find #"\[\[([^]]+)]]" s) second)]
|
||||
(l/infof "bracked word: %s" word)
|
||||
(Thread/sleep 500)
|
||||
(say-word word)))
|
||||
|
||||
(defn -main []
|
||||
(binding [*rcon* (rcon/connect "127.0.0.1" 27015 "monitor")]
|
||||
(l/info "ready!")
|
||||
(loop [x (read-line)]
|
||||
(l/infof "! %s" x)
|
||||
(when-let [{:keys [author body]} (parse-chat-message x)]
|
||||
(try-bracketed-words body))
|
||||
(flush)
|
||||
(recur (read-line)))))
|
||||
Reference in New Issue
Block a user