Page 1 of 1

Advice: Python IRC Bot

Posted: Tue Mar 05, 2013 7:06 pm
by Theladdie
Hey guys,

I am building an IRC bot in python. This is more a project for me to learn python.

What I wanted to do is for now when I say 'bot: hello' it would respond (thats the simple command I planned). Of course I want to add more commands so I don't want to have everything in one file but rather make plugin files with addtional code. On top of that for the hello example there might be other triggers like 'hi' or 'yo' which would give the same response...

So my question is do you know what would be the best way to check triggers and then get them to a function/plugin file to execute the code.

Re: Advice: Python IRC Bot

Posted: Wed Mar 06, 2013 9:03 am
by Theladdie
so to add, I don't want to use a frame work cause I prefer the deeper end (also I dabled with twisted and it did not really work out).

I think the only way is to check each plugin every time there is a message... But something tells me that is not efficient?

Re: Advice: Python IRC Bot

Posted: Wed Mar 06, 2013 9:40 am
by metachris
Plugins should register handlers for specific triggers. Then the main app can easily start them when a specific event occurs. There is a lot of stuff on stackoverflow and google; search for "python plugins".

Re: Advice: Python IRC Bot

Posted: Wed Mar 06, 2013 11:37 am
by Theladdie

Funny what one word does to your search.

I found the following link and am starting to crowbar it into my bot : ... lugin-api/

Re: Advice: Python IRC Bot

Posted: Wed Mar 06, 2013 12:22 pm
by -rst-
To implement the registration metachris mentioned, you could create a dictionary object to store the trigger+plugin tuples and each plugin could have a method to retrieve the triggers the plugin supports - something along:

Code: Select all

all_triggers = dict()
for i in pluginloader.getPlugins():
    print("Loading plugin " + i["name"])
    plugin = pluginloader.loadPlugin(i)
    plugin_triggers = plugin.get_triggers()
    for trg in plugin_triggers:
        all_triggers[trg] = plugin
and to use

Code: Select all

irc_input = ...
if irc_input in all_triggers:
    plugin = all_triggers[irc_input ]

Re: Advice: Python IRC Bot

Posted: Tue Mar 26, 2013 1:11 pm
by Theladdie

I sorta got it working

You can now see my butchered code here: