Class: Discorb::Message

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

Overview

Represents a message in Discord.

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?

Instance Attribute Details

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

Returns The activity of the message.

Returns:



60
61
62
# File 'lib/discorb/message.rb', line 60

def activity
  @activity
end

#application_id -> Discorb::Application (readonly)

Returns The application of the message.

Returns:



62
63
64
# File 'lib/discorb/message.rb', line 62

def application_id
  @application_id
end

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

Returns The attachments of the message.

Returns:



25
26
27
# File 'lib/discorb/message.rb', line 25

def attachments
  @attachments
end

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

Returns The user that sent the message.

Returns:



11
12
13
# File 'lib/discorb/message.rb', line 11

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.



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

def embed?
  @embeds.any?
end

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

Returns The components of the message.

Returns:



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

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.



13
14
15
# File 'lib/discorb/message.rb', line 13

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.



16
17
18
# File 'lib/discorb/message.rb', line 16

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.



77
78
79
# File 'lib/discorb/message.rb', line 77

def deleted
  @deleted
end

#dm? -> Boolean (readonly)

Returns Whether the message was sent in a DM.

Returns:

  • (Boolean)

    Whether the message was sent in a DM.



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

def embed?
  @embeds.any?
end

#edited? -> Boolean (readonly)

Returns Whether the message was edited.

Returns:

  • (Boolean)

    Whether the message was edited.



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

def embed?
  @embeds.any?
end

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

Returns:

  • (Discorb::Embed)

    The embed of the message.

  • (nil)

    If the message has no embed.



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

def embed?
  @embeds.any?
end

#embed? -> Boolean (readonly)

Returns:

  • (Boolean)


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

def embed?
  @embeds.any?
end

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

Returns The embeds of the message.

Returns:



27
28
29
# File 'lib/discorb/message.rb', line 27

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.



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

def embed?
  @embeds.any?
end

#guild? -> Boolean (readonly)

Returns Whether the message was sent in a guild.

Returns:

  • (Boolean)

    Whether the message was sent in a guild.



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

def embed?
  @embeds.any?
end

#id -> Discorb::Snowflake (readonly)

Returns The ID of the message.

Returns:



9
10
11
# File 'lib/discorb/message.rb', line 9

def id
  @id
end

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

Returns The interaction of the message.

Returns:



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

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.



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

def embed?
  @embeds.any?
end

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

Returns Whether the message mentions everyone.

Returns:

  • (Boolean)

    Whether the message mentions everyone.



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

def mention_everyone
  @mention_everyone
end

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

Returns The reference of the message.

Returns:



64
65
66
# File 'lib/discorb/message.rb', line 64

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.



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

def pinned
  @pinned
end

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

Returns The reactions of the message.

Returns:



29
30
31
# File 'lib/discorb/message.rb', line 29

def reactions
  @reactions
end

#reply? -> Boolean (readonly)

Returns Whether the message is a reply.

Returns:

  • (Boolean)

    Whether the message is a reply.



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

def embed?
  @embeds.any?
end

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

Returns The sticker of the message.

Returns:



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

def stickers
  @stickers
end

#thread -> Discorb::ThreadChannel (readonly)

Returns The thread channel of the message.

Returns:



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

def thread
  @thread
end

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

Returns Whether the message is tts.

Returns:

  • (Boolean)

    Whether the message is tts.



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

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

  • :chat_input_command

  • :thread_starter_message

  • :guild_invite_reminder

  • :context_menu_command

Returns:

  • (Symbol)

    The type of the message.



58
59
60
# File 'lib/discorb/message.rb', line 58

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.



21
22
23
# File 'lib/discorb/message.rb', line 21

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.



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

def embed?
  @embeds.any?
end

#webhook_id -> Discorb::Snowflake (readonly)

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

Returns:



31
32
33
# File 'lib/discorb/message.rb', line 31

def webhook_id
  @webhook_id
end

Instance Method Details

#add_reaction(emoji) -> Async::Task<void> Also known as: react_with

