Class: Twitch::Bot
- Inherits:
-
Object
- Object
- Twitch::Bot
- Defined in:
- lib/twitch_chatter/bot.rb
Overview
Constant Summary collapse
- DEFAULT_OPTIONS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default options for Twitch websocket. Nicknames starting with “justinfan” are considered anonymous.
{ nick: "justinfan0735", websocket_url: "wss://irc-ws.chat.twitch.tv:443", }.freeze
Instance Method Summary collapse
- #channels ⇒ Array<Symbol>
-
#initialize(**options) ⇒ Bot
constructor
A new instance of Bot.
- #join(streamer) {|message| ... } ⇒ nil
- #joined {|streamer| ... } ⇒ Object (also: #on_join)
-
#leave(streamer) ⇒ nil
(also: #part)
Disconnects from channel and removes all message callbacks.
- #left {|streamer| ... } ⇒ Object (also: #on_leave)
- #message {|message| ... } ⇒ Object (also: #on_message)
-
#ready {|nil| ... } ⇒ Object
Dispatched on websocket connect, but before channel joins.
-
#ready? ⇒ Boolean
If we’re connected to Twitch’s websocket.
-
#start ⇒ Async::Task
(also: #run)
Begin pulling messages.
Constructor Details
#initialize(**options) ⇒ Bot
Returns a new instance of Bot.
25 26 27 28 |
# File 'lib/twitch_chatter/bot.rb', line 25 def initialize(**) @nick = [:nick] || DEFAULT_OPTIONS[:nick] @websocket_url = Async::HTTP::Endpoint.parse([:websocket_url] || DEFAULT_OPTIONS[:websocket_url]) end |
Instance Method Details
#channels ⇒ Array<Symbol>
69 70 71 |
# File 'lib/twitch_chatter/bot.rb', line 69 def channels streams.keys.map { |s| Channel.new(s, bot: self) } end |
#join(streamer) {|message| ... } ⇒ nil
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/twitch_chatter/bot.rb', line 90 def join(streamer, &block) streamer = streamer.to_sym unless ready? yield_join(streamer, &block) return end streams[streamer] = [] unless streams.key?(streamer) streams[streamer] << block if block_given? @ws.write("JOIN ##{streamer}") @join_handle&.call(streamer) nil end |
#joined {|streamer| ... } ⇒ Object Also known as: on_join
74 75 76 |
# File 'lib/twitch_chatter/bot.rb', line 74 def joined(&block) @join_handle = block end |
#leave(streamer) ⇒ nil Also known as: part
Disconnects from channel and removes all message callbacks.
109 110 111 112 113 114 115 116 117 |
# File 'lib/twitch_chatter/bot.rb', line 109 def leave(streamer) return unless ready? streamer = streamer.to_sym streams[streamer] = [] @ws.write("PART ##{streamer}") @leave_handle&.call(streamer) nil end |
#left {|streamer| ... } ⇒ Object Also known as: on_leave
79 80 81 |
# File 'lib/twitch_chatter/bot.rb', line 79 def left(&block) @leave_handle = block end |
#message {|message| ... } ⇒ Object Also known as: on_message
124 125 126 |
# File 'lib/twitch_chatter/bot.rb', line 124 def (&block) @message_handle = block end |
#ready {|nil| ... } ⇒ Object
Dispatched on websocket connect, but before channel joins
32 33 34 |
# File 'lib/twitch_chatter/bot.rb', line 32 def ready(&block) @ready_handle = block end |
#ready? ⇒ Boolean
If we’re connected to Twitch’s websocket
38 39 40 |
# File 'lib/twitch_chatter/bot.rb', line 38 def ready? @ws != nil end |
#start ⇒ Async::Task Also known as: run
Begin pulling messages
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/twitch_chatter/bot.rb', line 44 def start Async do Async::WebSocket::Client.connect(@websocket_url) do |ws| @ws = ws ws.write("NICK #{@nick}") dispatch_ready while ( = ws.read) data = .to_str if data.start_with?("PING") ws.write("PONG :tmi.twitch.tv") next end next unless data.include?("PRIVMSG") dispatch(Message.new(data, bot: self)) end end end end |