Skip to content

Instantly share code, notes, and snippets.

@hagna
Created February 9, 2012 23:25
Show Gist options
  • Save hagna/1784211 to your computer and use it in GitHub Desktop.
Save hagna/1784211 to your computer and use it in GitHub Desktop.
(ns bot1.core
;; To run this (on windows linux or osx) you need lein which isn't much
;; just follow the instructions in the README at
;; https://github.com/technomancy/leiningen
;;
;; In an (ns ..) form, you typically use keywords. These are flags
;; telling ns what to do, which is why it is different from the REPL
;; and outside of an ns form. These are flags that call the
;; functions/macros for you.
(:require hobbit.bitly)
;; You can have multiple imports (and requires too).
(:import (hobbit.bitly Bitly)
(org.jibble.pircbot PircBot)))
(def ^:dynamic *bot*)
(def url-regex #"[A-Za-z]+://[^ ^/]+\.[^ ^/]+[^ ]+")
(def random (java.util.Random.))
;define characters list to use to generate string
(def chars
(map char (concat (range 48 58) (range 66 92) (range 97 123))))
;generates 1 random character
(defn random-char []
(nth chars (.nextInt random (count chars))))
; generates random string of length characters
(defn random-string [length]
(apply str (take length (repeatedly random-char))))
;; Clojure doesn't use camelCase for names. Use hypens to separate words
;; in names.
(defn send-msg "send a message to a recv, a recv is a channel name or a nick"
[this recv msg]
(.sendMessage this recv (.replace (str msg) \newline \ )))
(def *bitly* (hobbit.bitly/Bitly. "key" "username" "j.mp"))
(defn rick "generates a nice rick roll that seems different every time"
[]
(let [roll "http://www.youtube.com/watch?v=XZ5TajZYW6Y"]
(hobbit.core/shorten *bitly* (str roll "/#" (random-string 5)))))
(defn handle-message [channel sender login hostname message]
(let [url (re-find url-regex message)]
(if (not (= url nil))
(if (= sender "hagna")
(send-msg *bot* channel (hobbit.core/shorten *bitly* url))
(send-msg *bot* channel (rick))))))
(defn pircbot []
(proxy [PircBot] []
(onMessage [channel sender login hostname message]
(handle-message channel sender login hostname message)
)))
(defn -main [& args]
(def *bot* (pircbot))
(.connect *bot* "")
(.changeNick *bot* "rick")
(.joinChannel *bot* "#dev"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment