twitch_chatter 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/twitch_chatter/bot.rb +16 -16
- data/lib/twitch_chatter/models/channel.rb +12 -12
- data/lib/twitch_chatter/models/message.rb +19 -17
- data/lib/twitch_chatter.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 492969da096c5b1c22a1470cf299261cc6df2df03d48fc8b05ed29bfc0fc4b0c
|
4
|
+
data.tar.gz: 023faac7d335744978a18cd976c4669c8c062a630d4443b8235e49c01ca8566f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a59850e2a9194d7034abd2944bdd07b3219718c9f45c4be8a15ba8535070b22f151726e0805733cc07e1b9a5b5b9422ec344a3eb51f2287b422c50eaf812d9c
|
7
|
+
data.tar.gz: a27584857fd99b20d7e7923022ceacaf1c459b468c36b7719b29878c704ec6a0d88f6f8406c9dee8f81c935c7cb53656642adaa14a7c3e7d82003bb96610bf61
|
data/lib/twitch_chatter/bot.rb
CHANGED
@@ -21,22 +21,25 @@ module Twitch
|
|
21
21
|
websocket_url: "wss://irc-ws.chat.twitch.tv:443",
|
22
22
|
}.freeze
|
23
23
|
|
24
|
+
# @see [DEFAULT_OPTIONS]
|
24
25
|
def initialize(**options)
|
25
26
|
@nick = options[:nick] || DEFAULT_OPTIONS[:nick]
|
26
27
|
@websocket_url = Async::HTTP::Endpoint.parse(options[:websocket_url] || DEFAULT_OPTIONS[:websocket_url])
|
27
28
|
end
|
28
29
|
|
29
|
-
#
|
30
|
-
# @
|
30
|
+
# Dispatched on websocket connect, but before channel joins
|
31
|
+
# @yield [nil]
|
31
32
|
def ready(&block)
|
32
33
|
@ready_handle = block
|
33
34
|
end
|
34
35
|
|
36
|
+
# If we're connected to Twitch's websocket
|
35
37
|
# @return [Boolean]
|
36
38
|
def ready?
|
37
39
|
@ws != nil
|
38
40
|
end
|
39
41
|
|
42
|
+
# Begin pulling messages
|
40
43
|
# @return [Async::Task]
|
41
44
|
def start
|
42
45
|
Async do
|
@@ -54,7 +57,7 @@ module Twitch
|
|
54
57
|
|
55
58
|
next unless data.include?("PRIVMSG")
|
56
59
|
|
57
|
-
dispatch(Message.new(data,
|
60
|
+
dispatch(Message.new(data, bot: self))
|
58
61
|
end
|
59
62
|
end
|
60
63
|
end
|
@@ -64,27 +67,22 @@ module Twitch
|
|
64
67
|
|
65
68
|
# @return [Array<Symbol>]
|
66
69
|
def channels
|
67
|
-
streams.keys
|
70
|
+
streams.keys.map { |s| Channel.new(s, bot: self) }
|
68
71
|
end
|
69
72
|
|
70
|
-
# @
|
71
|
-
# @yieldparam streamer [Symbol]
|
72
|
-
# @yieldreturn [nil]
|
73
|
+
# @yieldparam [Symbol] streamer
|
73
74
|
def joined(&block)
|
74
75
|
@join_handle = block
|
75
76
|
end
|
76
77
|
|
77
|
-
# @
|
78
|
-
# @yieldparam streamer [Symbol]
|
79
|
-
# @yieldreturn [nil]
|
78
|
+
# @yieldparam [Symbol] streamer
|
80
79
|
def left(&block)
|
81
80
|
@leave_handle = block
|
82
81
|
end
|
83
82
|
|
84
83
|
# @param streamer [Symbol, String]
|
85
|
-
# @yield
|
86
84
|
# @yieldparam message [Twitch::Message]
|
87
|
-
# @
|
85
|
+
# @return [nil]
|
88
86
|
# @example
|
89
87
|
# bot.join(:twitchgaming) do |message|
|
90
88
|
# puts "##{message.channel} #{message.sender}: #{message}"
|
@@ -100,9 +98,12 @@ module Twitch
|
|
100
98
|
streams[streamer] << block if block_given?
|
101
99
|
@ws.write("JOIN ##{streamer}")
|
102
100
|
@join_handle&.call(streamer)
|
101
|
+
nil
|
103
102
|
end
|
104
103
|
|
104
|
+
# Disconnects from channel and removes all message callbacks.
|
105
105
|
# @param streamer [Symbol, String]
|
106
|
+
# @return [nil]
|
106
107
|
# @example
|
107
108
|
# bot.leave(:twitchgaming)
|
108
109
|
def leave(streamer)
|
@@ -112,11 +113,10 @@ module Twitch
|
|
112
113
|
streams[streamer] = []
|
113
114
|
@ws.write("PART ##{streamer}")
|
114
115
|
@leave_handle&.call(streamer)
|
116
|
+
nil
|
115
117
|
end
|
116
118
|
|
117
|
-
# @yield
|
118
119
|
# @yieldparam message [Twitch::Message]
|
119
|
-
# @yieldreturn [nil]
|
120
120
|
# @example
|
121
121
|
# bot.message do |message|
|
122
122
|
# puts "##{message.channel} #{message.sender}: #{message}"
|
@@ -126,7 +126,7 @@ module Twitch
|
|
126
126
|
end
|
127
127
|
|
128
128
|
alias_method :part, :leave
|
129
|
-
alias_method :on_leave, :
|
129
|
+
alias_method :on_leave, :left
|
130
130
|
alias_method :on_join, :joined
|
131
131
|
alias_method :on_message, :message
|
132
132
|
|
@@ -140,8 +140,8 @@ module Twitch
|
|
140
140
|
end
|
141
141
|
|
142
142
|
def dispatch_ready
|
143
|
-
join_all
|
144
143
|
@ready_handle&.call
|
144
|
+
join_all
|
145
145
|
end
|
146
146
|
|
147
147
|
def streams
|
@@ -4,27 +4,26 @@ module Twitch
|
|
4
4
|
# @!attribute [r] name
|
5
5
|
# @return [Symbol] Name of the channel
|
6
6
|
# @example
|
7
|
-
# channel = Twitch::Channel.new(
|
7
|
+
# channel = Twitch::Channel.new(:twitchgaming)
|
8
8
|
class Channel
|
9
|
+
# @return [String]
|
9
10
|
attr_reader :name
|
10
11
|
|
11
|
-
# @param
|
12
|
-
# @param
|
13
|
-
def initialize(name,
|
12
|
+
# @param [String, Symbol] name
|
13
|
+
# @param [Bot, nil] bot
|
14
|
+
def initialize(name, bot: nil)
|
14
15
|
@name = name.to_sym
|
15
|
-
@
|
16
|
+
@bot = bot
|
16
17
|
end
|
17
18
|
|
18
|
-
#
|
19
|
-
# @yieldparam message [Twitch::Message]
|
20
|
-
# @return [nil]
|
19
|
+
# (see Bot#join)
|
21
20
|
def join(&block)
|
22
|
-
@
|
21
|
+
@bot.join(@name, &block)
|
23
22
|
end
|
24
23
|
|
25
|
-
#
|
24
|
+
# (see Bot#join)
|
26
25
|
def leave
|
27
|
-
@
|
26
|
+
@bot.leave(@name)
|
28
27
|
end
|
29
28
|
|
30
29
|
# @return [Symbol]
|
@@ -37,6 +36,7 @@ module Twitch
|
|
37
36
|
@name.to_s
|
38
37
|
end
|
39
38
|
|
39
|
+
# @return [String]
|
40
40
|
def to_url
|
41
41
|
"https://twitch.tv/#{@name}"
|
42
42
|
end
|
@@ -44,7 +44,7 @@ module Twitch
|
|
44
44
|
alias_method :link, :to_url
|
45
45
|
alias_method :href, :to_url
|
46
46
|
|
47
|
-
# @param
|
47
|
+
# @param [Channel, String, Symbol] other
|
48
48
|
# @return [Boolean]
|
49
49
|
def ==(other)
|
50
50
|
if other.is_a?(Channel)
|
@@ -1,36 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Twitch
|
4
|
-
# @!attribute [r] sender
|
5
|
-
# @return [Channel] Sender of the message
|
6
|
-
# @!attribute [r] channel
|
7
|
-
# @return [Channel] Channel the message was sent to
|
8
|
-
# @!attribute [r] content
|
9
|
-
# @return [String] Content of the message
|
10
|
-
# @!attribute [r] raw
|
11
|
-
# @return [String] Raw message from Twitch
|
12
4
|
# @example
|
13
5
|
# message = Twitch::Message.new(":justinfan!justinfan@justinfan.tmi.twitch.tv PRIVMSG #twitchgaming :Hello world!\r\n")
|
14
6
|
# message.sender.name # => :justinfan
|
15
7
|
# message.channel.name # => :twitchgaming
|
16
8
|
# message.content # => "Hello world!"
|
17
9
|
# message.raw # => ":justinfan!justinfan@justinfan.tmi.twitch.tv PRIVMSG #justinfan :Hello world!\r\n"
|
18
|
-
class Message
|
19
|
-
|
10
|
+
class Message
|
11
|
+
# @return [Channel] Sender of the message
|
12
|
+
attr_reader :sender
|
13
|
+
# @return [Channel] Channel the message was sent to
|
14
|
+
attr_reader :channel
|
15
|
+
# @return [String] Content of the message
|
16
|
+
attr_reader :content
|
17
|
+
# @return [String] Raw message from Twitch
|
18
|
+
attr_reader :raw
|
20
19
|
|
21
20
|
# @param raw [String] Raw IRC websocket message from Twitch
|
22
|
-
# @param
|
21
|
+
# @param bot [Bot, nil] bot
|
23
22
|
# @example
|
24
23
|
# message = Twitch::Message.new(":justinfan!justinfan@justinfan.tmi.twitch.tv PRIVMSG #twitchgaming :Hello world!\r\n")
|
25
|
-
def initialize(raw,
|
24
|
+
def initialize(raw, bot: nil)
|
26
25
|
split = raw.split(" ")
|
27
26
|
content = split[3..-1].join(" ")[1..-1]
|
28
|
-
super(content)
|
29
27
|
|
30
28
|
@content = content
|
31
|
-
@
|
32
|
-
@channel = Channel.new(split[2][1..-1],
|
33
|
-
@sender = Channel.new(split[0].split("!")[0][1..-1],
|
29
|
+
@bot = bot
|
30
|
+
@channel = Channel.new(split[2][1..-1], bot: bot)
|
31
|
+
@sender = Channel.new(split[0].split("!")[0][1..-1], bot: bot)
|
34
32
|
@raw = raw
|
35
33
|
end
|
36
34
|
|
@@ -38,6 +36,8 @@ module Twitch
|
|
38
36
|
alias_method :streamer, :channel
|
39
37
|
alias_method :user, :sender
|
40
38
|
|
39
|
+
# Username regexp
|
40
|
+
# @api private
|
41
41
|
USERNAME = /[a-zA-Z0-9_]{4,25}/
|
42
42
|
# @return [Array<Twitch::Channel>] List of usernames mentioned in the message
|
43
43
|
def mentions
|
@@ -48,12 +48,14 @@ module Twitch
|
|
48
48
|
match = word.match(/@#{USERNAME}/)
|
49
49
|
next unless match
|
50
50
|
|
51
|
-
@mentions << Channel.new(match[0][1..-1],
|
51
|
+
@mentions << Channel.new(match[0][1..-1], bot: @bot)
|
52
52
|
end
|
53
53
|
|
54
54
|
@mentions
|
55
55
|
end
|
56
56
|
|
57
|
+
# Link regexp
|
58
|
+
# @api private
|
57
59
|
LINK = %r{(https?://[^\s]+)}
|
58
60
|
# @return [Array<String>] List of links mentioned in the message
|
59
61
|
# @example
|
data/lib/twitch_chatter.rb
CHANGED
@@ -3,8 +3,6 @@
|
|
3
3
|
require "async"
|
4
4
|
require "async/http/endpoint"
|
5
5
|
require "async/websocket/client"
|
6
|
-
require_relative "twitch_chatter/models"
|
7
|
-
require_relative "twitch_chatter/bot"
|
8
6
|
|
9
7
|
# @author Dylan Hackworth <me@dylhack.dev>
|
10
8
|
# @example
|
@@ -20,4 +18,6 @@ require_relative "twitch_chatter/bot"
|
|
20
18
|
#
|
21
19
|
# bot.start
|
22
20
|
module Twitch
|
21
|
+
require_relative "twitch_chatter/models"
|
22
|
+
require_relative "twitch_chatter/bot"
|
23
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twitch_chatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dylan Hackworth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: async-websocket
|