Python IRC bot using Twisted

Jean-Paul Calderone exarkun at divmod.com
Tue Jul 3 12:08:14 EDT 2007


On Tue, 03 Jul 2007 15:51:30 -0000, ddtm <dr.death.tm at gmail.com> wrote:
>On 3    , 17:55, Jean-Paul Calderone <exar... at divmod.com> wrote:
>> On Tue, 03 Jul 2007 13:44:34 -0000, ddtm <dr.death... at gmail.com> wrote:
>> >On 3    , 16:01, Jean-Paul Calderone <exar... at divmod.com> wrote:
>> > [snip]
>>
>> >Thank you very much! It's a very useful information. One more
>> >question: can I cancel the DelayedCall using its ID (it is returned
>> >from callLater(...)) from another function? In example bot there are
>> >two functions:
>> >def joined(self, channel):
>> >  ...
>> >def privmsg(self, user, channel, msg):
>> >  ...
>> >For example, I add callLater(...) to joined(...) function and I'd like
>> >to cancel this in privmsg(...) function. What should I do?
>>
>> Yep.  The object callLater returns has a `cancel' method (some others, too)
>> which will prevent the function from being called at the scheduled time.
>>
>> Jean-Paul
>
>I know what you are talking about, but the question is: How can I
>cancel scheduled task in function that differs from
>function where I scheduled the task? Demo bot on Twisted website has a
>class with a bunch of predefined functions joined(...), privmsg(...)
>and so on. I can't see any method of communicating these functions.
>I'm new to Python and to Twisted framework.
>The task is:
>to call callLater(...) in joined(...)
>to cancel the task in privmsg(...) on special condition

You need to preserve a reference to the object.  For example:

    class IRCBot(Whatever):
        def joined(...):
            self.announceJoinedCall = reactor.callLater(...)

        def privmsg(...):
            self.announceJoinedCall.cancel()
            self.announceJoinedCall = None

This skips over a bunch of details (what happens on multiple calls to
joined, what happens if privmsg is called after announceJoinedCall has
run, etc) but I hope it demonstrates the basic idea.

Jean-Paul



More information about the Python-list mailing list