class Twitchbot::Message

Class responsible for parsing messages created in [EventHandler]

TODO: Clean this up because its ugly TODO: Parse message-id

Attributes

bits[R]

@return [Integer] Number of bits that have been included in a message

channel[R]

@return [Channel] Channel that the message was sent to

command[R]

@return [String] IRC command of the raw IRC message

payload[R]

@return [String] Content of the IRC message

raw[R]

@return [String] Raw IRC message received

sender[R]

@return [String] Sender of the IRC message

target[R]

@return [String] Target of the IRC message

user[R]

@return [User] User that the message was sent by

Public Class Methods

new(handler, raw_message) click to toggle source
# 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

message?() click to toggle source

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
ping?() click to toggle source

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
respond(message) click to toggle source

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
send_channel(message) click to toggle source

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
send_whisper(user, message) click to toggle source

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
tagged?() click to toggle source

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
whisper?() click to toggle source

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