Last active
December 21, 2015 17:18
-
-
Save quanticle/6339254 to your computer and use it in GitHub Desktop.
Getting an ArityException when I try to call the send-command multimethod
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 clojure-irc-bot.core) | |
(import '(java.net Socket)) | |
(import '(java.io InputStream InputStreamReader BufferedReader OutputStream OutputStreamWriter PrintWriter)) | |
(defn connect-to-server [server-name, server-port] | |
"Connects to a server and returns a dictionary containing the raw socket, a | |
BufferedReader that allows you to read from the socket and a PrintWriter | |
that allows you to write to the socket" | |
(let [socket (Socket. server-name server-port) | |
reader (BufferedReader. (InputStreamReader. (.getInputStream socket))) | |
writer (PrintWriter. (OutputStreamWriter. (.getOutputStream socket)) true)] | |
{:socket socket, :reader reader, :writer writer})) | |
(defn command-type [command-type-name & command-args] | |
command-type-name) | |
(defmulti send-command command-type) | |
(defmethod send-command :NICK [socket-info nickname] | |
(println "Sending:" (str "NICK " nickname)) | |
(.println (:writer socket-info) (str "NICK " nickname))) | |
(defmethod send-command :USER [socket-info username realname] | |
(.println (:writer socket-info) (str "USER "))) | |
(defmethod send-command :JOIN [socket-info channel] | |
(.println (:writer socket-info) (str "#" channel))) | |
(defn find-message-type [connection, message] | |
message) | |
(defmulti handle-message find-message-type) | |
(defmethod handle-message :default [connection message] | |
(println message)) | |
(defn event-loop [server-name server-port channel nickname] | |
"Connects to the IRC server, joins the specified channel and waits for | |
data. Upon receiving data, it uses the handle-message multimethod to act | |
upon said data" | |
(let [connection (connect-to-server server-name, server-port)] | |
(send-command :NICK connection "quanticle-bot") | |
(send-command :USER connection "quanticle-bot" "quanticle-bot") | |
(send-command :JOIN connection "quanticle") | |
(loop [data (.readLine (:reader connection))] | |
(if (> (count data) 0) | |
(handle-message connection data) | |
nil) | |
(if (.isConnected (:socket connection)) | |
(recur (.readLine (:reader connection))) | |
nil)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment