class Twitch::Bot::Client
Twitch
chat client object
Twitch
chat client object
Constants
- MODERATOR_MESSAGES_COUNT
- TWITCH_PERIOD
- USER_MESSAGES_COUNT
Attributes
adapter[R]
channel[R]
config[R]
event_handlers[R]
event_loop_running[R]
input_thread[R]
memory[R]
messages_queue[R]
output_thread[R]
Public Class Methods
new( channel: nil, config:, &block )
click to toggle source
# File lib/twitch/bot/client.rb, line 24 def initialize( channel: nil, config:, &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
Public Instance Methods
add_moderator(user)
click to toggle source
# File lib/twitch/bot/client.rb, line 93 def add_moderator(user) channel.add_moderator(user) end
dispatch(event)
click to toggle source
# 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_channel()
click to toggle source
# File lib/twitch/bot/client.rb, line 66 def join_default_channel adapter.join_channel(@channel) if @channel end
part_channel()
click to toggle source
# File lib/twitch/bot/client.rb, line 70 def part_channel adapter.part_channel @channel = nil @messages_queue = [] end
register_handler(handler)
click to toggle source
Register an event handler for specific event types
@param [<EventHandler>] handler EventHandler
class to register
# 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)
click to toggle source
# File lib/twitch/bot/client.rb, line 97 def remove_moderator(user) channel.remove_moderator(user) end
run()
click to toggle source
# 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)
click to toggle source
# File lib/twitch/bot/client.rb, line 85 def send_data(data) adapter.send_data(data) end
send_message(message)
click to toggle source
# File lib/twitch/bot/client.rb, line 89 def send_message(message) messages_queue << message if messages_queue.last != message end
stop()
click to toggle source
# File lib/twitch/bot/client.rb, line 101 def stop dispatch StopEvent.new stop_event_loop part_channel if channel end
Private Instance Methods
event_loop_running?()
click to toggle source
# File lib/twitch/bot/client.rb, line 142 def event_loop_running? @event_loop_running end
execute_initialize_block(block)
click to toggle source
# File lib/twitch/bot/client.rb, line 169 def execute_initialize_block(block) if block.arity == 1 block.call self else instance_eval(&block) end end
max_messages_count()
click to toggle source
# File lib/twitch/bot/client.rb, line 183 def max_messages_count if channel.moderators.include?(config.setting("botname")) MODERATOR_MESSAGES_COUNT else USER_MESSAGES_COUNT end end
message_delay()
click to toggle source
# File lib/twitch/bot/client.rb, line 191 def message_delay TWITCH_PERIOD / max_messages_count end
register_default_handlers()
click to toggle source
# File lib/twitch/bot/client.rb, line 177 def register_default_handlers register_handler(Twitch::Bot::Client::PingHandler) register_handler(Twitch::Bot::Client::AuthenticatedHandler) register_handler(Twitch::Bot::Client::ModeHandler) end
set_traps()
click to toggle source
# File lib/twitch/bot/client.rb, line 128 def set_traps %w[TERM INT].each { |signal| trap(signal) { stop } } end
setup_logging()
click to toggle source
# File lib/twitch/bot/client.rb, line 112 def setup_logging Twitch::Bot::Logger.output = config.setting(:log_file) || "twitchbot.log" Twitch::Bot::Logger.level = (config.setting(:log_level) || "info").to_sym end
start_event_loop()
click to toggle source
# File lib/twitch/bot/client.rb, line 132 def start_event_loop raise "Already running" if event_loop_running? @event_loop_running = true end
start_input_thread()
click to toggle source
# File lib/twitch/bot/client.rb, line 146 def start_input_thread Twitch::Bot::Logger.debug("Starting input thread...") @input_thread = Thread.start do while event_loop_running? event = adapter.read_data dispatch(event) end end end
start_output_thread()
click to toggle source
# File lib/twitch/bot/client.rb, line 156 def start_output_thread Twitch::Bot::Logger.debug("Starting output thread...") @output_thread = Thread.start do while event_loop_running? sleep message_delay if (message = messages_queue.pop) adapter.send_message(message) end end end end
startup()
click to toggle source
# File lib/twitch/bot/client.rb, line 119 def startup set_traps start_event_loop start_input_thread start_output_thread adapter.connect Twitch::Bot::Logger.debug "Started." end
stop_event_loop()
click to toggle source
# File lib/twitch/bot/client.rb, line 138 def stop_event_loop @event_loop_running = false end