Class: Twitchbot::Bot

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

Overview

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

Constant Summary collapse

DEFAULT_URL =

The connection URL for Twitch

'wss://irc-ws.chat.twitch.tv'.freeze
DEFAULT_PLUGINS =

The built-in plugins to be used

[AuthPlugin,
PingPlugin,
ChannelPlugin,
MessageQueuePlugin,
DebugPlugin].freeze
DEFAULT_EVENTS =

The events that eventmachine dispatches to any plugins in use

%i[error close open message].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Bot

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

Yields:

  • (_self)

Yield Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#channelChannel

Returns The channel that the bot is connected to.

Returns:

  • (Channel)

    The channel that the bot is connected to



21
22
23
# File 'lib/twitchbot/bot.rb', line 21

def channel
  @channel
end

#command_prefixString

Returns The prefix to use when defining and parsing commands e.g. !.

Returns:

  • (String)

    The prefix to use when defining and parsing commands e.g. !



29
30
31
# File 'lib/twitchbot/bot.rb', line 29

def command_prefix
  @command_prefix
end

#debugBoolean

Returns Whether the bot should display debug info to STDOUT.

Returns:

  • (Boolean)

    Whether the bot should display debug info to STDOUT



25
26
27
# File 'lib/twitchbot/bot.rb', line 25

def debug
  @debug
end

#message_queueArray

Returns The array of messages that are to be sent to the server.

Returns:

  • (Array)

    The array of messages that are to be sent to the server



27
28
29
# File 'lib/twitchbot/bot.rb', line 27

def message_queue
  @message_queue
end

#passwordString

Returns Password of the bot account.

Returns:

  • (String)

    Password of the bot account



19
20
21
# File 'lib/twitchbot/bot.rb', line 19

def password
  @password
end

#pluginsArray

Returns The plugins that are in use by the bot.

Returns:

  • (Array)

    The plugins that are in use by the bot



23
24
25
# File 'lib/twitchbot/bot.rb', line 23

def plugins
  @plugins
end

#usernameString

Returns Username of the bot account.

Returns:

  • (String)

    Username of the bot account



17
18
19
# File 'lib/twitchbot/bot.rb', line 17

def username
  @username
end

Instance Method Details

#startObject

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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