Skip to content
Browse files

Start of timer plugin rewrite.

  • Loading branch information...
1 parent b258472 commit dcc8b194d929bbe9a11edc415fd1e6ae41a98507 @Raynes committed
Showing with 40 additions and 59 deletions.
  1. +2 −2 project.clj
  2. +38 −57 src/lazybot/plugins/timer.clj
View
4 project.clj
@@ -25,12 +25,12 @@
[ororo "0.1.0"]
[socrates "0.0.1"]
[innuendo "0.1.7"]
- [frinj "0.1.2"]
[tentacles "0.2.1"]
[findfn "0.1.3"]
[me.raynes/laser "0.1.16"]
[hiccup "1.0.2"]
- [me.raynes/least "0.1.3"]]
+ [me.raynes/least "0.1.3"]
+ [me.raynes/moments "0.1.1"]]
:uberjar-name "lazybot.jar"
:main lazybot.run
:copy-deps true
View
95 src/lazybot/plugins/timer.clj
@@ -1,66 +1,47 @@
(ns lazybot.plugins.timer
- (:refer-clojure :exclude [extend])
- (:use lazybot.registry
- clj-time.core)
- (:require [clojure.string :as s]))
+ (:require [clojure.string :as s]
+ [lazybot.registry :refer [defplugin send-message]]
+ [clj-time.core :as t]
+ [me.raynes.moments :as m]))
(def running-timers (atom {}))
+(def executor (m/executor 10))
-(defn cap-text [s n]
- (str (apply str (take n s)) (if (< 30 (count s)) "..." "")))
+(defn time-to-ms [time]
+ (t/plus (t/now)
+ (t/hours (:hour time))
+ (t/minutes (:minute time))
+ (t/seconds (:second time))))
-(defn compose-timer [[timer-n {:keys [end-time text]}]]
- (let [this-moment (now)
- [ehours eminutes eseconds] [(hour this-moment) (minute this-moment) (sec this-moment)]
- ftime (minus end-time (hours ehours) (minutes eminutes) (secs eseconds))
- end-in ((juxt hour minute sec) ftime) ]
- (str timer-n ": " (cap-text text 20) " ... Ends in: " (apply str (interpose ":" end-in)))))
+(defn task [count spec com-m]
+ (m/schedule-at executor (time-to-ms spec)
+ (fn []
+ (send-message com-m (:message spec))
+ (swap! running-timers
+ #(-> %
+ (update-in [:count] dec)
+ (dissoc count))))))
-(defplugin
- (:cmd
- "Prints a list of the currently running timers. Execute in PM if you want a full list."
- #{"timers"}
- (fn [{:keys [bot nick channel args] :as com-m}]
- (let [timers (map compose-timer @running-timers)]
- (if (> (count timers) 0)
- (if-let [n-to-show (first args)]
- (doseq [timer (take n-to-show timers)] (send-message com-m timer))
- (do
- (doseq [timer (take 3 timers)]
- (send-message com-m timer))
- (when (> (count timers) 3) (send-message com-m "and more..."))))
- (send-message com-m "No timers are currently running.")))))
+(defn set-timer [spec com-m]
+ (swap! running-timers
+ (fn [x]
+ (let [count (inc (:count x 0))]
+ (assoc x
+ :count count
+ count (task count spec com-m))))))
- (:cmd
- "Deletes a timer."
- #{"rmtimer"}
- (fn [{:keys [bot channel args] :as com-m}]
- (swap! running-timers dissoc (Integer/parseInt (first args)))
- (send-message com-m "Timer deleted.")))
+(defn parse-message [s]
+ (let [[offset message] (s/split s #" " 2)]
+ (-> (zipmap [:hour :minute :second]
+ (map #(Long. %) (s/split offset #":")))
+ (assoc :message message))))
+(defplugin
(:cmd
- "Creates a timer. You specify the time you want it to run in a 0:0:0 format, and a message
- to print once the timer has run. Once the timer completes, the message will be printed."
- #{"timer"}
- (fn [{:keys [bot channel args nick] :as com-m}]
- (let [ctime (now)
- args (-> args (->> (s/join " ")) (s/split #"[: ]+" 3))
- [time message] (split-at 3 args)
- [hour minute sec] (map #(Integer/parseInt %) time)
- newt (plus ctime (hours hour) (minutes minute) (secs sec))
- fint (in-secs (interval ctime newt))
- text (->> (or (seq message)
- [(str nick ": ping")])
- (s/join " "))
- n-timers (-> @running-timers keys (or [0]) (->> (apply max)) inc)]
- (swap! running-timers assoc n-timers
- {:end-time newt
- :text text})
- (send-message com-m "Timer added.")
- (future
- (Thread/sleep (* 1000 fint))
- (apply send-message com-m
- (if (= (first message) "/me")
- [(s/join " " (rest message)) :action? true]
- [text]))
- (swap! running-timers dissoc n-timers))))))
+ "Creates a timer. Specify the time as the first argument in h:m:s format."
+ #{"timer"}
+ (fn [{:keys [args] :as com-m}]
+ (-> (s/join " " args)
+ (parse-message)
+ (set-timer com-m))
+ (send-message com-m "Timer added."))))

0 comments on commit dcc8b19

Please sign in to comment.
Something went wrong with that request. Please try again.