Class: Twitch::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/twitch_chatter/models/message.rb

Overview

Examples:

message = Twitch::Message.new(":[email protected] PRIVMSG #twitchgaming :Hello world!\r\n")
message.sender.name  # => :justinfan
message.channel.name # => :twitchgaming
message.content      # => "Hello world!"
message.raw          # => ":[email protected] PRIVMSG #justinfan :Hello world!\r\n"

Constant Summary collapse

USERNAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Username regexp

/[a-zA-Z0-9_]{4,25}/
%r{(https?://[^\s]+)}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, bot: nil) ⇒ Message

Returns a new instance of Message.

Examples:

message = Twitch::Message.new(":[email protected] PRIVMSG #twitchgaming :Hello world!\r\n")

Parameters:

  • raw (String)

    Raw IRC websocket message from Twitch

  • bot (Bot, nil) (defaults to: nil)

    bot



24
25
26
27
28
29
30
31
32
33
# File 'lib/twitch_chatter/models/message.rb', line 24

def initialize(raw, bot: nil)
  split = raw.split(" ")
  content = split[3..-1].join(" ")[1..-1]

  @content = content
  @bot = bot
  @channel = Channel.new(split[2][1..-1], bot: bot)
  @sender = Channel.new(split[0].split("!")[0][1..-1], bot: bot)
  @raw = raw
end

Instance Attribute Details

#channelChannel (readonly) Also known as: streamer

Returns Channel the message was sent to.

Returns:

  • (Channel)

    Channel the message was sent to



14
15
16
# File 'lib/twitch_chatter/models/message.rb', line 14

def channel
  @channel
end

#contentString (readonly) Also known as: text

Returns Content of the message.

Returns:

  • (String)

    Content of the message



16
17
18
# File 'lib/twitch_chatter/models/message.rb', line 16

def content
  @content
end

#rawString (readonly)

Returns Raw message from Twitch.

Returns:

  • (String)

    Raw message from Twitch



18
19
20
# File 'lib/twitch_chatter/models/message.rb', line 18

def raw
  @raw
end

#senderChannel (readonly) Also known as: user

Returns Sender of the message.

Returns:

  • (Channel)

    Sender of the message



12
13
14
# File 'lib/twitch_chatter/models/message.rb', line 12

def sender
  @sender
end

Instance Method Details

Returns List of links mentioned in the message.

Examples:

bot.join(:twitchgaming) do |message|
  if message.links.any?
    puts "Links: #{message.links.join(", ")}"
  end
end

Returns:

  • (Array<String>)

    List of links mentioned in the message



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/twitch_chatter/models/message.rb', line 67

def links
  return @links if @links

  @links = []
  @content.split(" ").each do |word|
    match = word.match(LINK)
    next unless match

    @links << match[0]
  end

  @links
end

#mentionsArray<Twitch::Channel>

Returns List of usernames mentioned in the message.

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/twitch_chatter/models/message.rb', line 43

def mentions
  return @mentions if @mentions

  @mentions = []
  @content.split(" ").each do |word|
    match = word.match(/@#{USERNAME}/)
    next unless match

    @mentions << Channel.new(match[0][1..-1], bot: @bot)
  end

  @mentions
end