Add a reaction to the message.

Parameters:

Returns:

  • (Async::Task<void>)

    The task.



332
333
334
335
336
# File 'lib/discorb/message.rb', line 332

def add_reaction(emoji)
  Async do
    @client.http.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", "//channels/:channel_id/messages/:message_id/reactions/:emoji/@me", :put), 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.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/discorb/message.rb', line 203

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

#delete!(reason: nil) -> Async::Task<void>

Delete the message.

Parameters:

  • reason (String) (defaults to: nil)

    The reason for deleting the message.

Returns:

  • (Async::Task<void>)

    The task.



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

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

#edit(content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, allowed_mentions: Discorb::Unset, attachments: Discorb::Unset, components: Discorb::Unset, supress: Discorb::Unset) -> Async::Task<void>

Edit the message.

Parameters:

  • content (String) (defaults to: Discorb::Unset)

    The message content.

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

    The embed to send.

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

    The embeds to send.

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

    The allowed mentions.

  • attachments (Array<Discorb::Attachment>) (defaults to: Discorb::Unset)

    The new attachments.

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

    The components to send.

  • supress (Boolean) (defaults to: Discorb::Unset)

    Whether to supress embeds.

Returns:

  • (Async::Task<void>)

    The task.



260
261
262
263
264
265
266
# File 'lib/discorb/message.rb', line 260

def edit(content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, allowed_mentions: Discorb::Unset,
                                   attachments: Discorb::Unset, components: Discorb::Unset, supress: Discorb::Unset)
  Async do
    channel.edit_message(@id, content, embed: embed, embeds: embeds, allowed_mentions: allowed_mentions,
                                       attachments: attachments, components: components, supress: supress).wait
  end
end

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

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:



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/discorb/message.rb', line 383

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.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=100&after=#{after}", "//channels/:channel_id/messages/:message_id/reactions/:emoji", :get)).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.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=#{limit}&after=#{after}", "//channels/:channel_id/messages/:message_id/reactions/:emoji", :get)).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:



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

def flag
  @flag
end

#inspect -> Object

Meta



452
453
454
# File 'lib/discorb/message.rb', line 452

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

#pin(reason: nil) -> Async::Task<void>

Pin the message.

Parameters:

  • reason (String) (defaults to: nil)

    The reason for pinning the message.

Returns:

  • (Async::Task<void>)

    The task.



414
415
416
417
418
# File 'lib/discorb/message.rb', line 414

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

#publish -> Async::Task<void>

Publish the message.

Returns:

  • (Async::Task<void>)

    The task.



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

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

#remove_reaction(emoji) -> Async::Task<void> Also known as: delete_reaction

Remove a reaction from the message.

Parameters:

Returns:

  • (Async::Task<void>)

    The task.



348
349
350
351
352
# File 'lib/discorb/message.rb', line 348

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

#remove_reaction_of(emoji, member) -> Async::Task<void> Also known as: delete_reaction_of

Remove other member's reaction from the message.

Parameters:

Returns:

  • (Async::Task<void>)

    The task.



365
366
367
368
369
# File 'lib/discorb/message.rb', line 365

def remove_reaction_of(emoji, member)
  Async do
    @client.http.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/#{member.is_a?(Member) ? member.id : member}", "//channels/:channel_id/messages/:message_id/reactions/:emoji/:user_id", :delete)).wait
  end
end

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

Reply to the message.

Returns:



306
307
308
309
310
# File 'lib/discorb/message.rb', line 306

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

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

Start thread from the message.

Returns:



442
443
444
445
446
# File 'lib/discorb/message.rb', line 442

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.



289
290
291
292
293
294
295
296
# File 'lib/discorb/message.rb', line 289

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(reason: nil) -> Async::Task<void>

Unpin the message.

Parameters:

  • reason (String) (defaults to: nil)

    The reason for unpinning the message.

Returns:

  • (Async::Task<void>)

    The task.



428
429
430
431
432
# File 'lib/discorb/message.rb', line 428

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