I have a bot in node.js and I'm wondering what logic I should use for a .tell
function. The purpose of the function will be to give a message to users who are offline. So, if john
was offline on freenode.net/#foo and I typed .tell john your mother was great last night
, I want the bot to store this message and display it whenever john
joins the channel.
If john
is already in the channel, I want the bot to immediately just display the message.
I am already using mongodb
as a database to store information, so I can probably store all my messages in there. Here's the logic I had in mind so far:
1. After user A types .tell john your mother was great
, a listener will pickup the pattern /\.tell ([\w-]+) (.*)/
and store a variable for the message ( var msg
, the user who posted the message ( var from
), and the name of the user to be messaged ( var to
). I will also have a global variable signifying the channel name and network of the server.
2. I'll store a row in mongodb such as:
to from message network channel delivered time
--------------------------------------------------------------------------------
john meder your mother was great freenode.net #foobar 0 (TS)
last night
3. Setup a listener for anyone joining a channel since the bot can live in multiple channels under one network, and detect if there any messages that are not delivered and if there are, attempt to match the to
and network
and channel
to the stored messages, and if row(s) are found, display them.
This sounds ok, but can anyone review it and maybe offer advice? I'll be forced to have a listener listen for anyone entering any channel, right? There's no alternative to that?
This seems all fine. I don't see how you could not have a listener.
One thing I'd add is maybe a hash in the bot to keep track of who has pending messages. This way you won't be querying MongoDB every time someone joins a channel.
I.e.
messages = {};
messages['freenode.net'] = {};
messages['freenode.net']['john'] = 1;
Might be overkill, but if your monitoring a channel that has many JOINs, you might find yourself querying MongoDB a lot.
My two cents.