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, synthesise

Instance Attribute Details

#attachmentsArray<Attachment> (readonly)

Returns the files attached to this message.

Returns:

  • (Array<Attachment>)

    the files attached to this message.



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

def attachments
  @attachments
end

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

Returns the user that sent this message. (Will be a Discordrb::Member most of the time, it should only be a User for old messages when the author has left the server since then).

Returns:

  • (Member, User)

    the user that sent this message. (Will be a Discordrb::Member most of the time, it should only be a User for old messages when the author has left the server since then)



1701
1702
1703
# File 'lib/discordrb/data.rb', line 1701

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.



1706
1707
1708
# File 'lib/discordrb/data.rb', line 1706

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.



1695
1696
1697
# File 'lib/discordrb/data.rb', line 1695

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.



1738
1739
1740
# File 'lib/discordrb/data.rb', line 1738

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.



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

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.



1725
1726
1727
# File 'lib/discordrb/data.rb', line 1725

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.



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

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.



1716
1717
1718
# File 'lib/discordrb/data.rb', line 1716

def mentions
  @mentions
end

#nonceString (readonly)

Returns used for validating a message was sent.

Returns:

  • (String)

    used for validating a message was sent



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

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.



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

def pinned
  @pinned
end

#reactionsHash<String, Reaction> (readonly)

Returns the reaction objects attached to this message keyed by the name of the reaction.

Returns:

  • (Hash<String, Reaction>)

    the reaction objects attached to this message keyed by the name of the reaction



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

def reactions
  @reactions
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.



1719
1720
1721
# File 'lib/discordrb/data.rb', line 1719

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.



1709
1710
1711
# File 'lib/discordrb/data.rb', line 1709

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.



1731
1732
1733
# File 'lib/discordrb/data.rb', line 1731

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.



1751
1752
1753
# File 'lib/discordrb/data.rb', line 1751

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:



1869
1870
1871
# File 'lib/discordrb/data.rb', line 1869

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

#create_reaction(reaction) ⇒ Object Also known as: react

Reacts to a message

Parameters:

  • the (String, #to_reaction)

    unicode emoji, Emoji, or GlobalEmoji



1923
1924
1925
1926
1927
# File 'lib/discordrb/data.rb', line 1923

def create_reaction(reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  API::Channel.create_reaction(@bot.token, @channel.id, @id, reaction)
  nil
end

#deleteObject

Deletes this message.



1848
1849
1850
1851
# File 'lib/discordrb/data.rb', line 1848

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

#delete_all_reactionsObject

Removes all reactions from this message



1956
1957
1958
# File 'lib/discordrb/data.rb', line 1956

def delete_all_reactions
  API::Channel.delete_all_reactions(@bot.token, @channel.id, @id)
end

#delete_own_reaction(reaction) ⇒ Object

Delete's this clients reaction on this message

Parameters:

  • the (String, #to_reaction)

    reaction to remove



1950
1951
1952
1953
# File 'lib/discordrb/data.rb', line 1950

def delete_own_reaction(reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  API::Channel.delete_own_reaction(@bot.token, @channel.id, @id, reaction)
end

#delete_reaction(user, reaction) ⇒ Object

Deletes a reaction made by a user on this message

Parameters:

  • the (User, #resolve_id)

    user who used this reaction

  • the (String, #to_reaction)

    reaction to remove



1943
1944
1945
1946
# File 'lib/discordrb/data.rb', line 1943

def delete_reaction(user, reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  API::Channel.delete_user_reaction(@bot.token, @channel.id, @id, reaction, user.resolve_id)
end

#edit(new_content, new_embed = nil) ⇒ 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.

  • new_embed (Hash, Discordrb::Webhooks::Embed, nil) (defaults to: nil)

    The new embed the message should have. If nil the message will be changed to have no embed.

Returns:

  • (Message)

    the resulting message.



1842
1843
1844
1845
# File 'lib/discordrb/data.rb', line 1842

def edit(new_content, new_embed = nil)
  response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content, [], new_embed ? new_embed.to_hash : nil)
  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).



1892
1893
1894
1895
1896
1897
1898
1899
1900
# File 'lib/discordrb/data.rb', line 1892

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



1904
1905
1906
1907
# File 'lib/discordrb/data.rb', line 1904

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.



1874
1875
1876
# File 'lib/discordrb/data.rb', line 1874

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

#inspectObject

The inspect method is overwritten to give more useful output



1961
1962
1963
# File 'lib/discordrb/data.rb', line 1961

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

#my_reactionsArray<Reaction>

Returns the reactions made by the current bot or user

Returns:



1917
1918
1919
# File 'lib/discordrb/data.rb', line 1917

def my_reactions
  @reactions.select(&:me)
end

#pinObject

Pins this message



1854
1855
1856
1857
1858
# File 'lib/discordrb/data.rb', line 1854

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

#reacted_with(reaction) ⇒ Array<User>

Returns the list of users who reacted with a certain reaction

Parameters:

  • the (String, #to_reaction)

    unicode emoji, Emoji, or GlobalEmoji

Returns:

  • (Array<User>)

    the users who used this reaction



1934
1935
1936
1937
1938
# File 'lib/discordrb/data.rb', line 1934

def reacted_with(reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  response = JSON.parse(API::Channel.get_reactions(@bot.token, @channel.id, @id, reaction))
  response.map { |d| User.new(d, @bot) }
end

#reactions?true, false

Check if any reactions got used in this message

Returns:

  • (true, false)

    whether or not this message has reactions



1911
1912
1913
# File 'lib/discordrb/data.rb', line 1911

def reactions?
  @reactions.any?
end

#reply(content) ⇒ Object

Replies to this message with the specified content.



1833
1834
1835
# File 'lib/discordrb/data.rb', line 1833

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

#unpinObject

Unpins this message



1861
1862
1863
1864
1865
# File 'lib/discordrb/data.rb', line 1861

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.



1879
1880
1881
# File 'lib/discordrb/data.rb', line 1879

def webhook?
  !@webhook_id.nil?
end