rmmh / skybot

Python IRC bot
https://github.com/rmmh/skybot/wiki
The Unlicense
249 stars 170 forks source link

Add timer hooks #171

Open Red-M opened 6 years ago

Red-M commented 6 years ago

105 😃

Timers are called when someone speaks anywhere for the first time and only once. Use @hook.event('004') to start timers on bot connect.

An example:

from util import hook

@hook.timer(10)
def test_timer(inp, bot=None):
    print(dir(bot))

An example for messaging a channel using default config values and at bot start up:

from util import hook

@hook.event('004')
@hook.timer(10)
def test_timer(inp, bot=None):
    bot.conns['local irc'].msg("#channel','text')
gtwy commented 6 years ago

Thank you

gtwy commented 6 years ago

There seem to be two bugs with this. 1). The timer does not start until someone interacts with the bot. 2). "return" and "print" don't print to IRC. (they do, however, print to the console window.)

I made a fresh clone of your fork to make sure the issue wasn't from something else and it's still happening. Using your exact example plugin code, too. I'm sure I'm missing something obvious here, but "say", "message", "return" and "print" don't send text to IRC. I tried to look at the command function to see how it passes information to IRC, and all I can tell is that it just "returns" it.

It looks like dispatch doesn't have a way to handle input from a timer. See here: https://github.com/Red-M/skybot/blob/f051014fca683b3f309ef9655fd513f22c9fef35/core/main.py#L132-L153

Red-M commented 6 years ago

You just need to find the IRC server object in bot then you can use a .msg

I did put that timers won't start until someone says something in the commits. Timers are called when someone speaks anywhere for the first time. Use @hook.event('004') to start timers on bot connect.

An example for messaging a channel using default config values:

bot.conns['local irc'].msg("#channel','text')

I've included this in the PR comment for future use.

Red-M commented 6 years ago

Did this work for you?

vesatoivonen commented 3 years ago

Prevention of duplicate timers should also be implemented in my opinion. Event 004 can trigger multiple times if the network connection is flaky, which would lead to multiple running timers for the same hook.