class Discorb::Message

Represents a message.

Attributes

message_type[R]

@!visibility private

activity[R]

@return [Discorb::Message::Activity] The activity of the message.

application_id[R]

@return [Discorb::Application] The application of the message.

attachments[R]

@return [Array<Discorb::Attachment>] The attachments of the message.

author[R]

@return [Discorb::User, Discorb::Member] The user that sent the message.

components[R]

@return [Array<Array<Discorb::Components>>] The components of the message.

content[R]

@return [String] The content of the message.

created_at[R]

@return [Time] The time the message was created.

deleted[R]

@return [Boolean] Whether the message is deleted.

deleted?[R]

@return [Boolean] Whether the message is deleted.

edited_at[R]

@return [Time] The time the message was edited. @return [nil] If the message was not edited.

edited_timestamp[R]

@return [Time] The time the message was edited. @return [nil] If the message was not edited.

embeds[R]

@return [Array<Discorb::Embed>] The embeds of the message.

flag[R]

@return [Discorb::Message::Flag] The flag of the message. @see Discorb::Message::Flag

id[R]

@return [Discorb::Snowflake] The ID of the message.

interaction[R]

@return [Discorb::Message::Interaction] The interaction of the message.

mention_everyone[R]

@return [Boolean] Whether the message mentions everyone.

mention_everyone?[R]

@return [Boolean] Whether the message mentions everyone.

message_reference[R]

@return [Discorb::Message::Reference] The reference of the message.

pinned[R]

@return [Boolean] Whether the message is pinned.

pinned?[R]

@return [Boolean] Whether the message is pinned.

reactions[R]

@return [Array<Discorb::Reaction>] The reactions of the message.

sent_at[R]

@return [Time] The time the message was created.

stickers[R]

@return [Discorb::Message::Sticker] The sticker of the message.

thread[R]

@return [Discorb::ThreadChannel] The thread channel of the message.

timestamp[R]

@return [Time] The time the message was created.

to_s[R]

@return [String] The content of the message.

tts[R]

@return [Boolean] Whether the message is tts.

tts?[R]

@return [Boolean] Whether the message is tts.

type[R]

@return [Symbol] The type of the message. Currently, this will be one of:

  • `:default`

  • `:recipient_add`

  • `:recipient_remove`

  • `:call`

  • `:channel_name_change`

  • `:channel_icon_change`

  • `:channel_pinned_message`

  • `:guild_member_join`

  • `:user_premium_guild_subscription`

  • `:user_premium_guild_subscription_tier_1`

  • `:user_premium_guild_subscription_tier_2`

  • `:user_premium_guild_subscription_tier_3`

  • `:channel_follow_add`

  • `:guild_discovery_disqualified`

  • `:guild_discovery_requalified`

  • `:guild_discovery_grace_period_initial_warning`

  • `:guild_discovery_grace_period_final_warning`

  • `:thread_created`

  • `:reply`

  • `:application_command`

  • `:thread_starter_message`

  • `:guild_invite_reminder`

updated_at[R]

@return [Time] The time the message was edited. @return [nil] If the message was not edited.

webhook_id[R]

@return [Discorb::Snowflake] The ID of the channel the message was sent in.

Public Class Methods

new(client, data, no_cache: false) click to toggle source

@!visibility private

# File lib/discorb/message.rb, line 188
def initialize(client, data, no_cache: false)
  @client = client
  @data = {}
  @no_cache = no_cache
  _set_data(data)
  @client.messages[@id] = self unless @no_cache
end

Public Instance Methods

add_reaction(emoji) click to toggle source

Add a reaction to the message. @macro async @macro http

@param [Discorb::Emoji] emoji The emoji to react with.

# File lib/discorb/message.rb, line 294
def add_reaction(emoji)
  Async do
    @client.http.put("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", nil).wait
  end
end
Also aliased as: react_with
channel() click to toggle source
# File lib/discorb/message.rb, line 196
def channel
  @dm || @client.channels[@channel_id]
end
create_thread(*args, **kwargs)
Alias for: start_thread
delete!(reason: nil) click to toggle source

Delete the message.

@param [String] reason The reason for deleting the message.

# File lib/discorb/message.rb, line 239
def delete!(reason: nil)
  Async do
    channel.delete_message!(@id, reason: reason).wait
  end
end
delete_reaction(emoji)
Alias for: remove_reaction
delete_reaction_of(emoji, member)
Alias for: remove_reaction_of
edit(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) click to toggle source

Edit the message.

@param [String] content The message content. @param [Discorb::Embed] embed The embed to send. @param [Array<Discord::Embed>] embeds The embeds to send. @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions. @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send. @param [Boolean] supress Whether to supress embeds.

# File lib/discorb/message.rb, line 226
def edit(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil,
                                    components: nil, supress: nil)
  Async do
    channel.edit_message(@id, message_id, content, embed: embed, embeds: embeds, allowed_mentions: allowed_mentions,
                                                   components: components, supress: supress).wait
  end
end
edited?() click to toggle source
# File lib/discorb/message.rb, line 212
def edited?
  !@updated_at.nil?
end
embed() click to toggle source
# File lib/discorb/message.rb, line 261
def embed
  @embeds[0]
end
fetch_reacted_users(emoji, limit: nil, after: 0) click to toggle source

Fetch reacted users of reaction. @macro async @macro http

@param [Discorb::Emoji] emoji The emoji to fetch. @param [Integer, nil] limit The maximum number of users to fetch. `nil` for no limit. @param [Discorb::Snowflake, nil] after The ID of the user to start fetching from.

@return [Array<Discorb::User>] The users.

# File lib/discorb/message.rb, line 344
def fetch_reacted_users(emoji, limit: nil, after: 0)
  Async do
    if limit.nil? || !limit.positive?
      after = 0
      users = []
      loop do
        _resp, data = @client.http.get("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=100&after=#{after}").wait
        break if data.empty?

        users += data.map { |r| guild&.members&.[](r[:id]) || @client.users[r[:id]] || User.new(@client, r) }

        break if data.length < 100

        after = data[-1][:id]
      end
      next users
    else
      _resp, data = @client.http.get("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=#{limit}&after=#{after}").wait
      next data.map { |r| guild&.members&.[](r[:id]) || @client.users[r[:id]] || User.new(@client, r) }
    end
  end
end
guild() click to toggle source
# File lib/discorb/message.rb, line 200
def guild
  @client.guilds[@guild_id]
end
inspect() click to toggle source

Meta

# File lib/discorb/message.rb, line 408
def inspect
  "#<#{self.class} #{@content.inspect} id=#{@id}>"
end
jump_url() click to toggle source
# File lib/discorb/message.rb, line 208
def jump_url
  "https://discord.com/channels/#{@guild_id || "@me"}/#{@channel_id}/#{@id}"
end
pin(reason: nil) click to toggle source

Pin the message. @macro async @macro http

@param [String] reason The reason for pinning the message.

# File lib/discorb/message.rb, line 374
def pin(reason: nil)
  Async do
    channel.pin_message(self, reason: reason).wait
  end
end
publish() click to toggle source

Publish the message. @macro async @macro http

# File lib/discorb/message.rb, line 281
def publish
  Async do
    channel.post("/channels/#{@channel_id}/messages/#{@id}/crosspost", nil).wait
  end
end
react_with(emoji)
Alias for: add_reaction
remove_reaction(emoji) click to toggle source

Remove a reaction from the message. @macro async @macro http

@param [Discorb::Emoji] emoji The emoji to remove.

# File lib/discorb/message.rb, line 309
def remove_reaction(emoji)
  Async do
    @client.http.delete("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me").wait
  end
end
Also aliased as: delete_reaction
remove_reaction_of(emoji, member) click to toggle source

Remove other member's reaction from the message. @macro async @macro http

@param [Discorb::Emoji] emoji The emoji to remove. @param [Discorb::Member] member The member to remove the reaction from.

