lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.12.0 vs lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.13.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require_relative 'support/match'
require_relative 'support/help'
module SlackRubyBot
module Commands
@@ -53,28 +55,34 @@
def invoke(client, data)
finalize_routes!
expression, text = parse(client, data)
return false unless expression || data.attachments
+
routes.each_pair do |route, options|
match_method = options[:match_method]
case match_method
when :match
next unless expression
+
match = route.match(expression)
match ||= route.match(text) if text
next unless match
next if match.names.include?('bot') && !client.name?(match['bot'])
+
match = Support::Match.new(match)
when :scan
next unless expression
+
match = expression.scan(route)
next unless match.any?
when :attachment
next unless data.attachments && !data.attachments.empty?
+
match, attachment, field = match_attachments(data, route, options[:fields_to_scan])
next unless match
+
match = Support::Match.new(match, attachment, field)
end
call_command(client, data, match, options[:block])
return true
end
@@ -120,10 +128,11 @@
def parse(client, data)
text = data.text
return text unless direct_message?(data) && message_from_another_user?(data)
return text if message_begins_with_bot_mention?(text, client.names)
+
["#{client.name} #{text}", text]
end
def direct_message?(data)
data.channel && data.channel[0] == 'D'
@@ -137,18 +146,20 @@
bot_names = bot_names.join('|')
!!text.downcase.match(/\A(#{bot_names})\s|\A(#{bot_names})\z/)
end
def finalize_routes!
- return if routes && routes.any?
+ return if routes&.any?
+
command command_name_from_class
end
def match_attachments(data, route, fields_to_scan = nil)
fields_to_scan ||= %i[pretext text title]
data.attachments.each do |attachment|
fields_to_scan.each do |field|
next unless attachment[field]
+
match = route.match(attachment[field])
return match, attachment, field if match
end
end
false