Author Topic: ircbot: Library for Python for making bots  (Read 3036 times)

0 Members and 1 Guest are viewing this topic.

Offline Spyro543

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1189
  • Rating: +74/-3
    • View Profile
ircbot: Library for Python for making bots
« on: November 19, 2011, 11:14:34 am »
I just made a library for Python 2.7 that allows you to easily create IRC bots.
Here is the source code:
Code: [Select]
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!
« Last Edit: November 19, 2011, 03:31:58 pm by Spyro543 »

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: ircbot: Library for Python for making bots
« Reply #1 on: November 19, 2011, 01:58:53 pm »
Nice, that might be useful :D

So you have to make an infinite loop that checks for message()?

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Netham45

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2103
  • Rating: +213/-4
  • *explodes*
    • View Profile
Re: ircbot: Library for Python for making bots
« Reply #2 on: November 19, 2011, 02:22:26 pm »
Instead of checking for 'End of /MOTD command', you should check for the number that is before it.

Code: [Select]
irc.server.net 376: End of /MOTD
IRC servers that run in different languages may change the End of /motd text, but the number will pretty much always be there.

http://www.mirc.net/raws/

Those are almost always better to look for than the text.
Omnimaga Admin

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: ircbot: Library for Python for making bots
« Reply #3 on: November 19, 2011, 02:53:20 pm »
Instead of checking for 'End of /MOTD command', you should check for the number that is before it.

Code: [Select]
irc.server.net 376: End of /MOTD
IRC servers that run in different languages may change the End of /motd text, but the number will pretty much always be there.

http://www.mirc.net/raws/

Those are almost always better to look for than the text.
Ahh, yeah...
* alberthrocks is guilty for telling Spyro to check for "End of /MOTD" :P

(I've always been using that method to check :P For user handling, I do string splitting.)

Either way, it looks like a very nice resource to use! :D
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline Spyro543

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1189
  • Rating: +74/-3
    • View Profile
Re: ircbot: Library for Python for making bots
« Reply #4 on: November 19, 2011, 03:08:38 pm »
I'll add more commands, like one to disconnect, one to change nickname, one to check if there is any data...

Offline Spyro543

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1189
  • Rating: +74/-3
    • View Profile
Re: ircbot: Library for Python for making bots
« Reply #5 on: November 19, 2011, 05:02:38 pm »
Just added a zip file to first post that contains install batch file and source. Also I added 3 more commands. (This is a legal double post since you're allowed to post again after an hour if it's a project update.)
« Last Edit: November 19, 2011, 05:09:40 pm by Spyro543 »