Sha256: 50791a46af9414fc2b9c1355516a3c923c084257feaa0d200a0e4147d2e8698b

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module SlackRubyBot
  module Hooks
    class Message
      def call(client, data)
        return if message_to_self_not_allowed? && message_to_self?(client, data)
        return if bot_message_not_allowed? && bot_message?(client, data)

        data.text = data.text.strip if data.text
        result = child_command_classes.detect { |d| d.invoke(client, data) }
        result ||= built_in_command_classes.detect { |d| d.invoke(client, data) }
        result ||= SlackRubyBot::Commands::Unknown.tap { |d| d.invoke(client, data) }
        result
      end

      private

      def message_to_self_not_allowed?
        !SlackRubyBot::Config.allow_message_loops?
      end

      def message_to_self?(client, data)
        client.self && client.self.id == data.user
      end

      def bot_message_not_allowed?
        !SlackRubyBot::Config.allow_bot_messages?
      end

      def bot_message?(_client, data)
        data.subtype == 'bot_message'
      end

      #
      # All commands.
      #
      # @return [Array] Descendants of SlackRubyBot::Commands::Base.
      #
      def command_classes
        SlackRubyBot::Commands::Base.command_classes
      end

      #
      # All non-built-in, ie. custom commands.
      #
      # @return [Array] Non-built-in descendants of SlackRubyBot::Commands::Base.
      #
      def child_command_classes
        command_classes.reject do |k|
          k.name&.starts_with?('SlackRubyBot::Commands::')
        end
      end

      #
      # All built-in commands.
      #
      # @return [Array] Built-in descendants of SlackRubyBot::Commands::Base.
      #
      def built_in_command_classes
        command_classes.select do |k|
          k.name&.starts_with?('SlackRubyBot::Commands::') && k != SlackRubyBot::Commands::Unknown
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slack-ruby-bot-0.15.0 lib/slack-ruby-bot/hooks/message.rb
slack-ruby-bot-0.14.0 lib/slack-ruby-bot/hooks/message.rb