Skip to content

Instantly share code, notes, and snippets.

@nathanic
Created November 14, 2012 16:01
Show Gist options
  • Save nathanic/4072965 to your computer and use it in GitHub Desktop.
Save nathanic/4072965 to your computer and use it in GitHub Desktop.
clojure implementation of josh's big ben bot
(ns big-ben.core
(:use [clj-time.core :exclude [extend]]
[clj-time.local])
(:require [irclj.core :as ircb]
[overtone.at-at :as at-at]
[clojure.string :as string]))
(def HOUR-IN-MS (* 60 60 1000))
(def config
{:server "irc.freenode.net"
:port 6667
:channel "#silly-bot-test"
:nick "clj-big-ben"
})
; we'll stick the IRC connection handle here
(def irc (atom nil))
; under the hood clj-time uses JodaTime
(defn seconds-until-next-toll []
(let [t (local-now)
minutes-left (- 59 (minute t))
seconds-left (- 60 (sec t)) ]
(+ (* 60 minutes-left)
seconds-left)))
(defn announce-time []
(let [h (hour (local-now))
h (if (zero? h) 12 h)]
(ircb/message @irc
(:channel config)
(string/join " " (repeat h "*BONG*")))))
(defn -main [& args]
(reset! irc (ircb/connect (:server config)
(:port config)
(:nick config)))
(ircb/join @irc (:channel config))
; make a little thread pool for the periodic task
; under the hood at-at uses j.u.c.ScheduledThreadPoolExecutor
(let [my-pool (at-at/mk-pool)]
(at-at/every HOUR-IN-MS
announce-time
my-pool
:initial-delay (* 1000
(seconds-until-next-toll)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment