Skip to content

Commit

Permalink
ircm now has a @ducky; updated specs to match
Browse files Browse the repository at this point in the history
  • Loading branch information
WhereIsX committed May 5, 2021
1 parent 14a2fe6 commit c720a33
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 62 deletions.
90 changes: 45 additions & 45 deletions spec/irc_message_spec.cr
Expand Up @@ -39,70 +39,70 @@ describe IRCMessage do
raw_irc = "<user>!<user>@<user>.tmi.twitch.tv PRIVMSG #duckers :hmmm..."
ircm = IRCMessage.new(raw_irc)

ircm.user_message?.should be_true
ircm.is_user_msg?.should be_true
end

it "should return false if raw_irc does not contains the word PRIVMSG" do
raw_irc = ":tmi.twitch.tv CLEARCHAT #duckers :test-ducky"
ircm = IRCMessage.new(raw_irc)

ircm.user_message?.should be_false
ircm.is_user_msg?.should be_false
end
end

describe "#twitch_message?" do
it "should return true if raw_irc starts with :tmi.twitch.tv" do
raw_irc = ":tmi.twitch.tv CLEARCHAT #duckers :test-ducky"
ircm = IRCMessage.new(raw_irc)
# describe "#twitch_message?" do
# it "should return true if raw_irc starts with :tmi.twitch.tv" do
# raw_irc = ":tmi.twitch.tv CLEARCHAT #duckers :test-ducky"
# ircm = IRCMessage.new(raw_irc)

ircm.twitch_message?.should be_true
end
# ircm.twitch_message?.should be_true
# end

it "should return false if raw_irc does not starts with :tmi.twitch.tv" do
raw_irc = "<user>!<user>@<user>.tmi.twitch.tv PRIVMSG #duckers :hmmm..."
ircm = IRCMessage.new(raw_irc)
# it "should return false if raw_irc does not starts with :tmi.twitch.tv" do
# raw_irc = "<user>!<user>@<user>.tmi.twitch.tv PRIVMSG #duckers :hmmm..."
# ircm = IRCMessage.new(raw_irc)

ircm.twitch_message?.should be_false
end
end
# ircm.twitch_message?.should be_false
# end
# end

describe "#parse_type" do
it "should return MessageType::Ping if ping?" do
raw_irc = "PING :tmi.twitch.tv"
ircm = IRCMessage.new(raw_irc)
# describe "#parse_type" do
# it "should return MessageType::Ping if ping?" do
# raw_irc = "PING :tmi.twitch.tv"
# ircm = IRCMessage.new(raw_irc)

ircm.parse_type.should eq IRCMessage::MessageType::Ping
end
# ircm.parse_type.should eq IRCMessage::MessageType::Ping
# end

it "should return MessageType::UserMessage if user_message?" do
raw_irc = "<user>!<user>@<user>.tmi.twitch.tv PRIVMSG #duckers :hmmm..."
ircm = IRCMessage.new(raw_irc)
# it "should return MessageType::UserMessage if user_message?" do
# raw_irc = "<user>!<user>@<user>.tmi.twitch.tv PRIVMSG #duckers :hmmm..."
# ircm = IRCMessage.new(raw_irc)

ircm.parse_type.should eq IRCMessage::MessageType::UserMessage
end
# ircm.parse_type.should eq IRCMessage::MessageType::UserMessage
# end

it "should return MessageType::TwitchMessage if twitch_message?" do
raw_irc = ":tmi.twitch.tv CLEARCHAT #duckers :test-ducky"
ircm = IRCMessage.new(raw_irc)
# it "should return MessageType::TwitchMessage if twitch_message?" do
# raw_irc = ":tmi.twitch.tv CLEARCHAT #duckers :test-ducky"
# ircm = IRCMessage.new(raw_irc)

ircm.parse_type.should eq IRCMessage::MessageType::TwitchMessage
end
end
# ircm.parse_type.should eq IRCMessage::MessageType::TwitchMessage
# end
# end

