MetaChat REGISTER   ||   LOGIN   ||   IMAGES ARE OFF   ||   RECENT COMMENTS




artphoto by splunge
artphoto by TheophileEscargot
artphoto by Kronos_to_Earth
artphoto by ethylene

Home

About

Search

Archives

Mecha Wiki

Metachat Eye

Emcee

IRC Channels

IRC FAQ


 RSS


Comment Feed:

RSS

03 July 2006

Ask MetaChat: Programming. I've got an IRC bot that I've customized a fair bit (the example bot that comes with Perl's NET::IRC module). [More:]

The bot is entirely reactive, ie, it waits for someone to say something, then it responds accordingly. Until something happens on the channel, the bot is in a waiting state.

I'd like to make it aware of time, and be able to perform actions at specific times. What's the best way to do this? The easiest way would be to have it check the time every time it receives a channel event. This is clearly not a good solution. Is there a better way to do this without changing the entire architecture of the program?
*ponders possibility that most of #bunnies is made up of bunniebots*

Well, that explains a lot...
posted by wendell 03 July | 13:18
No one has ever loved me as much as the bots I create.
posted by agropyron 03 July | 13:40
You could use alarm().
posted by cmonkey 03 July | 13:47
If you want things at specific times, I'd just have timers run for everything. If you can get it to check the time once a day and then do the math for each of the timers, that'd probably be your best bet.
posted by Eideteker 03 July | 13:52
What you're describing is the reason people use threads. To keep from complicating your existing program, create a separate thread that calls

time_t time ( time_t * timer );

or whatever in your language of choice returns something similar to a time_t (which in C is just the number of seconds since 00:00 UTC 1 Jan 1970).

Then in your thread, sleep for the lesser of n seconds or the time to your bot's next scheduled activity.

On a scheduled activity, send a message to your existing thread; this can be as simple as synthesizing a fake input for it. ("AlarmFakeName: Hey bot, react to this")

Your existing thread continues being completely reactive, all you change in it is adding a response to the fake timer input.

(What cmonkey suggests is basically the same, but you have to write a SIGALARM handler; having a separate thread is probably easier to extend.)


posted by orthogonality 03 July | 23:20
Thank you for the answers! I shall ponder on these words.
posted by agropyron 04 July | 00:26
Anybody else but me || They're killing Screenhead!

HOME  ||   REGISTER  ||   LOGIN