cinchrb / cinch

The IRC Bot Building Framework
http://www.rubydoc.info/gems/cinch
MIT License
1k stars 180 forks source link

Messages sent by bot do not fire a channel event #214

Closed elifoster closed 8 years ago

elifoster commented 8 years ago

It seems that messages sent by the bot do not fire the :channel event. Is there a different event just for messages sent by the bot itself? Looking at the log it should be firing, since it recognizes the message as a PRIVMSG.

elifoster commented 8 years ago

I just ran a little test using a plugin that says a random word in the channel using Message#reply, and a plugin to repeat what is said in the channel using Message#reply on a channel event. It only repeated the command message (in this case $randword), not the actual random word.

dominikh commented 8 years ago

Messages sent by the bot do not trigger events, and that is by design. Events are for incoming messages. Messages (as in PRIVMSG or NOTICE) that a client sends to the server are not sent back to the client, so there is no incoming message.

If you want your bot to react to its own messages (which, by the way, is an easy way of getting stuck in an infinite loop until the server admin bans you), you will have to do that explicitly. Cinch will not do that for you.

elifoster commented 8 years ago

The infinite loop thing is a good point, but that is only something to worry about if you are sending messages back to the server on the message events, and don't have proper checks for whether the message is being duplicated, or if it was the bot's own message.

If the message is not considering incoming, why does the log show the message sent by the bot just like any other message?

dominikh commented 8 years ago

but that is only something to worry about if you are sending messages back to the server on the message events, and don't have proper checks for whether the message is being duplicated, or if it was the bot's own message

That would describe roughly every IRC (and other protocol) bot ever written. Reacting to your own messages is highly atypical.

is there some simple way to fire events manually?

You can create events synthetically by using HandlerList#dispatch (and Bot#handlers to get to the HandlerList). However, a much simpler approach would be to simply call a method: process(the_stuff_im_sending) in your specific message-sending handler that also wants some additional processing done on the message.

There should really never be a reason for a bot to react to its own messages.

elifoster commented 8 years ago

However, a much simpler approach would be to simply call a method: process(the_stuff_im_sending) in your specific message-sending handler that also wants some additional processing done on the message.

That was what I was thinking of doing, but I figured I'd create the issue in case it was not by design.

There should really never be a reason for a bot to react to its own messages.

See Quintus' logger, or any other logger that looks similarly to this (not using the actual Logger stuff). Since it uses events rather than the Logger things provided by Cinch, it doesn't log the bots messages. Granted, I get your point, but there are reasons.

dominikh commented 8 years ago

But that logger could be implemented with the Logger interface. It would be in a rather roundabout way, as it'd need to parse the message again to check whether it was a public message and to access the attributes, but it would still be a more robust solution than the current one.

That is, a possible Logger implementation would overwrite log, drop all events that aren't :incoming or :outgoing, then construct Message objects via Message.new for the log messages.