Class: Twitch::Bot

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

Overview

Examples:

bot = Twitch::Bot.new

bot.ready do
  puts "Ready!"
end

bot.join(:twitchgaming) do |message|
  puts "#{message.channel} #{message.user}: #{message.text}"
end

bot.start

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

Constructor Details

#initialize(**options) ⇒ Bot

Returns a new instance of Bot.

See Also:

  • [DEFAULT_OPTIONS]


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

def initialize(**options)
  @nick = options[:nick] || DEFAULT_OPTIONS[:nick]
  @websocket_url = Async::HTTP::Endpoint.parse(options[:websocket_url] || DEFAULT_OPTIONS[:websocket_url])
end

Instance Method Details

#channelsArray<Symbol>

Returns:

  • (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

Examples:

bot.join(:twitchgaming) do |message|
  puts "##{message.channel} #{message.sender}: #{message}"
end

Parameters:

  • streamer (Symbol, String)

Yield Parameters:

Returns:

  • (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

Yield Parameters:

  • streamer (Symbol)


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.

Examples:

bot.leave(:twitchgaming)

Parameters:

  • streamer (Symbol, String)

Returns:

  • (nil)


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

Yield Parameters:

  • streamer (Symbol)


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

Examples:

bot.message do |message|
  puts "##{message.channel} #{message.sender}: #{message}"
end

Yield Parameters:



124
125
126
# File 'lib/twitch_chatter/bot.rb', line 124

def message(&block)
  @message_handle = block
end

#ready {|nil| ... } ⇒ Object

Dispatched on websocket connect, but before channel joins

Yields:

  • (nil)


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

Returns:

  • (Boolean)


38
39
40
# File 'lib/twitch_chatter/bot.rb', line 38

def ready?
  @ws != nil
end

#startAsync::Task Also known as: run

Begin pulling messages

Returns:

  • (Async::Task)


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_message = ws.read)
        data = ws_message.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