<commit_before>require 'rest-client'
require 'faye/websocket'
require 'eventmachine'

require 'discordrb/endpoints/endpoints'

require 'discordrb/events/message'

require 'discordrb/exceptions'
require 'discordrb/data'

module Discordrb
  class Bot
    include Discordrb::Events
    def initialize(email, password)
      @email = email
      @password = password

      @token = login()

      Thread.new { websocket_connect() }

      @event_handlers = {}
    end

    def message(attributes = {}, &block)
      @event_handlers[MessageEvent] ||= []
      @event_handlers[MessageEvent] << MessageEventHandler.new(attributes, block)
    end

    private

    def login
      # Login
      login_response = RestClient.post Discordrb::Endpoints::LOGIN, :email => @email, :password => <PASSWORD>
      raise HTTPStatusException.new(login_response.code) if login_response.code >= 400

      # Parse response
      login_response_object = JSON.parse(login_response)
      raise InvalidAuthenticationException unless login_response_object['token']

      login_response_object['token']
    end

    def websocket_connect
      EM.run {
        @ws = Faye::WebSocket::Client.new(Discordrb::Endpoints::WEBSOCKET_HUB)

        @ws.on :open do |event|; websocket_open(event); end
        @ws.on :message do |event|; websocket_message(event); end

        @ws.on :close do |event|<commit_msg>Extegrate the websocket connection into a separate method<commit_after>require 'rest-client'
require 'faye/websocket'
require 'eventmachine'

require 'discordrb/endpoints/endpoints'

require 'discordrb/events/message'

require 'discordrb/exceptions'
require 'discordrb/data'

module Discordrb
  class Bot
    include Discordrb::Events
    def initialize(email, password)
      @email = email
      @password = password

      @token = login

      @event_handlers = {}
    end

    def run
      websocket_connect
    end

    def message(attributes = {}, &block)
      @event_handlers[MessageEvent] ||= []
      @event_handlers[MessageEvent] << MessageEventHandler.new(attributes, block)
    end

    private

    def login
      # Login
      login_response = RestClient.post Discordrb::Endpoints::LOGIN, :email => @email, :password => <PASSWORD>
      raise HTTPStatusException.new(login_response.code) if login_response.code >= 400

      # Parse response
      login_response_object = JSON.parse(login_response)
      raise InvalidAuthenticationException unless login_response_object['token']

      login_response_object['token']
    end

    def websocket_connect
      EM.run {
        @ws = Faye::WebSocket::Client.new(Discordrb::Endpoints::WEBSOCKET_HUB)

        @ws.on :open do |event|; websocket_open(event); end
        @ws.on :message do |event|; websocket_message(event); end

        @ws.on :close do |event|
{'fraction_non_alphanumeric': 0.10231729055258466, 'fraction_numerical': 0.0021390374331550803, 'mean_word_length': 3.5185185185185186, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1029782', 'n_tokens_mistral': 855, 'n_tokens_neox': 826, 'n_words': 184}