Sha256: d4b36b72937ba910c8efdfb828ac12c1096581b0f18540d8dba858fe788bf69e

Contents?: true

Size: 874 Bytes

Versions: 2

Compression:

Stored size: 874 Bytes

Contents

# frozen_string_literal: true

module Twitch
  module Bot
    module Message
      # This class stores the details of a user's chat message.
      class UserMessage < Base
        attr_reader :text, :user

        def initialize(text:, user:)
          @text = text
          @user = user
          @type = :user_message
        end

        def command_name?(check_command)
          command == check_command
        end

        def command?
          !(command.nil? || command.empty?)
        end

        def command
          first_word.match(/^!(\w+)/) do |match|
            match.captures&.first
          end
        end

        def command_args
          text_words.tap(&:shift)
        end

        private

        def text_words
          text.split(/\s+/)
        end

        def first_word
          text_words.first
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitch-bot-2.0.1 lib/twitch/bot/message/user_message.rb
twitch-bot-2.0.0 lib/twitch/bot/message/user_message.rb