lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.6.0 vs lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.6.1
- old
+ new
@@ -1,8 +1,9 @@
module SlackRubyBot
module Commands
class Base
+ include Loggable
class_attribute :routes
def self.send_message(client, channel, text, options = {})
logger.warn '[DEPRECATION] `send_message` is deprecated. Please use `client.say` instead.'
if text && text.length > 0
@@ -20,17 +21,10 @@
def self.send_gif(client, channel, keywords, options = {})
logger.warn '[DEPRECATION] `send_gif` is deprecated. Please use `client.say` instead.'
client.say(options.merge(channel: channel, text: '', gif: keywords))
end
- def self.logger
- @logger ||= begin
- $stdout.sync = true
- Logger.new(STDOUT)
- end
- end
-
def self.default_command_name
name && name.split(':').last.downcase
end
def self.operator(*values, &block)
@@ -72,20 +66,27 @@
def self.match(match, &block)
self.routes ||= {}
self.routes[match] = block
end
- private
-
def self.parse(client, data)
text = data.text
- return text unless data.channel && data.channel[0] == 'D' && data.user && data.user != SlackRubyBot.config.user_id
- client.names.each do |name|
- text.downcase.tap do |td|
- return text if td == name || td.starts_with?("#{name} ")
- end
- end
+ return text unless direct_message?(data) && message_from_another_user?(data)
+ return text if bot_mentioned_in_message?(text, client.names)
["#{client.name} #{text}", text]
+ end
+
+ def self.direct_message?(data)
+ data.channel && data.channel[0] == 'D'
+ end
+
+ def self.message_from_another_user?(data)
+ data.user && data.user != SlackRubyBot.config.user_id
+ end
+
+ def self.bot_mentioned_in_message?(text, bot_names)
+ bot_names = bot_names.join('|')
+ !!text.downcase.match(/\A(#{bot_names})\s|\A(#{bot_names})\z/)
end
def self.finalize_routes!
return if self.routes && self.routes.any?
command default_command_name