Class: Discordrb::Message

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/discordrb/data/message.rb

Overview

A message on Discord that was sent to a text channel

Constant Summary collapse

FLAGS =

Map of message flags.

{
  crossposted: 1 << 0,
  crosspost: 1 << 1,
  suppress_embeds: 1 << 2,
  source_message_deleted: 1 << 3,
  urgent: 1 << 4,
  thread: 1 << 5,
  ephemeral: 1 << 6,
  loading: 1 << 7,
  failed_to_mention_roles: 1 << 8,
  suppress_notifications: 1 << 12,
  voice_message: 1 << 13,
  snapshot: 1 << 14,
  uikit_components: 1 << 15
}.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.



47
48
49
# File 'lib/discordrb/data/message.rb', line 47

def attachments
  @attachments
end

#channelChannel (readonly)

Returns the channel in which this message was sent.

Returns:

  • (Channel)

    the channel in which this message was sent.



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

def channel
  @channel
end

#componentsArray<Component> (readonly)

Returns Interaction components for this message.

Returns:

  • (Array<Component>)

    Interaction components for this message.



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

def components
  @components
end

#contentString (readonly) Also known as: text, to_s

Returns the content of this message.

Returns:

  • (String)

    the content of this message.



26
27
28
# File 'lib/discordrb/data/message.rb', line 26

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.



63
64
65
# File 'lib/discordrb/data/message.rb', line 63

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.



37
38
39
# File 'lib/discordrb/data/message.rb', line 37

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.



50
51
52
# File 'lib/discordrb/data/message.rb', line 50

def embeds
  @embeds
end

#flagsInteger (readonly)

Returns flags set on the message.

Returns:

  • (Integer)

    flags set on the message.



88
89
90
# File 'lib/discordrb/data/message.rb', line 88

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



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

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.



41
42
43
# File 'lib/discordrb/data/message.rb', line 41

def mentions
  @mentions
end

#nonceString (readonly)

Returns used for validating a message was sent.

Returns:

  • (String)

    used for validating a message was sent.



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

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.



72
73
74
# File 'lib/discordrb/data/message.rb', line 72

def pinned
  @pinned
end

#pinned_atTime? (readonly)

Returns the time at when this message was pinned. Only present on messages fetched via Channel#pins.

Returns:

  • (Time, nil)

    the time at when this message was pinned. Only present on messages fetched via Channel#pins.



94
95
96
# File 'lib/discordrb/data/message.rb', line 94

def pinned_at
  @pinned_at
end

#reactionsArray<Reaction> (readonly)

Returns the reaction objects contained in this message.

Returns:

  • (Array<Reaction>)

    the reaction objects contained in this message.



53
54
55
# File 'lib/discordrb/data/message.rb', line 53

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.



44
45
46
# File 'lib/discordrb/data/message.rb', line 44

def role_mentions
  @role_mentions
end

#serverServer? (readonly)

Returns the server in which this message was sent.

Returns:

  • (Server, nil)

    the server in which this message was sent.



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

def server
  @server
end

#threadChannel? (readonly)

Returns The thread that was started from this message, or nil.

Returns:

  • (Channel, nil)

    The thread that was started from this message, or nil.



91
92
93
# File 'lib/discordrb/data/message.rb', line 91

def thread
  @thread
end

#timestampTime (readonly)

Returns the timestamp at which this message was sent.

Returns:

  • (Time)

    the timestamp at which this message was sent.



34
35
36
# File 'lib/discordrb/data/message.rb', line 34

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.



56
57
58
# File 'lib/discordrb/data/message.rb', line 56

def tts
  @tts
end

#typeInteger (readonly)

Returns what the type of the message is.

Returns:

  • (Integer)

    what the type of the message is



76
77
78
# File 'lib/discordrb/data/message.rb', line 76

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



82
83
84
# File 'lib/discordrb/data/message.rb', line 82

def webhook_id
  @webhook_id
end

Instance Method Details

#all_reaction_users(limit: 100) ⇒ Hash<String => Array<User>>

Returns a hash of all reactions to a message as keys and the users that reacted to it as values.

Examples:

Get all the users that reacted to a message for a giveaway.

giveaway_participants = message.all_reaction_users

Parameters:

  • limit (Integer) (defaults to: 100)

    the limit of how many users to retrieve per distinct reaction emoji. nil will return all users

Returns:

  • (Hash<String => Array<User>>)

    A hash mapping the string representation of a reaction to an array of users.



378
379
380
381
# File 'lib/discordrb/data/message.rb', line 378

def all_reaction_users(limit: 100)
  all_reactions = @reactions.map { |r| { r.to_s => reacted_with(r, limit: limit) } }
  all_reactions.reduce({}, :merge)
end

#authorMember, User 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)



176
177
178
179
180
181
182
183
184
185
# File 'lib/discordrb/data/message.rb', line 176

def author
  return @author if @author

  if @channel.server
    @author = @channel.server.member(@author_id)
    Discordrb::LOGGER.debug("Member with ID #{@author_id} not cached (possibly left the server).") if @author.nil?
  end

  @author ||= @bot.user(@author_id)
end

#await(key, attributes = {}, &block) ⇒ Object

Deprecated.

Will be changed to blocking behavior in v4.0. Use #await! instead.

Add an Await for a message with the same user and channel.

See Also:



266
267
268
# File 'lib/discordrb/data/message.rb', line 266

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

#await!(attributes = {}, &block) ⇒ Object

Add a blocking Await for a message with the same user and channel.

See Also:



272
273
274
# File 'lib/discordrb/data/message.rb', line 272

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

#await_reaction(key, attributes = {}, &block) ⇒ Object

Deprecated.

Will be changed to blocking behavior in v4.0. Use #await_reaction! instead.

Add an Await for a reaction to be added on this message.

See Also:



279
280
281
# File 'lib/discordrb/data/message.rb', line 279

def await_reaction(key, attributes = {}, &block)
  @bot.add_await(key, Discordrb::Events::ReactionAddEvent, { message: @id }.merge(attributes), &block)
end

#await_reaction!(attributes = {}, &block) ⇒ Object

Add a blocking Await for a reaction to be added on this message.

See Also:



285
286
287
# File 'lib/discordrb/data/message.rb', line 285

def await_reaction!(attributes = {}, &block)
  @bot.add_await!(Discordrb::Events::ReactionAddEvent, { message: @id }.merge(attributes), &block)
end

#buttonsArray<Components::Button>

Returns:



437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/discordrb/data/message.rb', line 437

def buttons
  results = @components.collect do |component|
    case component
    when Components::Button
      component
    when Components::ActionRow
      component.buttons
    end
  end

  results.flatten.compact
end

#chat_input_command?true, false

Whether or not this message was of type "CHAT_INPUT_COMMAND"

Returns:

  • (true, false)


423
424
425
# File 'lib/discordrb/data/message.rb', line 423

def chat_input_command?
  @type == 20
end

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

Reacts to a message.

Parameters:

  • reaction (String, #to_reaction)

    the unicode emoji or Emoji



335
336
337
338
339
# File 'lib/discordrb/data/message.rb', line 335

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

#crosspostObject

Crossposts a message in a news channel.



258
259
260
261
# File 'lib/discordrb/data/message.rb', line 258

def crosspost
  response = API::Channel.crosspost_message(@bot.token, @channel.id, @id)
  Message.new(JSON.parse(response), @bot)
end

#delete(reason = nil) ⇒ Object

Deletes this message.



238
239
240
241
# File 'lib/discordrb/data/message.rb', line 238

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

#delete_all_reactionsObject

Removes all reactions from this message.



399
400
401
# File 'lib/discordrb/data/message.rb', line 399

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

#delete_own_reaction(reaction) ⇒ Object

Deletes this client's reaction on this message.

Parameters:

  • reaction (String, #to_reaction)

    the reaction to remove



393
394
395
396
# File 'lib/discordrb/data/message.rb', line 393

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:

  • user (User, String, Integer)

    the user or user ID who used this reaction

  • reaction (String, #to_reaction)

    the reaction to remove



386
387
388
389
# File 'lib/discordrb/data/message.rb', line 386

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_embeds = nil, new_components = nil, flags = 0) ⇒ 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_embeds (Hash, Discordrb::Webhooks::Embed, Array<Hash>, Array<Discordrb::Webhooks::Embed>, nil) (defaults to: nil)

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

  • new_components (View, Array<Hash>) (defaults to: nil)

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

  • flags (Integer) (defaults to: 0)

    Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) can be edited.

Returns:

  • (Message)

    the resulting message.



229
230
231
232
233
234
235
# File 'lib/discordrb/data/message.rb', line 229

def edit(new_content, new_embeds = nil, new_components = nil, flags = 0)
  new_embeds = (new_embeds.instance_of?(Array) ? new_embeds.map(&:to_hash) : [new_embeds&.to_hash]).compact
  new_components = new_components.to_a

  response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content, [], new_embeds, new_components, flags)
  Message.new(JSON.parse(response), @bot)
end

#emojiArray<Emoji>

Returns the emotes that were used/mentioned in this message.

Returns:

  • (Array<Emoji>)

    the emotes that were used/mentioned in this message.



300
301
302
303
304
305
# File 'lib/discordrb/data/message.rb', line 300

def emoji
  return if @content.nil?
  return @emoji unless @emoji.empty?

  @emoji = @bot.parse_mentions(@content).select { |el| el.is_a? Discordrb::Emoji }
end

#emoji?true, false

Check if any emoji were used in this message.

Returns:

  • (true, false)

    whether or not any emoji were used



309
310
311
# File 'lib/discordrb/data/message.rb', line 309

def emoji?
  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.



290
291
292
# File 'lib/discordrb/data/message.rb', line 290

def from_bot?
  @author&.current_bot?
end

#inspectObject

The inspect method is overwritten to give more useful output



404
405
406
# File 'lib/discordrb/data/message.rb', line 404

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

Returns a URL that a user can use to navigate to this message in the client.

Returns:

  • (String)

    a URL that a user can use to navigate to this message in the client



409
410
411
# File 'lib/discordrb/data/message.rb', line 409

def link
  "https://discord.com/channels/#{@server&.id || '@me'}/#{@channel.id}/#{@id}"
end

#my_reactionsArray<Reaction>

Returns the reactions made by the current bot or user.

Returns:



321
322
323
# File 'lib/discordrb/data/message.rb', line 321

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

#pin(reason = nil) ⇒ Object

Pins this message



244
245
246
247
248
# File 'lib/discordrb/data/message.rb', line 244

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

#reacted_with(reaction, limit: 100) ⇒ Array<User>

Returns the list of users who reacted with a certain reaction.

Examples:

Get all the users that reacted with a thumbs up.

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

Parameters:

  • reaction (String, #to_reaction)

    the unicode emoji or Emoji

  • limit (Integer) (defaults to: 100)

    the limit of how many users to retrieve. nil will return all users

Returns:

  • (Array<User>)

    the users who used this reaction



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/discordrb/data/message.rb', line 349

def reacted_with(reaction, limit: 100)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  reaction = reaction.to_s if reaction.respond_to?(:to_s)

  get_reactions = proc do |fetch_limit, after_id = nil|
    resp = API::Channel.get_reactions(@bot.token, @channel.id, @id, reaction, nil, after_id, fetch_limit)
    JSON.parse(resp).map { |d| User.new(d, @bot) }
  end

  # Can be done without pagination
  return get_reactions.call(limit) if limit && limit <= 100

  paginator = Paginator.new(limit, :down) do |last_page|
    if last_page && last_page.count < 100
      []
    else
      get_reactions.call(100, last_page&.last&.id)
    end
  end

  paginator.to_a
end

#reactions?true, false

Check if any reactions were used in this message.

Returns:

  • (true, false)

    whether or not this message has reactions



315
316
317
# File 'lib/discordrb/data/message.rb', line 315

def reactions?
  !@reactions.empty?
end

#referenced_messageMessage?

Returns the Message this Message was sent in reply to.

Returns:

  • (Message, nil)

    the Message this Message was sent in reply to.



428
429
430
431
432
433
434
# File 'lib/discordrb/data/message.rb', line 428

def referenced_message
  return @referenced_message if @referenced_message
  return nil unless @message_reference

  referenced_channel = @bot.channel(@message_reference['channel_id'])
  @referenced_message = referenced_channel.message(@message_reference['message_id'])
end

#reply(content) ⇒ Message

Deprecated.

Please use #respond.

Replies to this message with the specified content.

Parameters:

  • content (String)

    The content to send. Should not be longer than 2000 characters or it will result in an error.

Returns:

  • (Message)

    the message that was sent.

See Also:



195
196
197
# File 'lib/discordrb/data/message.rb', line 195

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

#reply!(content, tts: false, embed: nil, attachments: nil, allowed_mentions: {}, mention_user: false, components: nil, flags: 0) ⇒ Message

Responds to this message as an inline reply.

Parameters:

  • content (String)

    The content to send. Should not be longer than 2000 characters or it will result in an error.

  • tts (true, false) (defaults to: false)

    Whether or not this message should be sent using Discord text-to-speech.

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

    The rich embed to append to this message.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png

  • allowed_mentions (Hash, Discordrb::AllowedMentions, false, nil) (defaults to: {})

    Mentions that are allowed to ping on this message. false disables all pings

  • mention_user (true, false) (defaults to: false)

    Whether the user that is being replied to should be pinged by the reply.

  • components (View, Array<Hash>) (defaults to: nil)

    Interaction components to associate with this message.

  • flags (Integer) (defaults to: 0)

    Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) and SUPPRESS_NOTIFICATIONS (1 << 12) can be set.

