summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shard.yml2
-rw-r--r--src/twitch/irc.cr28
2 files changed, 23 insertions, 7 deletions
diff --git a/shard.yml b/shard.yml
index 27d4133..c41f539 100644
--- a/shard.yml
+++ b/shard.yml
@@ -6,7 +6,7 @@ authors:
- snapcase <snapcase@octalzeroes.com>
- Zac Nowicki <zachnowicki@gmail.com>
-crystal: 0.21.1
+crystal: 0.28.0
license: MIT
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