luk3yx / miniirc

A mini IRC framework.
MIT License
11 stars 1 forks source link

Handler as a method in a class #16

Closed impredicative closed 5 years ago

impredicative commented 5 years ago

I will come around to the other preexisting issue later, but for now I'm curious about registering a handler which is a method in a class, and not a standalone function.

For example, I can easily start a target method in a thread which automatically gets self as its first argument. In a similar manner, I'd like to be able to register a handler which automatically gets self as its first argument (assuming I registered it for a method, not a function). Is this possible? This could significantly simplify some code for me. If this description of the issue is not clear, I can share sample code. Thanks.

luk3yx commented 5 years ago

You could possibly do irc.Handler('PRIVMSG')(self.privmsg_func) inside your __init__.

impredicative commented 5 years ago

If I do that, will privmsg_func then receive self as its first argument whenever it's called for each PRIVMSG, or not?

luk3yx commented 5 years ago

It should (as long as you do it with self.function instead of Class.function). Example: https://github.com/luk3yx/lurklite/blob/master/lurklite/core.py#L254

impredicative commented 5 years ago

I used miniirc.Handler('PRIVMSG')(self.privmsg_func) in my __init__, and this works. It wouldn't hurt to have an example of this in the miniirc readme. Thanks.

impredicative commented 5 years ago

@luk3yx Whenever possible, I encourage documenting the above line or pattern of code somewhere in the readme. It is not obvious and is expected to be generally useful.

luk3yx commented 5 years ago

I have added some documentation, if there's a problem with it please let me know.