Returns:

  • (Message)

    the message that was sent.



209
210
211
212
213
214
215
# File 'lib/discordrb/data/message.rb', line 209

def reply!(content, tts: false, embed: nil, attachments: nil, allowed_mentions: {}, mention_user: false, components: nil, flags: 0)
  allowed_mentions = { parse: [] } if allowed_mentions == false
  allowed_mentions = allowed_mentions.to_hash.transform_keys(&:to_sym)
  allowed_mentions[:replied_user] = mention_user

  respond(content, tts, embed, attachments, allowed_mentions, self, components, flags)
end

#reply?true, false

Whether or not this message was sent in reply to another message

Returns:

  • (true, false)


417
418
419
# File 'lib/discordrb/data/message.rb', line 417

def reply?
  !@referenced_message.nil?
end

#respond(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0) ⇒ Message

Sends a message to this channel.

Parameters:

  • content (String)

    The content to send. Should not be longer than 2000 characters or it will result in an error.

  • tts (true, false) (defaults to: false)

    Whether or not this message should be sent using Discord text-to-speech.

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

    The rich embed to append to this message.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png

  • allowed_mentions (Hash, Discordrb::AllowedMentions, false, nil) (defaults to: nil)

    Mentions that are allowed to ping on this message. false disables all pings

  • message_reference (Message, String, Integer, nil) (defaults to: nil)

    The message, or message ID, to reply to if any.

  • components (View, Array<Hash>) (defaults to: nil)

    Interaction components to associate with this message.

  • flags (Integer) (defaults to: 0)

    Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2) and SUPPRESS_NOTIFICATIONS (1 << 12) can be set.

Returns:

  • (Message)

    the message that was sent.



218
219
220
# File 'lib/discordrb/data/message.rb', line 218

def respond(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0)
  @channel.send_message(content, tts, embed, attachments, allowed_mentions, message_reference, components, flags)
end

#suppress_embedsMessage

Removes embeds from the message

Returns:

  • (Message)

    the resulting message.



327
328
329
330
331
# File 'lib/discordrb/data/message.rb', line 327

def suppress_embeds
  flags = @flags | (1 << 2)
  response = API::Channel.edit_message(@bot.token, @channel.id, @id, :undef, :undef, :undef, :undef, flags)
  Message.new(JSON.parse(response), @bot)
end

#to_messageDiscordrb::Message Also known as: message

to_message -> self or message

Returns:



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

def to_message
  self
end

#unpin(reason = nil) ⇒ Object

Unpins this message



251
252
253
254
255
# File 'lib/discordrb/data/message.rb', line 251

def unpin(reason = nil)
  API::Channel.unpin_message(@bot.token, @channel.id, @id, reason)
  @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.



295
296
297
# File 'lib/discordrb/data/message.rb', line 295

def webhook?
  !@webhook_id.nil?
end