class Discordrb::Message

A message on Discord that was sent to a text channel

Constants

ZERO_DISCRIM

The discriminator that webhook user accounts have.

Attributes

attachments[R]

@return [Array<Attachment>] the files attached to this message.

author[R]

@return [Member, User] the user that sent this message. (Will be a {Member} most of the time, it should only be a

{User} for old messages when the author has left the server since then)
channel[R]

@return [Channel] the channel in which this message was sent.

content[R]

@return [String] the content of this message.

edit_timestamp[R]

@return [Time] the timestamp at which this message was edited. `nil` if the message was never edited.

edited[R]

@return [true, false] whether the message was edited or not.

edited?[R]

@return [true, false] whether the message was edited or not.

edited_timestamp[R]

@return [Time] the timestamp at which this message was edited. `nil` if the message was never edited.

embeds[R]

@return [Array<Embed>] the embed objects contained in this message.

mention_everyone[R]

@return [true, false] whether the message mentioned everyone or not.

mention_everyone?[R]

@return [true, false] whether the message mentioned everyone or not.

mentions[R]

@return [Array<User>] the users that were mentioned in this message.

mentions_everyone?[R]

@return [true, false] whether the message mentioned everyone or not.

nonce[R]

@return [String] used for validating a message was sent.

pinned[R]

@return [true, false] whether the message is pinned or not.

pinned?[R]

@return [true, false] whether the message is pinned or not.

reactions[R]

@return [Hash<String => Reaction>] the reaction objects attached to this message keyed by the name of the reaction

role_mentions[R]

@return [Array<Role>] the roles that were mentioned in this message.

text[R]

@return [String] the content of this message.

timestamp[R]

@return [Time] the timestamp at which this message was sent.

to_s[R]

@return [String] the content of this message.

tts[R]

@return [true, false] whether the message used Text-To-Speech (TTS) or not.

tts?[R]

@return [true, false] whether the message used Text-To-Speech (TTS) or not.

user[R]

@return [Member, User] the user that sent this message. (Will be a {Member} most of the time, it should only be a

{User} for old messages when the author has left the server since then)
webhook_id[R]

@return [Integer, nil] the webhook ID that sent this message, or `nil` if it wasn't sent through a webhook.

writer[R]

@return [Member, User] the user that sent this message. (Will be a {Member} most of the time, it should only be a

{User} for old messages when the author has left the server since then)

Public Class Methods

new(data, bot) click to toggle source

@!visibility private

# File lib/discordrb/data.rb, line 2461
def initialize(data, bot)
  @bot = bot
  @content = data['content']
  @channel = bot.channel(data['channel_id'].to_i)
  @pinned = data['pinned']
  @tts = data['tts']
  @nonce = data['nonce']
  @mention_everyone = data['mention_everyone']

  @author = if data['author']
              if data['author']['discriminator'] == ZERO_DISCRIM
                # This is a webhook user! It would be pointless to try to resolve a member here, so we just create
                # a User and return that instead.
                Discordrb::LOGGER.debug("Webhook user: #{data['author']['id']}")
                User.new(data['author'], @bot)
              elsif @channel.private?
                # Turn the message user into a recipient - we can't use the channel recipient
                # directly because the bot may also send messages to the channel
                Recipient.new(bot.user(data['author']['id'].to_i), @channel, bot)
              else
                member = @channel.server.member(data['author']['id'].to_i)

                unless member
                  Discordrb::LOGGER.debug("Member with ID #{data['author']['id']} not cached (possibly left the server).")
                  member = @bot.ensure_user(data['author'])
                end

                member
              end
            end

  @webhook_id = data['webhook_id'].to_i if data['webhook_id']

  @timestamp = Time.parse(data['timestamp']) if data['timestamp']
  @edited_timestamp = data['edited_timestamp'].nil? ? nil : Time.parse(data['edited_timestamp'])
  @edited = !@edited_timestamp.nil?
  @id = data['id'].to_i

  @emoji = []

  @reactions = {}

  data['reactions']&.each do |element|
    @reactions[element['emoji']['name']] = Reaction.new(element)
  end

  @mentions = []

  data['mentions']&.each do |element|
    @mentions << bot.ensure_user(element)
  end

  @role_mentions = []

  # Role mentions can only happen on public servers so make sure we only parse them there
  if @channel.text?
    data['mention_roles']&.each do |element|
      @role_mentions << @channel.server.role(element.to_i)
    end
  end

  @attachments = []
  @attachments = data['attachments'].map { |e| Attachment.new(e, self, @bot) } if data['attachments']

  @embeds = []
  @embeds = data['embeds'].map { |e| Embed.new(e, self) } if data['embeds']
end

Public Instance Methods

await(key, attributes = {}, &block) click to toggle source

Add an {Await} for a message with the same user and channel. @see Bot#add_await @deprecated Will be changed to blocking behavior in v4.0. Use {#await!} instead.

# File lib/discordrb/data.rb, line 2568
def await(key, attributes = {}, &block)
  @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @author.id, in: @channel.id }.merge(attributes), &block)
end
await!(attributes = {}) click to toggle source

Add a blocking {Await} for a message with the same user and channel. @see Bot#add_await!

# File lib/discordrb/data.rb, line 2574
def await!(attributes = {})
  @bot.add_await!(Discordrb::Events::MessageEvent, { from: @author.id, in: @channel.id }.merge(attributes))
end
create_reaction(reaction) click to toggle source

Reacts to a message. @param reaction [String, to_reaction] the unicode emoji or {Emoji}

# File lib/discordrb/data.rb, line 2628
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
Also aliased as: react
delete() click to toggle source

Deletes this message.

# File lib/discordrb/data.rb, line 2546
def delete
  API::Channel.delete_message(@bot.token, @channel.id, @id)
  nil
end
delete_all_reactions() click to toggle source

Removes all reactions from this message.

# File lib/discordrb/data.rb, line 2663
def delete_all_reactions
  API::Channel.delete_all_reactions(@bot.token, @channel.id, @id)
end
delete_own_reaction(reaction) click to toggle source

Deletes this client's reaction on this message. @param reaction [String, to_reaction] the reaction to remove

# File lib/discordrb/data.rb, line 2657
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) click to toggle source

Deletes a reaction made by a user on this message. @param user [User, resolve_id] the user who used this reaction @param reaction [String, to_reaction] the reaction to remove

# File lib/discordrb/data.rb, line 2650
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) click to toggle source

Edits this message to have the specified content instead. You can only edit your own messages. @param new_content [String] the new content the message should have. @param new_embed [Hash, Discordrb::Webhooks::Embed, nil] The new embed the message should have. If `nil` the message will be changed to have no embed. @return [Message] the resulting message.

# File lib/discordrb/data.rb, line 2540
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
emoji() click to toggle source

@return [Array<Emoji>] the emotes that were used/mentioned in this message.

# File lib/discordrb/data.rb, line 2597
def emoji
  return if @content.nil?

  emoji = scan_for_emoji
  emoji.each do |element|
    @emoji << @bot.parse_mention(element)
  end
  @emoji
end
emoji?() click to toggle source

Check if any emoji were used in this message. @return [true, false] whether or not any emoji were used

# File lib/discordrb/data.rb, line 2609
def emoji?
  emoji = scan_for_emoji
  return true unless emoji.empty?
end
from_bot?() click to toggle source

@return [true, false] whether this message was sent by the current {Bot}.

# File lib/discordrb/data.rb, line 2579
def from_bot?
  @author&.current_bot?
end
inspect() click to toggle source

The inspect method is overwritten to give more useful output

# File lib/discordrb/data.rb, line 2668
def inspect
  "<Message content=\"#{@content}\" id=#{@id} timestamp=#{@timestamp} author=#{@author} channel=#{@channel}>"
end
my_reactions() click to toggle source

Returns the reactions made by the current bot or user. @return [Array<Reaction>] the reactions

# File lib/discordrb/data.rb, line 2622
def my_reactions
  @reactions.values.select(&:me)
end
pin() click to toggle source

Pins this message

# File lib/discordrb/data.rb, line 2552
def pin
  API::Channel.pin_message(@bot.token, @channel.id, @id)
  @pinned = true
  nil
end
react(reaction)
Alias for: create_reaction
reacted_with(reaction) click to toggle source

Returns the list of users who reacted with a certain reaction. @param reaction [String, to_reaction] the unicode emoji or {Emoji} @example Get all the users that reacted with a thumbsup.

thumbs_up_reactions = message.reacted_with("\u{1F44D}")

@return [Array<User>] the users who used this reaction

# File lib/discordrb/data.rb, line 2641
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?() click to toggle source

Check if any reactions were used in this message. @return [true, false] whether or not this message has reactions

# File lib/discordrb/data.rb, line 2616
def reactions?
  @reactions.any?
end
reply(content) click to toggle source

Replies to this message with the specified content. @see Channel#send_message

# File lib/discordrb/data.rb, line 2531
def reply(content)
  @channel.send_message(content)
end
scan_for_emoji() click to toggle source

@!visibility private @return [Array<String>] the emoji mentions found in the message

# File lib/discordrb/data.rb, line 2590
def scan_for_emoji
  emoji = @content.split
  emoji = emoji.grep(/<(?<animated>a)?:(?<name>\w+):(?<id>\d+)>?/)
  emoji
end
unpin() click to toggle source

Unpins this message

# File lib/discordrb/data.rb, line 2559
def unpin
  API::Channel.unpin_message(@bot.token, @channel.id, @id)
  @pinned = false
  nil
end
webhook?() click to toggle source

@return [true, false] whether this message has been sent over a webhook.

# File lib/discordrb/data.rb, line 2584
def webhook?
  !@webhook_id.nil?
end