class Twitchbot::Message
Class responsible for parsing messages created in [EventHandler]
TODO: Clean this up because its ugly TODO: Parse message-id
Attributes
@return [Integer] Number of bits that have been included in a message
@return [Channel] Channel
that the message was sent to
@return [String] IRC command of the raw IRC message
@return [String] Content of the IRC message
@return [String] Raw IRC message received
@return [String] Sender of the IRC message
@return [String] Target of the IRC message
@return [User] User
that the message was sent by
Public Class Methods
# File lib/twitchbot/message.rb, line 26 def initialize(handler, raw_message) @handler = handler @raw = raw_message msg = @raw.dup @tags = msg.slice! /^\S+/ if tagged? msg.lstrip! /^(?<sender>:\S+) (?<command>\S+)( (?<target>\S+))?( (?<payload>.+))?$/ =~ msg @sender = sender @command = command @target = target @payload = payload if message? || whisper? @payload.slice! 0, 1 @channel = @handler.bot.channel @display_name = @tags[/display-name=(\w+)/, 1] @user_id = @tags[/user-id=(?<user_id>\d+)/, 1] /:(?<user>\w+)/ =~ @sender if @channel.users.key? user @channel.users[user].update_attributes @display_name, @user_id else @channel.users[user] = User.new user, @display_name, @user_id end @user = @channel.users[user] end if message? /bits=(?<bits>\d+)/ =~ @tags @bits = bits.nil? ? 0 : bits.to_i /badges=(?<badges>[a-zA-Z\/,0-9\-]+)/ =~ @tags @user.update_badges badges || '' end # Grab broadcaster status even though twitch doesn't inject it in the tags # in a whisper if whisper? if @user.name.downcase.eql? @handler.bot.channel.name.downcase @user.update_badges 'broadcaster/1' end end end
Public Instance Methods
Method to determine if the IRC message is an actual message to the [Channel] by a [User]
# File lib/twitchbot/message.rb, line 75 def message? @command.eql? 'PRIVMSG'.freeze end
Method to determine if the IRC message is a PING challenge
# File lib/twitchbot/message.rb, line 105 def ping? @raw.start_with? 'PING' end
Method to respond to the IRC message target with a private message
# File lib/twitchbot/message.rb, line 85 def respond(message) if message? send_channel message end if whisper? send_whisper @user, message end end
Method to send a message to the joined [Channel]
# File lib/twitchbot/message.rb, line 95 def send_channel(message) @handler.send_channel message end
Method to send a whisper to the specified [User]
# File lib/twitchbot/message.rb, line 100 def send_whisper(user, message) @handler.send_whisper user, message end
Method to determine if the IRC message includes any tags from the :twitch.tv/tags
capability
# File lib/twitchbot/message.rb, line 70 def tagged? @raw.start_with? '@' end
Method to determine if the IRC message is a whisper to the bot
# File lib/twitchbot/message.rb, line 80 def whisper? @command.eql? 'WHISPER'.freeze end