Class: Twitchbot::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/twitchbot/event_handler.rb

Overview

Class responsible for handling the eventmachine event that is provided whenever an event is fired

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, connection, bot) ⇒ EventHandler

Returns a new instance of EventHandler.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/twitchbot/event_handler.rb', line 14

def initialize(event, connection, bot)
  @connection = connection
  @bot = bot

  if event.respond_to? :data
    @chunks = event.data.split "\n"
    @messages = []
    @chunks.each do |chunk|
      @messages << Message.new(self, chunk.chomp)
    end
  end
end

Instance Attribute Details

#botBot (readonly)

Returns The bot that the channel is a member of.

Returns:

  • (Bot)

    The bot that the channel is a member of



8
9
10
# File 'lib/twitchbot/event_handler.rb', line 8

def bot
  @bot
end

#connectionFaye::WebSocket::Client (readonly)

Returns The WebSocket client instance.

Returns:

  • (Faye::WebSocket::Client)

    The WebSocket client instance



12
13
14
# File 'lib/twitchbot/event_handler.rb', line 12

def connection
  @connection
end

#messagesArray (readonly)

Returns The different messages received.

Returns:

  • (Array)

    The different messages received



10
11
12
# File 'lib/twitchbot/event_handler.rb', line 10

def messages
  @messages
end

Instance Method Details

#messageObject

Method that provides a shortcut to grab the first message in an event handler. We can typically use this after authenticating, but there is no guarantee that twitch will not send multiple ‘messages’ in a single :message event



46
47
48
# File 'lib/twitchbot/event_handler.rb', line 46

def message
  @messages.first
end

#send_channel(message) ⇒ Object

Add a formatted channel message to the message queue



33
34
35
# File 'lib/twitchbot/event_handler.rb', line 33

def send_channel(message)
  @bot.message_queue.push("PRIVMSG ##{bot.channel.name} :#{message}")
end

#send_raw(message) ⇒ Object

Add a raw message to the message queue



28
29
30
# File 'lib/twitchbot/event_handler.rb', line 28

def send_raw(message)
  @bot.message_queue.push(message)
end

#send_whisper(user, message) ⇒ Object

Add a whisper to the specified user to the message queue



38
39
40
# File 'lib/twitchbot/event_handler.rb', line 38

def send_whisper(user, message)
  @bot.message_queue.push("PRIVMSG #jtv :/w #{user} #{message}")
end