class Twitchbot::Bot

Main Bot class that we pass all bot account information to, as well as any plugins that should be used

Constants

DEFAULT_EVENTS

The events that eventmachine dispatches to any plugins in use

DEFAULT_PLUGINS

The built-in plugins to be used

DEFAULT_URL

The connection URL for Twitch

Attributes

channel[RW]

@return [Channel] The channel that the bot is connected to

command_prefix[RW]

@return [String] The prefix to use when defining and parsing commands e.g. +!+

debug[RW]

@return [Boolean] Whether the bot should display debug info to STDOUT

message_queue[RW]

@return [Array] The array of messages that are to be sent to the server

password[RW]

@return [String] Password of the bot account

plugins[RW]

@return [Array] The plugins that are in use by the bot

username[RW]

@return [String] Username of the bot account

Public Class Methods

new() { |self| ... } click to toggle source

Create a new Bot instance, passing a block to set the necessary attributes to have the bot function

# File lib/twitchbot/bot.rb, line 44
def initialize
  @username = ''
  @password = ''
  @channel = ''
  @plugins = []
  @message_queue = Queue.new
  @command_prefix = '!'

  yield self

  @channel = Channel.new(@channel)
end

Public Instance Methods

start() click to toggle source

Start the event loop, initiate the websocket client, and register the plugins with eventmachine

# File lib/twitchbot/bot.rb, line 59
def start
  EM.run do
    connection = Faye::WebSocket::Client.new DEFAULT_URL
    plugins = (@plugins << DEFAULT_PLUGINS).flatten!.reverse!.map! &:new
    DEFAULT_EVENTS.each do |default_event|
      connection.on(default_event) do |em_event|
        handler = EventHandler.new em_event, connection, self
        plugins.each do |plugin|
          plugin.send(default_event, handler)
        end
      end
    end
  end
end