1. Welcome to Smogon! Check out the Smogon Starters Hangout for everything you need to know about starting out in the community. Don't forget to introduce yourself in the Introduction and Hangout Thread, too!
  2. Welcome to Smogon Forums! Please take a minute to read the rules.

Programming Pierce - event based IRC framework

Discussion in 'Technical Projects' started by rory, Oct 13, 2009.

  1. rory

    rory T^T
    is a Site Staff Alumnusis a Battle Server Moderator Alumnus

    Joined:
    Oct 13, 2007
    Messages:
    210
    This is a project I have been working on for quite a while now and I am very glad to say that it is more or less finished. What inspired this was creating a "BaseBot" for #stark that would help people out spitting out information and doing calculations. As it progressed I decided I wanted to clean it up and write a framework in python that would make it easy to add on to the Uxie (the basebot) and maintain clean code. I haven't written Uxie in the framework yet, since I haven't bothered to write my pokemon data package yet, but I'm still releasing this framework.

    It is, as far as I know, the only event based IRC framework for python3.

    The files for the project and all the source code can be found here.

    An example of the simplicity of this framework is as follows:

    Code:
    [rory@localhost ~]$ python3
    Python 3.1.1 (r311:74480, Aug 24 2009, 15:50:43) 
    [GCC 4.4.0 20090506 (Red Hat 4.4.0-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from pierce.irc import make_project
    >>> make_project('uxie')
    >>> 
    [rory@localhost ~]$ cd uxie
    [rory@localhost uxie]$ ls
    packages  run.py  settings.py
    
    The project is now created. As you can see the make_project function created a packages folder, a run.py file, and a settings.py file.

    I then went and created a file in the packages folder called uxie.py and wrote the following in it:

    Code:
    import re
    
    from pierce.irc.templates import Handler, Package
    
    class MyHandler(Handler):
        # have the handler respond to messages
        event = ['privmsg']
        
        # only on the channel #rorytest!
        chan = ['#rorytest']
    
        # Any message must match the following regexp for the handler to be called
        regexp = [re.compile('(Uxie: )?What do you do?', re.IGNORECASE)]
        
        # what to do when the handler is called
        def __call__(self, event):
            # message the event channel
            event.chan.msg("I don't do shit yet!")
    
    # a package with my handler in it
    class Uxie(Package):
        contents = [ MyHandler ]
    and settings.py (most of which was already there):

    Code:
    # Server and port to connect to
    server = 'irc.synirc.net'
    port = 6667
    
    # Nick, user, and realname to connect with
    # Leave user None to use the same nick and username
    nick = 'Uxie'
    user = None
    realname = 'A Pierce IRC Bot'
    
    # Packages to add to the connection
    from packages.uxie import Uxie  # importing the package I just created
    from pierce.irc.packages.printer import Printer
    from pierce.irc.packages.extras import AutoIdentify
    packages = [ Uxie, Printer, AutoIdentify ] # all packages here will be added
    
    # Channels to automatically join on connection
    auto_join = ['#rorytest']
    
    # Passwords for specific nicks
    passwords = {'uxie': 'XXXXXXXX'}
    Then all i need to do is:
    Code:
    [rory@localhost uxie]$ python3 run.py connect
    and uxie runs with the following output:
    Code:
    [20:43:23] >> USER Uxie Uxie Uxie :A Pierce IRC Bot
    [20:43:23] >> NICK Uxie
    [20:43:23] {AUTH} -avarice.az.us.synirc.net - *** Looking up your hostname...
    [20:43:23] {AUTH} -avarice.az.us.synirc.net - *** Checking ident...
    [20:43:23] {AUTH} -avarice.az.us.synirc.net - *** No ident response; username prefixed with ~
    [20:43:24] {AUTH} -avarice.az.us.synirc.net - *** Couldn't resolve your hostname; using your IP address instead
    [20:43:24] << Welcome to the synIRC IRC Network Uxie!~Uxie@12.24.38.132
    [20:43:24] << Your host is avarice.az.us.synirc.net, running version Unreal3.2.6
    [20:43:24] << This server was created Tue Jul 17 2007 at 10:16:41 GMT
    [20:43:24] << avarice.az.us.synirc.net Unreal3.2.6 iowghraAsORTVSxNCWqBzvdHtGp lvhopsmntikrRcaqOALQbSeIKVfMCuzNTGj
    [20:43:24] <<  CMDS=KNOCK,MAP,DCCALLOW,USERIP NAMESX SAFELIST HCN MAXCHANNELS=20 CHANLIMIT=#:20 MAXLIST=b:60,e:60,I:60 NICKLEN=30 CHANNELLEN=32 TOPICLEN=307 KICKLEN=307 AWAYLEN=307 MAXTARGETS=20 :are supported by this server
    [20:43:24] <<  WALLCHOPS WATCH=128 SILENCE=15 MODES=12 CHANTYPES=# PREFIX=(qaohv)~&@%+ CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG NETWORK=synIRC CASEMAPPING=ascii EXTBAN=~,cqnr ELIST=MNUCT STATUSMSG=~&@%+ EXCEPTS :are supported by this server
    [20:43:24] <<  INVEX :are supported by this server
    [20:43:24] << There are 39 users and 5131 invisible on 12 servers
    [20:43:24] <<  31 :operator(s) online
    [20:43:24] <<  1354 :channels formed
    [20:43:24] << I have 741 clients and 0 servers
    [20:43:24] << Current Local Users: 741  Max: 1005
    [20:43:24] << Current Global Users: 5170  Max: 6288
    [20:43:24] << - avarice.az.us.synirc.net Message of the Day -
    [20:43:24] << - 28/11/2008 22:10
    [20:43:24] << -                           ___ ____   ____
    [20:43:24] << -           ___ _   _ _ __ |_ _|  _ \ / ___|
    [20:43:24] << -          / __| | | | '_ \ | || |_) | |
    [20:43:24] << -          \__ \ |_| | | | || ||  _ <| |___
    [20:43:24] << -          |___/\__, |_| |_|___|_| \_\\____|
    [20:43:24] << -               |___/      Phoenix, AZ, USA
    [20:43:24] << -
    [20:43:24] << - Server     : avarice.az.us.synirc.net
    [20:43:24] << - Ports      : 6660-9, 7000
    [20:43:24] << - SSL Ports  : 7001, 8000
    [20:43:24] << - Admin      : Sony     (sony@synirc.net)
    [20:43:24] << - Coadmin    : Special  (special@synirc.net)
    [20:43:24] << - IRCop      : Josh     (josh@synirc.net)
    [20:43:24] << -            : Pak000   (pak000@synirc.net)
    [20:43:24] << -
    [20:43:24] << - Please visit us online at http://www.synirc.net/
    [20:43:24] << - For network related assistance please join #Help
    [20:43:24] << - For network wide bans and other issues email helpcom@synirc.net
    [20:43:24] << -
    [20:43:24] << - synIRC.net will scan your IP for open proxies upon connection to our
    [20:43:24] << - services. You must be 18 or older to connect and chat on our services, or
    [20:43:24] << - have parental consent. synIRC.net and its affiliates are not responsible
    [20:43:24] << - for any bodily or mental harm that could possibly come from using its
    [20:43:24] << - services. synIRC.net is not responsible for any data received or sent
    [20:43:24] << - out to our clients. By connecting, you consent to have logging of your
    [20:43:24] << - conversations done by other clients and are also bound to the rules and
    [20:43:24] << - regulations that can be found at http://www.synirc.net/charter/
    [20:43:24] << End of /MOTD command.
    [20:43:24] {Uxie} *** Uxie  sets mode +ix
    [20:43:24] {Uxie} -Global- [Logon News - Aug 02 2009] Follow synIRC on Twitter! http://twitter.com/synirc
    [20:43:24] {Uxie} -NickServ- This nickname is registered and protected.  If it is your
    [20:43:24] {Uxie} -NickServ- nick, type /msg NickServ IDENTIFY password.  Otherwise,
    [20:43:24] {NickServ} <Uxie> IDENTIFY XXXXXXXX
    [20:43:24] {Uxie} -NickServ- please choose a different nick.
    [20:43:24] {Uxie} -NickServ- If you do not change within 20 seconds, I will change your nick.
    [20:43:24] {Uxie} *** services.synirc.net  sets mode +r
    [20:43:24] {Uxie} -NickServ- Password accepted - you are now recognized.
    [20:43:24] {Uxie} -NickServ-  Automatically joining #rorytest .
    [20:43:24] {#rorytest} Uxie has joined #rorytest
    [20:43:24] {Uxie} -NickServ-  Automatically joining #stark .
    [20:43:24] {#stark} Uxie has joined #stark
    [20:43:24] {#rorytest} *** ChanServ sets mode +qo Uxie Uxie
    [20:43:31] {#stark} <Jibaku> Uxie is back!
    [20:43:35] {#stark} <Thorns> ok
    [20:43:38] {#stark} <Thorns> time to get to work
    [20:43:40] {#stark} <Theorymon> gives me an excuse to use it with Starmie + Kyogre + Ho-oh
    [20:43:40] {#stark} <Rory> it doesn't do shit yet
    [20:43:49] {#stark} <Rory> it just autojoined all my channels since it's in my group
    [20:43:51] {#rorytest} <Rory> hi
    [20:44:18] {#rorytest} <Rory> uxie: what do you do?
    [20:44:18] {#rorytest} <Uxie> I don't do shit yet!
    As you can see at the end of the log, Uxie responds to the handler I just wrote.

    In the near future I plan to do the following:


    • Finish my pokemon data package
    • Write Uxie
    • Write some fun game bots for #fluodome
    • Write logging packages and other extras for the framework
    • Make settings.py and run.py a bit better
    • Documentation and a tutorial
  2. rory

    rory T^T
    is a Site Staff Alumnusis a Battle Server Moderator Alumnus

    Joined:
    Oct 13, 2007
    Messages:
    210
    Sneak preview of what the Uxie source will be like, with descriptive comments.

    Stayed up pretty late finishing up the pkdata package. It's not completely done yet, but enough for me to be able to sleep well. The new !damage command is going to be far more "customizable" than it has been in the past. For example you will be able to set things like: weather=sun attacker_status=paralysis modifiers="light screen, flash fire"


    (code no longer relevant, see below post)
  3. rory

    rory T^T
    is a Site Staff Alumnusis a Battle Server Moderator Alumnus

    Joined:
    Oct 13, 2007
    Messages:
    210
    Had a little bit of time today, so I finished up my pokemon data library. It will be paired with Pierce to make Uxie extremely powerful and fast. I decided to go with sqlite3 whose library is installed with python3 by default. I was originally going to use the shoddybattle xml files, but they do not have as much data in them as the database files I converted from veekun's mysql pokedex dump. It would seem all that is left now is using the IRC framework with this pokemon data library to create Uxie. Shouldn't be too much work, the only problem is me finding time to do it.

    Here is an example of how the pokemon library is used.

    Code:
    >>> import pokelib.dp.pokemon
    Generating Pokémon
    ...Generating #1-100
    ...Generating #101-200
    ...Generating #201-300
    ...Generating #301-400
    ...Generating #401-500
    >>> Charizard = pokelib.dp.pokemon.get.by_name('charizard') # Get the Charizard base class
    >>> Charizard.types
    ('Fire', 'Flying')
    >>> Charizard.abilities
    ('Blaze',)
    >>> Charizard.weight
    905.0
    >>> Charizard.height
    17.0
    >>> Charizard.bases
    (78, 84, 78, 109, 85, 100)
    >>> char = Charizard() # Create a Charizard with default attributes
    >>> char.ivs
    (31, 31, 31, 31, 31, 31)
    >>> char.evs
    (0, 0, 0, 0, 0, 0)
    >>> char.nature
    'Hardy'
    >>> char2 = Charizard(nature='brave') # Create a brave Charizard
    >>> char2.nature
    'Brave'
    >>> char.get_stats()
    (297, 204, 192, 254, 206, 236)
    >>> char2.get_stats()
    (297, 224, 192, 254, 206, 212)

Users Viewing Thread (Users: 0, Guests: 0)