Created
December 20, 2015 20:13
-
-
Save blizzarddreams/ac75c07d35ca9c0fee81 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package ircscalabot | |
| import java.io._ | |
| import java.net.Socket | |
| /** | |
| * Created by Anthony on 12/19/2015. | |
| */ | |
| object Bot extends App { | |
| val scalabot = new Bot("scalaevalbot", "", "chat.freenode.net", 6667, List("#botwars"), '$') | |
| scalabot.connect() | |
| } | |
| class Bot(name: String, pass: String, server: String, port: Int, channels: List[String], symbol: Char) { | |
| var writer: BufferedWriter = _ | |
| var reader: BufferedReader = _ | |
| val commands = Commands.commands | |
| def login(): Unit = { | |
| writer.write(s"USER $name 8 * :$name\r\n") | |
| writer.flush() | |
| writer.write(s"NICK $name\r\n") | |
| writer.flush() | |
| } | |
| def joinChannels(): Unit = { | |
| for (i <- channels) { | |
| writer.write(s"JOIN $i\r\n") | |
| writer.flush() | |
| } | |
| } | |
| def connect(): Unit = { | |
| val conn = new Socket(server, port) | |
| writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())) | |
| reader = new BufferedReader(new InputStreamReader(conn.getInputStream())) | |
| //login() | |
| //joinChannels() | |
| while (true) { | |
| val line = reader.readLine() | |
| if (line != null) { | |
| println(line) | |
| } | |
| var realMessage: String = "" | |
| try { | |
| realMessage = line.split("PRIVMSG")(1).split(" :")(1) | |
| } catch { | |
| case e: java.lang.ArrayIndexOutOfBoundsException => realMessage = "" | |
| case e: java.lang.StringIndexOutOfBoundsException => realMessage = "" | |
| } | |
| if (realMessage.length > 0) { | |
| if (realMessage.charAt(0) == symbol) { | |
| val channel = line.split("PRIVMSG ")(1).split(" :")(0) | |
| val command = realMessage.split(symbol)(1).split(" ")(0) | |
| var args: String = "" | |
| if (realMessage.contains(" ")) { | |
| args = realMessage.split(s"$command ")(1) | |
| } else { | |
| args = "" | |
| } | |
| writer.write(s"PRIVMSG $channel :${commands(command)(args)}\r\n") | |
| writer.flush() | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment