Class: Discorb::Message

Inherits:
DiscordModel show all
Defined in:
lib/discorb/message.rb

Overview

Represents a message.

Direct Known Subclasses

Webhook::Message

Defined Under Namespace

Classes: Activity, Flag, Interaction, Reference, Sticker

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DiscordModel

#==, #eql?, #hash

Instance Attribute Details

#activity -> Discorb::Message::Activity (readonly)

Returns The activity of the message.

Returns:



117
118
119
# File 'lib/discorb/message.rb', line 117

def activity
  @activity
end

#application_id -> Discorb::Application (readonly)

Returns The application of the message.

Returns:



119
120
121
# File 'lib/discorb/message.rb', line 119

def application_id
  @application_id
end

#attachments -> Array<Discorb::Attachment> (readonly)

Returns The attachments of the message.

Returns:



83
84
85
# File 'lib/discorb/message.rb', line 83

def attachments
  @attachments
end

#author -> Discorb::User, Discorb::Member (readonly)

Returns The user that sent the message.

Returns:



69
70
71
# File 'lib/discorb/message.rb', line 69

def author
  @author
end

#channel -> nil, Discorb::Channel (readonly)

Note:

This method returns an object from client cache. it will return nil if the object is not in cache.

Returns:

  • (nil)

    The object wasn't cached.

  • (Discorb::Channel)

    The channel the message was sent in.



# File 'lib/discorb/message.rb', line 170

#components -> Array<Array<Discorb::Component>> (readonly)

Returns The components of the message.

Returns:



132
133
134
# File 'lib/discorb/message.rb', line 132

def components
  @components
end

#content -> String (readonly) Also known as: to_s

Returns The content of the message.

Returns:

  • (String)

    The content of the message.



71
72
73
# File 'lib/discorb/message.rb', line 71

def content
  @content
end

#created_at -> Time (readonly) Also known as: timestamp, sent_at

Returns The time the message was created.

Returns:

  • (Time)

    The time the message was created.



74
75
76
# File 'lib/discorb/message.rb', line 74

def created_at
  @created_at
end

#deleted -> Boolean (readonly) Also known as: deleted?

Returns Whether the message is deleted.

Returns:

  • (Boolean)

    Whether the message is deleted.



134
135
136
# File 'lib/discorb/message.rb', line 134

def deleted
  @deleted
end

#edited? -> Boolean (readonly)

Returns Whether the message was edited.

Returns:

  • (Boolean)

    Whether the message was edited.



# File 'lib/discorb/message.rb', line 170

#embed -> Discorb::Embed? (readonly)

Returns:

  • (Discorb::Embed)

    The embed of the message.

  • (nil)

    If the message has no embed.



# File 'lib/discorb/message.rb', line 170

#embeds -> Array<Discorb::Embed> (readonly)

Returns The embeds of the message.

Returns:



85
86
87
# File 'lib/discorb/message.rb', line 85

def embeds
  @embeds
end

#guild -> nil, Discorb::Guild (readonly)

Note:

This method returns an object from client cache. it will return nil if the object is not in cache.

Returns:

  • (nil)

    The object wasn't cached.

  • (Discorb::Guild)

    The guild the message was sent in.

  • (nil)

    If the message was not sent in a guild.



# File 'lib/discorb/message.rb', line 170

#id -> Discorb::Snowflake (readonly)

Returns The ID of the message.

Returns:



67
68
69
# File 'lib/discorb/message.rb', line 67

def id
  @id
end

#interaction -> Discorb::Message::Interaction (readonly)

Returns The interaction of the message.

Returns:



128
129
130
# File 'lib/discorb/message.rb', line 128

def interaction
  @interaction
end

#jump_url -> String (readonly)

Returns The URL to jump to the message.

Returns:

  • (String)

    The URL to jump to the message.



# File 'lib/discorb/message.rb', line 170

#mention_everyone -> Boolean (readonly) Also known as: mention_everyone?

Returns Whether the message mentions everyone.

Returns:

  • (Boolean)

    Whether the message mentions everyone.



140
141
142
# File 'lib/discorb/message.rb', line 140

def mention_everyone
  @mention_everyone
end

#message_reference -> Discorb::Message::Reference (readonly)

Returns The reference of the message.

Returns:



121
122
123
# File 'lib/discorb/message.rb', line 121

def message_reference
  @message_reference
end

