[Twisted-Python] Connect to an IRC server, modules.

Alberto Piu piu.alberto at gmail.com
Mon Nov 2 07:24:56 MST 2009


Greetings,

first of all, I apologize for bad english. I am new with Twisted, and  
although it seems very powerful, I'm having some problems in using it.
I'm trying to create an IRC bot, which basically does nothing. My idea  
is to write a skeleton that's improved by modules, which do different  
things in an IRC channel.
When some events happen some modules become active, so that they can  
send messages, or notices, or private messages, and all what an IRC  
bot can do; so, every module has to have complete access to the  
methods of the bot class, but in the same time I need to keep the two  
things separated, to improve modularity.

I used an example, called LogBot example. I have a .py file (I'll call  
it "mainfile), in which the LogBot class is located, where the reactor  
is set up etc, then I have a dir called modules, where modules are.

- mainfile.py
- modules
	- modulea
		- modulea.py
	- moduleb
		- moduleb.py

(I omitted __init__.py files).

Now the question is: how I can make accessible all the methods of the  
LogBot class from EVERY .py inside the modules directory? I tried to  
do the following:
In the mainfile, inside the LogBot class, I have

def joined(self, channel):
         """This will get called when the bot joins the channel."""
         self.logger.log("[I have joined %s]" % channel)

This method is called when the bot joins the channel "channel".
Know, if I want to use modulea to greet user "x" when I enter the  
channel, I would write:

def joined(self, channel):
         """This will get called when the bot joins the channel."""
         self.logger.log("[I have joined %s]" % channel)
	modulea.greet("x", "Hi, user x")

where greet is a function inside the module modulea (imported  
somewhere in my mainfile).
Anyway, modulea is defined as following:

def greet(who, message):
	## But here, how can the function know the method to use?

The solution can be...

##In mainfile.py
def joined(self, channel):
         """This will get called when the bot joins the channel."""
         self.logger.log("[I have joined %s]" % channel)
	modulea.greet(self, "x", "Hi, user x")

##In module a:
def greet(bot, who, message):
	bot.msg(who, message)

But this does not work.
I tried to explain as better as I could. If you can, please use  
examples and speak easy (can't understand very well).
Hope you can help me!

Thank you, in advance.
--Alberto Piu




More information about the Twisted-Python mailing list