summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/twitch/irc.cr28
1 files changed, 22 insertions, 6 deletions
diff --git a/src/twitch/irc.cr b/src/twitch/irc.cr
index f5fccfc..0aa42ea 100644
--- a/src/twitch/irc.cr
+++ b/src/twitch/irc.cr
@@ -5,17 +5,19 @@ require "./rate_limiter"
class Twitch::IRC
private getter socket : IO
private getter limiter : RateLimiter(String)
+ private getter tags : Array(String)
- def self.new(nick, token)
+ def self.new(nick : String, token : String)
new(TCPSocket.new("irc.chat.twitch.tv", 6667), nick, token)
end
def initialize(@socket : IO, @nick : String, @token : String)
@limiter = RateLimiter(String).new
+ @tags = [] of String
limiter.bucket(:join, 50_u32, 15.seconds)
end
- def run(channels)
+ def run(channels : Array(String))
authenticate
spawn join_channels(channels)
@connected = true
@@ -24,18 +26,21 @@ class Twitch::IRC
end
end
+ def message(channel : String, message : String)
+ raw_write("PRIVMSG", [channel, message])
+ end
+
def on_message(&@on_message : FastIRC::Message ->)
end
- def join_channel(channel)
+ def join_channel(channel : String)
return unless @connected
wait = limiter.rate_limited?(:join, "")
sleep(wait) if wait.is_a?(Time::Span)
raw_write("JOIN", ["##{channel}"])
end
- def dispatch(message)
- puts message.inspect
+ def dispatch(message : FastIRC::Message)
case message.command
when "PING"
pong
@@ -44,12 +49,23 @@ class Twitch::IRC
end
end
+ def tags=(tags : Array(String))
+ @tags = tags
+ end
+
private def authenticate
+ request_tags
raw_write("PASS", [@token])
raw_write("NICK", [@nick])
end
- private def join_channels(channels)
+ private def request_tags
+ @tags.each do |tag|
+ raw_write("CAP REQ", [":twitch.tv/#{tag}"])
+ end
+ end
+
+ private def join_channels(channels : Array(String))
channels.each do |channel|
join_channel(channel)
end