# File lib/discorb/message.rb, line 325
def remove_reaction_of(emoji, member)
  Async do
    @client.http.delete("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/#{member.is_a?(Member) ? member.id : member}").wait
  end
end
Also aliased as: delete_reaction_of
reply(*args, **kwargs) click to toggle source

Reply to the message. @macro async @macro http @param (see post) @return [Discorb::Message] The message.

# File lib/discorb/message.rb, line 270
def reply(*args, **kwargs)
  Async do
    channel.post(*args, reference: self, **kwargs).wait
  end
end
start_thread(*args, **kwargs) click to toggle source

Start thread from the message.

@param (see Discorb::Channel#start_thread)

@return [<Type>] <description>

# File lib/discorb/message.rb, line 398
def start_thread(*args, **kwargs)
  Async do
    channel.start_thread(*args, message: self, **kwargs).wait
  end
end
Also aliased as: create_thread
to_reference(fail_if_not_exists: true) click to toggle source

Convert the message to reference object.

@param [Boolean] fail_if_not_exists Whether to raise an error if the message does not exist.

@return [Hash] The reference object.

# File lib/discorb/message.rb, line 252
def to_reference(fail_if_not_exists: true)
  {
    message_id: @id,
    channel_id: @channel_id,
    guild_id: @guild_id,
    fail_if_not_exists: fail_if_not_exists,
  }
end
unpin() click to toggle source

Unpin the message. @macro async @macro http

# File lib/discorb/message.rb, line 385
def unpin
  Async do
    channel.unpin_message(self, reason: reason).wait
  end
end
webhook?() click to toggle source
# File lib/discorb/message.rb, line 204
def webhook?
  @webhook_id != nil
end

Private Instance Methods

_set_data(data) click to toggle source
# File lib/discorb/message.rb, line 510
def _set_data(data)
  @id = Snowflake.new(data[:id])
  @channel_id = data[:channel_id]

  if data[:guild_id]
    @guild_id = data[:guild_id]
    @dm = nil
  else
    @dm = Discorb::DMChannel.new(@client, data[:channel_id])
    @guild_id = nil
  end

  if data[:member].nil? && data[:webhook_id]
    @webhook_id = Snowflake.new(data[:webhook_id])
    @author = Webhook::Message::Author.new(data[:author])
  elsif data[:guild_id].nil? || data[:guild_id].empty?
    @author = @client.users[data[:author][:id]] || User.new(@client, data[:author])
  else
    @author = guild.members[data[:author][:id]] || Member.new(@client,
                                                              @guild_id, data[:author], data[:member])
  end
  @content = data[:content]
  @created_at = Time.iso8601(data[:timestamp])
  @updated_at = data[:edited_timestamp].nil? ? nil : Time.iso8601(data[:edited_timestamp])

  @tts = data[:tts]
  @mention_everyone = data[:mention_everyone]
  @mention_roles = data[:mention_roles].map { |r| guild.roles[r] }
  @attachments = data[:attachments].map { |a| Attachment.new(a) }
  @embeds = data[:embeds] ? data[:embeds].map { |e| Embed.new(data: e) } : []
  @reactions = data[:reactions] ? data[:reactions].map { |r| Reaction.new(self, r) } : []
  @pinned = data[:pinned]
  @type = self.class.message_type[data[:type]]
  @activity = data[:activity] && Activity.new(data[:activity])
  @application_id = data[:application_id]
  @message_reference = data[:message_reference] && Reference.from_hash(data[:message_reference])
  @flag = Flag.new(0b111 - data[:flags])
  @sticker_items = data[:sticker_items] ? data[:sticker_items].map { |s| Message::Sticker.new(s) } : []
  # @referenced_message = data[:referenced_message] && Message.new(@client, data[:referenced_message])
  @interaction = data[:interaction] && Message::Interaction.new(@client, data[:interaction])
  @thread = data[:thread] && Channel.make_channel(@client, data[:thread])
  @components = data[:components].map { |c| c[:components].map { |co| Component.from_hash(co) } }
  @data.update(data)
  @deleted = false
end