#pinned -> Boolean (readonly) Also known as: pinned?

Returns Whether the message is pinned.

Returns:

  • (Boolean)

    Whether the message is pinned.



143
144
145
# File 'lib/discorb/message.rb', line 143

def pinned
  @pinned
end

#reactions -> Array<Discorb::Reaction> (readonly)

Returns The reactions of the message.

Returns:



87
88
89
# File 'lib/discorb/message.rb', line 87

def reactions
  @reactions
end

#stickers -> Discorb::Message::Sticker (readonly)

Returns The sticker of the message.

Returns:



126
127
128
# File 'lib/discorb/message.rb', line 126

def stickers
  @stickers
end

#thread -> Discorb::ThreadChannel (readonly)

Returns The thread channel of the message.

Returns:



130
131
132
# File 'lib/discorb/message.rb', line 130

def thread
  @thread
end

#tts -> Boolean (readonly) Also known as: tts?

Returns Whether the message is tts.

Returns:

  • (Boolean)

    Whether the message is tts.



137
138
139
# File 'lib/discorb/message.rb', line 137

def tts
  @tts
end

#type -> Symbol (readonly)

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

Returns:

  • (Symbol)

    The type of the message.



115
116
117
# File 'lib/discorb/message.rb', line 115

def type
  @type
end

#updated_at -> Time? (readonly) Also known as: edited_at, edited_timestamp

Returns:

  • (Time)

    The time the message was edited.

  • (nil)

    If the message was not edited.



79
80
81
# File 'lib/discorb/message.rb', line 79

def updated_at
  @updated_at
end

#webhook? -> Boolean (readonly)

Returns Whether the message was sent by a webhook.

Returns:

  • (Boolean)

    Whether the message was sent by a webhook.



# File 'lib/discorb/message.rb', line 170

#webhook_id -> Discorb::Snowflake (readonly)

Returns The ID of the channel the message was sent in.

Returns:



89
90
91
# File 'lib/discorb/message.rb', line 89

def webhook_id
  @webhook_id
end

Instance Method Details

#add_reaction(emoji) -> Object Also known as: react_with

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Add a reaction to the message.

Parameters:

Raises:



343
344
345
346
347
# File 'lib/discorb/message.rb', line 343

def add_reaction(emoji)
  Async do
    @client.http.put("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", nil).wait
  end
end

#clean_content(user: true, channel: true, role: true, emoji: true, everyone: true, codeblock: false) -> String

Removes the mentions from the message.

Parameters:

  • user (Boolean) (defaults to: true)

    Whether to clean user mentions.

  • channel (Boolean) (defaults to: true)

    Whether to clean channel mentions.

  • role (Boolean) (defaults to: true)

    Whether to clean role mentions.

  • emoji (Boolean) (defaults to: true)

    Whether to clean emoji.

  • everyone (Boolean) (defaults to: true)

    Whether to clean @everyone and @here.

  • codeblock (Boolean) (defaults to: false)

    Whether to clean codeblocks.

Returns:

  • (String)

    The cleaned content of the message.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/discorb/message.rb', line 228

