Class: Twitchbot::Bot
- Inherits:
-
Object
- Object
- Twitchbot::Bot
- 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
-
#channel ⇒ Channel
The channel that the bot is connected to.
-
#command_prefix ⇒ String
The prefix to use when defining and parsing commands e.g.
-
#debug ⇒ Boolean
Whether the bot should display debug info to STDOUT.
-
#message_queue ⇒ Array
The array of messages that are to be sent to the server.
-
#password ⇒ String
Password of the bot account.
-
#plugins ⇒ Array
The plugins that are in use by the bot.
-
#username ⇒ String
Username of the bot account.
Instance Method Summary collapse
-
#initialize {|_self| ... } ⇒ Bot
constructor
Create a new Bot instance, passing a block to set the necessary attributes to have the bot function.
-
#start ⇒ Object
Start the event loop, initiate the websocket client, and register the plugins with eventmachine.
Constructor Details
#initialize {|_self| ... } ⇒ Bot
Create a new Bot instance, passing a block to set the necessary attributes to have the bot function
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
#channel ⇒ Channel
Returns The channel that the bot is connected to.
21 22 23 |
# File 'lib/twitchbot/bot.rb', line 21 def channel @channel end |
#command_prefix ⇒ String
Returns 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 |
#debug ⇒ Boolean
Returns Whether the bot should display debug info to STDOUT.
25 26 27 |
# File 'lib/twitchbot/bot.rb', line 25 def debug @debug end |
#message_queue ⇒ Array
Returns 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 end |
#password ⇒ String
Returns Password of the bot account.
19 20 21 |
# File 'lib/twitchbot/bot.rb', line 19 def password @password end |
#plugins ⇒ Array
Returns The plugins that are in use by the bot.
23 24 25 |
# File 'lib/twitchbot/bot.rb', line 23 def plugins @plugins end |
#username ⇒ String
Returns Username of the bot account.
17 18 19 |
# File 'lib/twitchbot/bot.rb', line 17 def username @username end |
Instance Method Details
#start ⇒ Object
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 |