I just made a library for Python 2.7 that allows you to easily create IRC bots.
Here is the source code:
import socket
import string
import time
s = socket.socket(AF_INET, SOCK_STREAM)
def connect(server, port=6667):
s.connect((server, 6667))
def ready():
x = s.recv(2048)
if "376" in x:
return True
else:
return False
def register(nick):
s.send("NICK %s\r\n" % nick)
s.send("USER %s 8 * : %s\r\n" % (nick, nick))
def join(channel):
s.send("JOIN %s\r\n" % channel)
def send_msg(reciever, message):
s.send("PRIVMSG %s :%s" % (reciever, message))
def send_raw(data):
s.send(data)
def recieve():
x = s.recv(2048)
if x != "":
return x
def username():
x = s.recv(2048)
return x.split("!")[0].strip(":")
def hostmask():
return x.split("!")[1].split(" ")[0]
def message(message):
x = s.recv(2048)
msg = x.rpartition("PRIVMSG %s :" % (channel))
return msg
def format_recv():
return "<%s> %s" % (username(), message())
def timestamp():
return "%s:%s:%s" % (time.localtime()[3], time.localtime()[4], time.localtime()[5])
def op(channel, reciever):
s.send("MODE %s +o %s\r\n" % (channel, reciever))
def deop(channel, reciever):
s.send("MODE %s -o %s\r\n" % (channel, reciever))
def voice(channel, reciever):
s.send("MODE %s +v %s\r\n" % (channel, reciever))
def devoice(channel, reciever):
s.send("MODE %s -v %s\r\n" % (channel, reciever))
def ban(channel, reciever):
s.send("MODE %s +b %s!*@*\r\n" % (channel, reciever))
def kick(channel, nick, reason=None):
s.send("KICK %s %s %s\r\n" % (channel, nick, reason))
def data_present(check):
x = s.recv(2048)
if check in x:
return True
else:
return False
def nick(nick):
s.send("NICK %s")
def disconnect(reason="Disconnected."):
s.send("QUIT :%s" % reason)
s.close()
if "PING" in s.recv(2048):
ping = s.recv.rstrip()
ping = split(ping)
s.send("PONG %s" % ping[1])
Here's all the commands you can currently use:
connect(server) - Connects to the IRC server.
ready() - Returns true if the end of the MOTD is reached.
register(nick) - Registers with the nick.
join(channel) - Joins the channel.
send_msg(reciever, message) - Sends a message to reciever.
send_raw(data) - Sends raw IRC data to the server.
recieve() - Returns the IRC data recieved if it is not empty.
username() - Returns the username of the last message sent.
hostmask() - Returns the hostmask of the last message sent.
message() - Returns the last message sent.
format_recv() - Returns the last message sent like you would see it in an IRC client.
timestamp() - Returns the current time as a string.
op(channel, reciever) - Ops the reciever.
deop(channel, reciever) - Deops the reciever.
voice(channel, reciever) - Gives the reciever voice.
devoice(channel, reciever) - Removes voice from the reciever.
ban(channel, reciever) - Bans the reciever.
kick(channel, reciever[, reason]) Kicks the reciever.
disconnect([reason]) - Disconnects from IRC.
nick(new_nick) - Changes your bot's nick.
data_present(check) - Returns true if <check> is in the reciving IRC data.
It also includes a ping responder, so your bot should never time out.
Soon, I'll attach the .py file and an installer batch file.
Yay! Download the install.zip, unzip it, run install.bat, and yay!
Bug reports and suggestions welcome!