describe "#parse" do
it "should tags, username and message" do
raw_irc = "@login=<login>;target-msg-id=<target-msg-id> :yana!yana@yana.tmi.twitch.tv PRIVMSG #duckers :shiny!"
ircm = IRCMessage.new(raw_irc)
# describe "#parse" do
# it "should tags, username and message" do
# raw_irc = "@login=<login>;target-msg-id=<target-msg-id> :yana!yana@yana.tmi.twitch.tv PRIVMSG #duckers :shiny!"
# ircm = IRCMessage.new(raw_irc)

tags = {"@login" => "<login>", "target-msg-id" => "<target-msg-id>"}
username = "yana"
message = "shiny!"
# tags = {"@login" => "<login>", "target-msg-id" => "<target-msg-id>"}
# username = "yana"
# message = "shiny!"

parsed_data = ircm.parse
# parsed_data = ircm.parse

parsed_data[:tags].should eq tags
parsed_data[:username].should eq username
parsed_data[:message].should eq message
end
end
end
# parsed_data[:tags].should eq tags
# parsed_data[:username].should eq username
# parsed_data[:message].should eq message
# end
# end
end
44 changes: 27 additions & 17 deletions src/chatty/irc_message.cr
@@ -1,7 +1,8 @@
require "./chat_colors.cr"
require "../models/ducky.cr"

class IRCMessage
getter(type, time, username, message, raw_irc, words)
getter(type, time, username, message, raw_irc, words, ducky)

@raw_irc : String
@time : Time
Expand All @@ -10,6 +11,7 @@ class IRCMessage
@username : String
@message : String
@words : Array(String)
@ducky : Model::Ducky | Nil

enum MessageType
TwitchMessage
Expand All @@ -18,12 +20,14 @@ class IRCMessage
end

def initialize(@raw_irc : String)
# default values
@time = Time.local
@type = parse_type()
@username = ""
@message = @raw_irc
@words = Array(String).new
@tags = Hash(String, String).new
@ducky = nil

# then parse it further if is user message
if @type == MessageType::UserMessage
Expand All @@ -32,12 +36,15 @@ class IRCMessage
@username = parsed_ircm[:username]
@message = parsed_ircm[:message]
@words = @message.split(' ', remove_empty: true)
@ducky = Model::Ducky.find_by(username: @username)
# give ducky peas if its a ducky chatting
give_peas_for_chatting()
end
end

# TODO: rename username => sender
# parse_user_message?
def parse : NamedTuple(
private def parse : NamedTuple(
username: String,
message: String,
tags: Hash(String, String),
Expand Down Expand Up @@ -67,13 +74,16 @@ class IRCMessage
return {tags: tags, username: username, message: message}
end

# private def parse_raw_channel_chatter(raw_irc : String) : NamedTuple(
# tags: Hash(String, String),
# username: String,
# message: String)
# end
private def give_peas_for_chatting
ducky = @ducky
return if ducky.nil?
ducky.points += 1
if !ducky.save
puts "⚠️ you wat"
end
end

def parse_type : MessageType | Nil
private def parse_type : MessageType | Nil
if ping?
return MessageType::Ping
elsif user_message?
Expand All @@ -89,21 +99,21 @@ class IRCMessage
return @raw_irc.starts_with?("PING")
end

def is_ping? : Bool
@type == MessageType::Ping
private def user_message? : Bool
# return @raw_irc.starts_with?("@badge-info")
return @raw_irc.includes?("PRIVMSG")
end

def is_user_msg? : Bool
@type == MessageType::UserMessage
private def twitch_message? : Bool
return @raw_irc.starts_with?(":tmi.twitch.tv")
end

def user_message? : Bool
# return @raw_irc.starts_with?("@badge-info")
return @raw_irc.includes?("PRIVMSG")
def is_ping? : Bool
@type == MessageType::Ping
end

def twitch_message? : Bool
return @raw_irc.starts_with?(":tmi.twitch.tv")
def is_user_msg? : Bool
@type == MessageType::UserMessage
end

def print : String
Expand Down

0 comments on commit c720a33

Please sign in to comment.