Class: Twitch::Bot::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/twitch/bot/client.rb,
lib/twitch/bot/default_handlers.rb

Overview

Twitch chat client object

Defined Under Namespace

Classes: AuthenticatedHandler, ModeHandler, PingHandler, StopEvent

Constant Summary collapse

MODERATOR_MESSAGES_COUNT =
100
USER_MESSAGES_COUNT =
20
TWITCH_PERIOD =
30.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, channel: nil, &block) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/twitch/bot/client.rb', line 24

def initialize(
  config:, channel: nil, &block
)
  @config = config
  @channel = Twitch::Bot::Channel.new(channel) if channel

  @messages_queue = []
  @event_handlers = {}
  @event_loop_running = false

  setup_logging

  memory_class = config.setting("memory") || "Twitch::Bot::Memory::Hash"
  @memory = Object.const_get(memory_class).new(client: self)

  adapter_class = config.setting("adapter") || "Twitch::Bot::Adapter::Irc"
  @adapter = Object.const_get(adapter_class).new(client: self)

  execute_initialize_block block if block
  register_default_handlers
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



22
23
24
# File 'lib/twitch/bot/client.rb', line 22

def channel
  @channel
end

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/twitch/bot/client.rb', line 22

def config
  @config
end

#memoryObject (readonly)

Returns the value of attribute memory.



22
23
24
# File 'lib/twitch/bot/client.rb', line 22

def memory
  @memory
end

Instance Method Details

#add_moderator(user) ⇒ Object



93
94
95
# File 'lib/twitch/bot/client.rb', line 93

def add_moderator(user)
  channel.add_moderator(user)
end

#dispatch(event) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/twitch/bot/client.rb', line 76

def dispatch(event)
  type = event.type
  Twitch::Bot::Logger.debug "Dispatching #{type}..."
  (event_handlers[type] || []).each do |handler_class|
    Twitch::Bot::Logger.debug "Calling #{handler_class}..."
    handler_class.new(event: event, client: self).call
  end
end

#join_default_channelObject



66
67
68
# File 'lib/twitch/bot/client.rb', line 66

def join_default_channel
  adapter.join_channel(@channel) if @channel
end

#part_channelObject



70
71
72
73
74
# File 'lib/twitch/bot/client.rb', line 70

def part_channel
  adapter.part_channel
  @channel = nil
  @messages_queue = []
end

#register_handler(handler) ⇒ Object

Register an event handler for specific event types

Parameters:

  • handler (<EventHandler>)

    EventHandler class to register



51
52
53
54
55
56
# File 'lib/twitch/bot/client.rb', line 51

def register_handler(handler)
  handler.handled_events.each do |event_type|
    (event_handlers[event_type] ||= []) << handler
    Twitch::Bot::Logger.debug "Registered #{handler} for #{event_type}"
  end
end

#remove_moderator(user) ⇒ Object



97
98
99
# File 'lib/twitch/bot/client.rb', line 97

def remove_moderator(user)
  channel.remove_moderator(user)
end

#runObject



58
59
60
61
62
63
64
# File 'lib/twitch/bot/client.rb', line 58

def run
  startup

  # Wait for threads to finish
  input_thread.join
  output_thread.join
end

#send_data(data) ⇒ Object



85
86
87
# File 'lib/twitch/bot/client.rb', line 85

def send_data(data)
  adapter.send_data(data)
end

#send_message(message) ⇒ Object



89
90
91
# File 'lib/twitch/bot/client.rb', line 89

def send_message(message)
  messages_queue << message if messages_queue.last != message
end

#stopObject



101
102
103
104
105
# File 'lib/twitch/bot/client.rb', line 101

def stop
  dispatch StopEvent.new
  stop_event_loop
  part_channel if channel
end