<commit_before>module Kirpich
  class Bot
    include Virtus.model

    attribute :client
    attribute :self_id, String
    attribute :state,   Hash, default: {}

    def on_message(data)
      request = Kirpich::Request.new(data)

      return unless can_respond?(request)

      state[request.channel] ||= {}
      answer = Kirpich::Brain.respond_on(request)

      return unless answer
      response = eval_answer(answer, request, state[request.channel])

      return unless response

      state[request.channel] = state[request.channel].merge(response.state)
      send_response(response, request)
    end

    def on_hello
      Kirpich.logger.info 'I am touch!'
      random_post_timer
    end

    private

    def can_respond?(request)
      return false if self_id == request.user
      return false if request.bot_message? || request.changed_message?
      return false if request.text.empty?

      true
    end

    def eval_answer(answer, request, state)
      Kirpich.logger.info "Respond with #{answer.type}"

      if answer.type == :last_answer
        answer = state[:last_answer]
      else
        state[:last_answer] = answer
      end

      return unless answer

      method_object = Kirpich::Answers.method(answer.type)
      method_object.call(request, state, *answer.args)
    end

    def send_response(response, request)
      return unless response.body.any?

      response.body.each do |text|
        client.post text, request
      end
    end

    def random_post_timer
      time = 10000 + rand(5000)

      client.post_after(time) do
        request = Kirpich::Request.new channel: %w(C08189F96 G084E5SC9).sample
        answer = eval_answer(Kirpich::Brain.random_response, request, {})
        send_response(answer, request)

        random_post_timer
      end
    end
  end
end
<commit_msg>Less random<commit_after>module Kirpich
  class Bot
    include Virtus.model

    attribute :client
    attribute :self_id, String
    attribute :state,   Hash, default: {}

    def on_message(data)
      request = Kirpich::Request.new(data)

      return unless can_respond?(request)

      state[request.channel] ||= {}
      answer = Kirpich::Brain.respond_on(request)

      return unless answer
      response = eval_answer(answer, request, state[request.channel])

      return unless response

      state[request.channel] = state[request.channel].merge(response.state)
      send_response(response, request)
    end

    def on_hello
      Kirpich.logger.info 'I am touch!'
      random_post_timer
    end

    private

    def can_respond?(request)
      return false if self_id == request.user
      return false if request.bot_message? || request.changed_message?
      return false if request.text.empty?

      true
    end

    def eval_answer(answer, request, state)
      Kirpich.logger.info "Respond with #{answer.type}"

      if answer.type == :last_answer
        answer = state[:last_answer]
      else
        state[:last_answer] = answer
      end

      return unless answer

      method_object = Kirpich::Answers.method(answer.type)
      method_object.call(request, state, *answer.args)
    end

    def send_response(response, request)
      return unless response.body.any?

      response.body.each do |text|
        client.post text, request
      end
    end

    def random_post_timer
      time = 20000 + rand(5000)

      client.post_after(time) do
        request = Kirpich::Request.new channel: %w(C08189F96 G084E5SC9).sample
        answer = eval_answer(Kirpich::Brain.random_response, request, {})
        send_response(answer, request)

        random_post_timer
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08679039301310043, 'fraction_numerical': 0.01146288209606987, 'mean_word_length': 2.6723446893787575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6674142', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1068, 'n_words': 259}