def clean_content(user: true, channel: true, role: true, emoji: true, everyone: true, codeblock: false)
  ret = @content.dup
  ret.gsub!(/<@!?(\d+)>/) do |match|
    member = guild&.members&.[]($1)
    member ||= @client.users[$1]
    member ? "@#{member.name}" : "@Unknown User"
  end if user
  ret.gsub!(/<#(\d+)>/) do |match|
    channel = @client.channels[$1]
    channel ? "<##{channel.id}>" : "#Unknown Channel"
  end
  ret.gsub!(/<@&(\d+)>/) do |match|
    role = guild&.roles&.[]($1)
    role ? "@#{role.name}" : "@Unknown Role"
  end if role
  ret.gsub!(/<a?:([a-zA-Z0-9_]+):\d+>/) do |match|
    $1
  end if emoji
  ret.gsub!(/@(everyone|here)/, "@\u200b\\1") if everyone
  unless codeblock
    codeblocks = ret.split("```", -1)
    original_codeblocks = @content.scan(/```(.+?)```/m)
    res = []
    max = codeblocks.length
    codeblocks.each_with_index do |codeblock, i|
      if max % 2 == 0 && i == max - 1 or i.even?
        res << codeblock
      else
        res << original_codeblocks[i / 2]
      end
    end
    res.join("```")
  else
    ret
  end
end

#delete!(reason: nil) -> Object

Delete the message.

Parameters:

  • reason (String) (defaults to: nil)

    The reason for deleting the message.



288
289
290
291
292
# File 'lib/discorb/message.rb', line 288

def delete!(reason: nil)
  Async do
    channel.delete_message!(@id, reason: reason).wait
  end
end

#edit(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) -> Object

Edit the message.

Parameters:

  • content (String) (defaults to: nil)

    The message content.

  • embed (Discorb::Embed) (defaults to: nil)

    The embed to send.

  • embeds (Array<Discorb::Embed>) (defaults to: nil)

    The embeds to send.

  • allowed_mentions (Discorb::AllowedMentions) (defaults to: nil)

    The allowed mentions.

  • components (Array<Discorb::Component>, Array<Array<Discorb::Component>>) (defaults to: nil)

    The components to send.

  • supress (Boolean) (defaults to: nil)

    Whether to supress embeds.



275
276
277
278
279
280
281
# File 'lib/discorb/message.rb', line 275

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

#fetch_reacted_users(emoji, limit: nil, after: 0) -> Async::Task<Array<Discorb::User>>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Fetch reacted users of reaction.

Parameters:

  • emoji (Discorb::Emoji)

    The emoji to fetch.

  • limit (Integer, nil) (defaults to: nil)

    The maximum number of users to fetch. nil for no limit.

  • after (Discorb::Snowflake, nil) (defaults to: 0)

    The ID of the user to start fetching from.

Returns:

Raises:



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/discorb/message.rb', line 393

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

#flag -> Discorb::Message::Flag

Returns The flag of the message.

Returns:

See Also:



124
125
126
# File 'lib/discorb/message.rb', line 124

def flag
  @flag
end

#inspect -> Object

Meta



457
458
459
# File 'lib/discorb/message.rb', line 457

def inspect
  "#<#{self.class} #{@content.inspect} id=#{@id}>"
end

#pin(reason: nil) -> Object

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Pin the message.

Parameters:

  • reason (String) (defaults to: nil)

    The reason for pinning the message.

Raises:



423
424
425
426
427
# File 'lib/discorb/message.rb', line 423

def pin(reason: nil)
  Async do
    channel.pin_message(self, reason: reason).wait
  end
end

#publish -> Object

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Publish the message.

Raises:



330
331
332
333
334
# File 'lib/discorb/message.rb', line 330

def publish
  Async do
    channel.post("/channels/#{@channel_id}/messages/#{@id}/crosspost", nil).wait
  end
end

#remove_reaction(emoji) -> Object Also known as: delete_reaction

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Remove a reaction from the message.

Parameters:

Raises:



358
359
360
361
362
# File 'lib/discorb/message.rb', line 358

def remove_reaction(emoji)
  Async do
    @client.http.delete("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me").wait
  end
end

#remove_reaction_of(emoji, member) -> Object Also known as: delete_reaction_of

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Remove other member's reaction from the message.

Parameters:

Raises:



374
375
376
377
378
# File 'lib/discorb/message.rb', line 374

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

#reply(*args, **kwargs) -> Async::Task<Discorb::Message>

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Reply to the message.

Returns:

Raises:



319
320
321
322
323
# File 'lib/discorb/message.rb', line 319

def reply(*args, **kwargs)
  Async do
    channel.post(*args, reference: self, **kwargs).wait
  end
end

#start_thread(*args, **kwargs) -> Async::Task<<Type>> Also known as: create_thread

Start thread from the message.

Returns:

  • (Async::Task<<Type>>)

    <description>



447
448
449
450
451
# File 'lib/discorb/message.rb', line 447

def start_thread(*args, **kwargs)
  Async do
    channel.start_thread(*args, message: self, **kwargs).wait
  end
end

#to_reference(fail_if_not_exists: true) -> Hash

Convert the message to reference object.

Parameters:

  • fail_if_not_exists (Boolean) (defaults to: true)

    Whether to raise an error if the message does not exist.

Returns:

  • (Hash)

    The reference object.



301
302
303
304
305
306
307
308
# File 'lib/discorb/message.rb', line 301

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 -> Object

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Unpin the message.

Raises:



434
435
436
437
438
# File 'lib/discorb/message.rb', line 434

def unpin
  Async do
    channel.unpin_message(self, reason: reason).wait
  end
end