Created
July 12, 2016 16:27
-
-
Save kentros/6d934b3cd6f83d1d2dd6545f5c8d4676 to your computer and use it in GitHub Desktop.
Simple clojure trivia plugin for lazybot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns lazybot.plugins.trivia | |
(:use (lazybot registry info)) | |
(:require [clojure.string :as s] | |
[clojure.java.io :as io])) | |
(use '[incanter.stats :only (levenshtein-distance)]) | |
(def answer (atom "")) | |
(def buzzer (atom 0)) | |
(defn rand-seq-elem [sequence] | |
(let [f (fn [[k old] new] | |
[(inc k) (if (zero? (rand-int k)) new old)])] | |
(->> sequence (reduce f [1 nil]) second))) | |
(defn rand-line [filename] | |
(with-open [reader (io/reader filename)] | |
(rand-seq-elem (line-seq reader)))) | |
(defn start-trivia [x] (reset! answer (second x)) (reset! buzzer (System/currentTimeMillis)) (first x)) | |
(defn end-trivia [] (reset! answer "") (reset! buzzer 0)) | |
(defplugin | |
(:hook | |
:on-message | |
(fn [{:keys [bot com nick message channel] :as com-m}] | |
(if-not (empty? @answer) | |
(if (.equalsIgnoreCase message @answer) | |
(do | |
(send-message (assoc com-m :channel "#trivia") (str nick ", you got it right! The answer is: " @answer)) | |
(end-trivia)) | |
(if (> (- 1 (/ (levenshtein-distance @answer message) (.length @answer))) 0.50) | |
(do | |
(send-message (assoc com-m :channel "#trivia") (str nick ", close enough for me! The exact answer is: " @answer)) | |
(end-trivia)) | |
(if (> (System/currentTimeMillis) (+ 60000 @buzzer)) | |
(do | |
(send-message (assoc com-m :channel "#trivia") (str "Time's up! Answer: " @answer)) | |
(end-trivia)))))))) | |
(:cmd | |
"Ask a trivia question!" | |
#{"trivia" "t"} | |
(fn [com-m] (if (empty? @answer) (send-message com-m (start-trivia | |
(-> "jquestions" io/resource io/file rand-line (s/split #"`")))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment