Created
December 19, 2018 21:03
-
-
Save aturley/4ffe643fd866a789ba2e1cead3feebfe to your computer and use it in GitHub Desktop.
A Twitch IRC bot written from scratch in Pony
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
use "buffered" | |
use "debug" | |
use "net" | |
class TwitchChatConnectionNotify is TCPConnectionNotify | |
let _nick: String | |
let _password: String | |
let _reader: Reader | |
new iso create(nick: String, password: String) => | |
_nick = nick | |
_password = password | |
_reader = Reader | |
fun ref connected(conn: TCPConnection ref) => | |
Debug("CONNECTED!") | |
conn.write("PASS " + _password + "\r\n") | |
conn.write("NICK " + _nick + "\r\n") | |
fun ref received( | |
conn: TCPConnection ref, | |
data: Array[U8] iso, | |
times: USize) | |
: Bool | |
=> | |
Debug("RECEIVED DATA!") | |
let data': Array[U8] val = consume data | |
Debug(String.from_array(data')) | |
_reader.append(data') | |
try | |
while true do | |
let msg: String = _reader.line()? | |
_process_message(msg, conn) | |
end | |
end | |
true | |
fun ref connect_failed(conn: TCPConnection ref) => | |
Debug("CONNECT FAILED!") | |
fun ref _process_message(msg: String, conn: TCPConnection ref) => | |
if msg.contains("You are in a maze of twisty passages, all alike.") then | |
conn.write("JOIN :#aturls\r\n") | |
end | |
try | |
let parts = msg.split(" ") | |
if parts(1)? == "366" then | |
Debug("FOUND 366") | |
conn.write("PRIVMSG #aturls :Wilbur!\r\n") | |
end | |
end | |
actor Main | |
new create(env: Env) => | |
var twitch_nick = "" | |
var twitch_password = "" | |
try | |
for x in env.vars.values() do | |
let parts = x.split("=") | |
if parts(0)? == "TWITCH_USERNAME" then | |
twitch_nick = parts(1)? | |
end | |
if parts(0)? == "TWITCH_PASSWORD" then | |
twitch_password = parts(1)? | |
end | |
end | |
else | |
env.err.print("couldn't get nickname and password") | |
return | |
end | |
if twitch_nick.size() == 0 then | |
env.err.print("could not get nickname, please set the nickname in the TWITCH_USERNAME environment variable.") | |
return | |
else | |
env.out.print("nick=" + twitch_nick) | |
end | |
if twitch_password.size() == 0 then | |
env.err.print("could not get password, please set the nickname in the TWITCH_PASSWORD environment variable.") | |
return | |
else | |
env.out.print("got password") | |
end | |
try | |
TCPConnection(env.root as AmbientAuth, | |
TwitchChatConnectionNotify(twitch_nick, twitch_password), | |
"irc.chat.twitch.tv", "6667") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment