Skip to content

Instantly share code, notes, and snippets.

@hglbrg
Created January 11, 2015 18:18
Show Gist options
  • Save hglbrg/1673ac10b5001710dbc3 to your computer and use it in GitHub Desktop.
Save hglbrg/1673ac10b5001710dbc3 to your computer and use it in GitHub Desktop.
Spartan IRC Bot in Python
import socket #imports module allowing connection to IRC
import threading #imports module allowing timing functions
#sets variables for connection to twitch chat
bot_owner = 'SpartacurseTV'
nick = 'phaseplant'
channel = '#SpartacurseTV'
server = 'irc.twitch.tv'
password = '*snip*'
queue = 0 #sets variable for anti-spam queue functionality
irc = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
irc.connect((server, 6667)) #connects to the server
#sends variables for connection to twitch chat
irc.send('PASS ' + password + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
irc.send('JOIN ' + channel + '\r\n')
def irc_split(line):
tags = sender = None
if line[0] == "@":
tags, line = line.split(" ", 1)
if line[0] == ":":
sender, line = line.split(" ", 1)
if " :" in line:
line, trailing = line.split(" :", 1)
line = line.split(" ")
line.append(trailing)
else:
line = line.split(" ")
cmd = line.pop(0).upper()
return tags, sender, cmd, line
while True:
data = irc.recv(4096) #gets output from IRC server
print("I got %r" % data)
tags, sender, cmd, args = irc_split(data)
print(" - sender is %r" % sender)
print(" - command is %r" % cmd)
print(" - parameters are %r" % args)
nick = sender.split("!")[0] if sender else None
if cmd == "PING":
irc.send("PONG %s\r\n" % args.join(" "))
elif cmd == "PRIVMSG":
channel, text = args
reply = "oh hey %s" % nick
irc.send("PRIVMSG %s :%s\r\n" % (channel, reply))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment