Class: Discordrb::Message

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/discordrb/data.rb

Overview

A message on Discord that was sent to a text channel

Constant Summary collapse

ZERO_DISCRIM =

The discriminator that webhook user accounts have.

'0000'.freeze

Instance Attribute Summary collapse

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time

Instance Attribute Details

#attachmentsArray<Attachment> (readonly)

Returns the files attached to this message.

Returns:

  • (Array<Attachment>)

    the files attached to this message.



1622
1623
1624
# File 'lib/discordrb/data.rb', line 1622

def attachments
  @attachments
end

#authorMember (readonly) Also known as: user, writer

Returns the user that sent this message.

Returns:

  • (Member)

    the user that sent this message.



1601
1602
1603
# File 'lib/discordrb/data.rb', line 1601

def author
  @author
end

#channelChannel (readonly)

Returns the channel in which this message was sent.

Returns:

  • (Channel)

    the channel in which this message was sent.



1606
1607
1608
# File 'lib/discordrb/data.rb', line 1606

def channel
  @channel
end

#contentString (readonly) Also known as: text, to_s

Returns the content of this message.

Returns:

  • (String)

    the content of this message.



1596
1597
1598
# File 'lib/discordrb/data.rb', line 1596

def content
  @content
end

#editedtrue, false (readonly) Also known as: edited?

Returns whether the message was edited or not.

Returns:

  • (true, false)

    whether the message was edited or not.



1635
1636
1637
# File 'lib/discordrb/data.rb', line 1635

def edited
  @edited
end

#edited_timestampTime (readonly) Also known as: edit_timestamp

Returns the timestamp at which this message was edited. nil if the message was never edited.

Returns:

  • (Time)

    the timestamp at which this message was edited. nil if the message was never edited.



1612
1613
1614
# File 'lib/discordrb/data.rb', line 1612

def edited_timestamp
  @edited_timestamp
end

#embedsArray<Embed> (readonly)

Returns the embed objects contained in this message.

Returns:

  • (Array<Embed>)

    the embed objects contained in this message.



1625
1626
1627
# File 'lib/discordrb/data.rb', line 1625

def embeds
  @embeds
end

#mention_everyonetrue, false (readonly) Also known as: mention_everyone?, mentions_everyone?

Returns whether the message mentioned everyone or not.

Returns:

  • (true, false)

    whether the message mentioned everyone or not.



1639
1640
1641
# File 'lib/discordrb/data.rb', line 1639

def mention_everyone
  @mention_everyone
end

#mentionsArray<User> (readonly)

Returns the users that were mentioned in this message.

Returns:

  • (Array<User>)

    the users that were mentioned in this message.



1616
1617
1618
# File 'lib/discordrb/data.rb', line 1616

def mentions
  @mentions
end

#nonceString (readonly)

Returns used for validating a message was sent.

Returns:

  • (String)

    used for validating a message was sent



1632
1633
1634
# File 'lib/discordrb/data.rb', line 1632

def nonce
  @nonce
end

#pinnedtrue, false (readonly) Also known as: pinned?

Returns whether the message is pinned or not.

Returns:

  • (true, false)

    whether the message is pinned or not.



1644
1645
1646
# File 'lib/discordrb/data.rb', line 1644

def pinned
  @pinned
end

#role_mentionsArray<Role> (readonly)

Returns the roles that were mentioned in this message.

Returns:

  • (Array<Role>)

    the roles that were mentioned in this message.



1619
1620
1621
# File 'lib/discordrb/data.rb', line 1619

def role_mentions
  @role_mentions
end

#timestampTime (readonly)

Returns the timestamp at which this message was sent.

Returns:

  • (Time)

    the timestamp at which this message was sent.



1609
1610
1611
# File 'lib/discordrb/data.rb', line 1609

def timestamp
  @timestamp
end

#ttstrue, false (readonly) Also known as: tts?

Returns whether the message used Text-To-Speech (TTS) or not.

Returns:

  • (true, false)

    whether the message used Text-To-Speech (TTS) or not.



1628
1629
1630
# File 'lib/discordrb/data.rb', line 1628

def tts
  @tts
end

#webhook_idInteger? (readonly)

Returns the webhook ID that sent this message, or nil if it wasn't sent through a webhook.

Returns:

  • (Integer, nil)

    the webhook ID that sent this message, or nil if it wasn't sent through a webhook.



1648
1649
1650
# File 'lib/discordrb/data.rb', line 1648

def webhook_id
  @webhook_id
end

Instance Method Details

#await(key, attributes = {}, &block) ⇒ Object

Add an Await for a message with the same user and channel.

See Also:



1748
1749
1750
# File 'lib/discordrb/data.rb', line 1748

def await(key, attributes = {}, &block)
  @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @author.id, in: @channel.id }.merge(attributes), &block)
end

#deleteObject

Deletes this message.



1727
1728
1729
1730
# File 'lib/discordrb/data.rb', line 1727

def delete
  API::Channel.delete_message(@bot.token, @channel.id, @id)
  nil
end

#edit(new_content) ⇒ Message

Edits this message to have the specified content instead. You can only edit your own messages.

Parameters:

  • new_content (String)

    the new content the message should have.

Returns:

  • (Message)

    the resulting message.



1721
1722
1723
1724
# File 'lib/discordrb/data.rb', line 1721

def edit(new_content)
  response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content)
  Message.new(JSON.parse(response), @bot)
end

#emojiArray<Emoji>

Returns the emotes that were used/mentioned in this message (Only returns Emoji the bot has access to, else nil).

Returns:

  • (Array<Emoji>)

    the emotes that were used/mentioned in this message (Only returns Emoji the bot has access to, else nil).



1771
1772
1773
1774
1775
1776
1777
1778
1779
# File 'lib/discordrb/data.rb', line 1771

def emoji
  return if @content.nil?

  emoji = scan_for_emoji
  emoji.each do |element|
    @emoji << @bot.parse_mention(element)
  end
  @emoji
end

#emoji?true, false

Check if any emoji got used in this message

Returns:

  • (true, false)

    whether or not any emoji got used



1783
1784
1785
1786
# File 'lib/discordrb/data.rb', line 1783

def emoji?
  emoji = scan_for_emoji
  return true unless emoji.empty?
end

#from_bot?true, false

Returns whether this message was sent by the current Bot.

Returns:

  • (true, false)

    whether this message was sent by the current Bot.



1753
1754
1755
# File 'lib/discordrb/data.rb', line 1753

def from_bot?
  @author && @author.current_bot?
end

#inspectObject

The inspect method is overwritten to give more useful output



1789
1790
1791
# File 'lib/discordrb/data.rb', line 1789

def inspect
  "<Message content=\"#{@content}\" id=#{@id} timestamp=#{@timestamp} author=#{@author} channel=#{@channel}>"
end

#pinObject

Pins this message



1733
1734
1735
1736
1737
# File 'lib/discordrb/data.rb', line 1733

def pin
  API::Channel.pin_message(@bot.token, @channel.id, @id)
  @pinned = true
  nil
end

#reply(content) ⇒ Object

Replies to this message with the specified content.



1713
1714
1715
# File 'lib/discordrb/data.rb', line 1713

def reply(content)
  @channel.send_message(content)
end

#unpinObject

Unpins this message



1740
1741
1742
1743
1744
# File 'lib/discordrb/data.rb', line 1740

def unpin
  API::Channel.unpin_message(@bot.token, @channel.id, @id)
  @pinned = false
  nil
end

#webhook?true, false

Returns whether this message has been sent over a webhook.

Returns:

  • (true, false)

    whether this message has been sent over a webhook.



1758
1759
1760
# File 'lib/discordrb/data.rb', line 1758

def webhook?
  !@webhook_id.nil?
end