[Twisted-Python] Connect to an IRC server, modules.
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
On Nov 2, 2009, at 9:24 AM, Alberto Piu wrote:
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.
It's OK. Your english is not so bad. I think I understand your question :). I will try to use very simple english in my answer.
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!
Your explanation is not bad. But, the fix that you describe looks basically right to me. It is hard to fix a program that I cannot see. Can you attach a *complete*, but *simple* example, as described here: <http://sscce.org/
, and ask the question again?
Do not attach your whole IRC bot. Remove all the parts which are not a part of your problem, but make sure that, if I do 'python your-irc- bot.py', it will run and do something. Thanks, and good luck!
participants (2)
-
Alberto Piu
-
Glyph Lefkowitz