diff options
author | Daniel Worrall <Daniel.Worrall@hotmail.co.uk> | 2019-05-18 22:10:35 +0100 |
---|---|---|
committer | Daniel Worrall <Daniel.Worrall@hotmail.co.uk> | 2019-05-18 22:10:35 +0100 |
commit | 549a2cd77231b2317a7bbc1db5e7fda1bf2866fb (patch) | |
tree | aa057206436b48801b36242c2e5c50d1742034fd /src | |
parent | 79d6d80f6a346b8b4297f1a67a6b39c6a965daf1 (diff) | |
download | twitch-549a2cd77231b2317a7bbc1db5e7fda1bf2866fb.tar.gz twitch-549a2cd77231b2317a7bbc1db5e7fda1bf2866fb.zip |
Support messaging and tags
Diffstat (limited to 'src')
-rw-r--r-- | src/twitch/irc.cr | 28 |
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 |