Simple way to handle any bot mentions
I needed to handle any message that mentions bot. I didn't find how to do this simply and came to a little dirty scan:
scan(/\<@([[:alnum:][:punct:]]*)\>/) do |client, data, user_ids|
next unless user_ids.flatten.any? { |u_id| client.users[u_id].name == client.name }
# ...
end
Is there better way to achieve this? If not, I think it should be.
I think this is about right, but since you know the name upfront you could put it inside the scan with /bot|mybot|whateverbot/ if you want to optimize.
I'd love this documented in README, please?
Actually even better, maybe we should add a supported handler like mention do |client, data, ...| that wraps this up with tests & al? That'd be a great PR.
Actually even better, maybe we should add a supported handler like mention do |client, data, ...| that wraps this up with tests & al?
Yes, that's what I was talking about :+1: I'll try to make PR when I have time.
but since you know the name upfront you could put it inside the scan with /bot|mybot|whateverbot/ if you want to optimize
The problem is that as far as I undestand name is known only on after-connect time in client as I didn't configure it anywhere. That's why I came to this and asked better solution.
You're right @gsmetal. I also think whatever you come up with could work for any mention, so it could support mention do ... which would be a bot mention and mention do "dblock" that would be mentioning anyone else or even mentions do |array_of_people|, etc.