From 549a2cd77231b2317a7bbc1db5e7fda1bf2866fb Mon Sep 17 00:00:00 2001 From: Daniel Worrall Date: Sat, 18 May 2019 22:10:35 +0100 Subject: Support messaging and tags --- shard.yml | 2 +- src/twitch/irc.cr | 28 ++++++++++++++++++++++------ 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 - Zac Nowicki -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 -- cgit v1.2.3