Last active
February 8, 2016 00:46
-
-
Save singlerider/c0368c508f96804c5567 to your computer and use it in GitHub Desktop.
Connect to Twitch Chat and Whispers simultaneously (IRC connection)
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
server = 'irc.twitch.tv' | |
port = 6667 | |
username = 'lorenzotherobot' | |
oauth_password = 'oauth:5fakeOuathud90jsdam' # get this from http://twitchapps.com/tmi/ | |
channel = "#singlerider" | |
from twisted.internet import reactor | |
from twisted.words.protocols import irc | |
from twisted.internet.protocol import ClientFactory | |
import time | |
SERVER = server | |
NICKNAME = username | |
PASSWORD = oauth_password | |
class Bot(irc.IRCClient): | |
def __init__(self): | |
self.nickname = NICKNAME | |
self.password = PASSWORD | |
self.channel = channel | |
def dataReceived(self, data): | |
print(data) | |
irc.IRCClient.dataReceived(self, data) | |
def signedOn(self): | |
print("\033[91m\n\nYOLO, I was signed on to the server!!!\n\033[0m") | |
self.join(channel) | |
def connectionLost(self, reason): | |
irc.IRCClient.connectionLost(self, reason) | |
reactor.stop() | |
def privmsg(self, user, channel, msg): | |
"""Called when the bot receives a message.""" | |
self.msg(channel, msg + "\r\n") | |
class BotFactory(ClientFactory): | |
protocol = Bot | |
class Whisper(irc.IRCClient): | |
def __init__(self): | |
self.nickname = NICKNAME | |
self.password = PASSWORD | |
def dataReceived(self, data): | |
print(data) | |
if data.split()[1] == "WHISPER": | |
user = data.split()[0].lstrip(":") | |
channel = user.split("!")[0] | |
msg = " ".join(data.split()[3:]).lstrip(":") | |
self.privmsg(user, channel, msg) | |
irc.IRCClient.dataReceived(self, data) | |
def signedOn(self): | |
print("\033[91m\n\nShhhh!!! Whispers only!\n\033[0m") | |
self.sendLine("CAP REQ :twitch.tv/commands") | |
def connectionLost(self, reason): | |
irc.IRCClient.connectionLost(self, reason) | |
reactor.stop() | |
def privmsg(self, user, channel, msg): | |
line = ":%s PRIVMSG #jtv :/w %s %s" % (user, channel, msg) | |
self.sendLine(line) | |
class WhisperFactory(ClientFactory): | |
protocol = Whisper | |
if __name__ == "__main__": | |
reactor.connectTCP(SERVER, 6667, BotFactory()) | |
reactor.connectTCP("52.223.240.119", 443, WhisperFactory()) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment