Class: Discordrb::Message
- Inherits:
-
Object
- Object
- Discordrb::Message
- 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
- TYPES =
Map of message types.
{ default: 0, recipient_add: 1, recipient_remove: 2, call: 3, channel_name_change: 4, channel_icon_change: 5, channel_pinned_message: 6, server_member_join: 7, server_boost: 8, server_boost_tier_one: 9, server_boost_tier_two: 10, server_boost_tier_three: 11, channel_follow_add: 12, server_discovery_disqualified: 14, server_discovery_requalified: 15, server_discovery_grace_period_initial_warning: 16, server_discovery_grace_period_final_warning: 17, thread_created: 18, reply: 19, chat_input_command: 20, thread_starter_message: 21, server_invite_reminder: 22, context_menu_command: 23, automod_action: 24, role_subscription_purchase: 25, interaction_premium_upsell: 26, stage_start: 27, stage_end: 28, stage_speaker: 29, stage_raise_hand: 30, stage_topic: 31, server_application_premium_subscription: 32, server_incident_alert_mode_enabled: 36, server_incident_alert_mode_disabled: 37, server_incident_report_raid: 38, server_incident_report_false_alarm: 39, purchase_notification: 44, poll_result: 46, changelog: 47, server_join_request_accepted: 52, server_join_request_rejected: 53, server_join_request_withdrawn: 54, report_to_mod_deleted_message: 58, report_to_mod_timeout_user: 59, report_to_mod_kick_user: 60, report_to_mod_ban_user: 61, report_to_mod_closed_report: 62, server_emoji_added: 63 }.freeze
Instance Attribute Summary collapse
-
#attachments ⇒ Array<Attachment>
readonly
The files attached to this message.
-
#call ⇒ Call?
readonly
The call in a private channel that prompted this message.
-
#channel ⇒ Channel
readonly
The channel in which this message was sent.
-
#components ⇒ Array<Component>
readonly
Interaction components for this message.
-
#content ⇒ String
(also: #text, #to_s)
readonly
The content of this message.
-
#edited ⇒ true, false
(also: #edited?)
readonly
Whether the message was edited or not.
-
#edited_timestamp ⇒ Time
(also: #edit_timestamp)
readonly
The timestamp at which this message was edited.
-
#embeds ⇒ Array<Embed>
readonly
The embed objects contained in this message.
-
#flags ⇒ Integer
readonly
Flags set on the message.
-
#mention_everyone ⇒ true, false
(also: #mention_everyone?, #mentions_everyone?)
readonly
Whether the message mentioned everyone or not.
-
#mentions ⇒ Array<User>
readonly
The users that were mentioned in this message.
-
#nonce ⇒ String
readonly
Used for validating a message was sent.
-
#pinned ⇒ true, false
(also: #pinned?)
readonly
Whether the message is pinned or not.
-
#pinned_at ⇒ Time?
readonly
The time at when this message was pinned.
-
#reactions ⇒ Array<Reaction>
readonly
The reaction objects contained in this message.
-
#role_mentions ⇒ Array<Role>
readonly
The roles that were mentioned in this message.
-
#server ⇒ Server?
readonly
The server in which this message was sent.
-
#snapshots ⇒ Array<Snapshot>
readonly
The message snapshots included in this message.
-
#thread ⇒ Channel?
readonly
The thread that was started from this message, or nil.
-
#timestamp ⇒ Time
readonly
The timestamp at which this message was sent.
-
#tts ⇒ true, false
(also: #tts?)
readonly
Whether the message used Text-To-Speech (TTS) or not.
-
#type ⇒ Integer
readonly
What the type of the message is.
-
#webhook_id ⇒ Integer?
readonly
The webhook ID that sent this message, or
nil
if it wasn't sent through a webhook.
Attributes included from IDObject
Instance Method Summary collapse
-
#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.
-
#author ⇒ Member, User
(also: #user, #writer)
The user that sent this message.
-
#await(key, attributes = {}, &block) ⇒ Object
deprecated
Deprecated.
Will be changed to blocking behavior in v4.0. Use #await! instead.
-
#await!(attributes = {}, &block) ⇒ Object
Add a blocking Await for a message with the same user and channel.
-
#await_reaction(key, attributes = {}, &block) ⇒ Object
deprecated
Deprecated.
Will be changed to blocking behavior in v4.0. Use #await_reaction! instead.
-
#await_reaction!(attributes = {}, &block) ⇒ Object
Add a blocking Await for a reaction to be added on this message.
- #buttons ⇒ Array<Components::Button>
-
#create_reaction(reaction) ⇒ Object
(also: #react)
Reacts to a message.
-
#crosspost ⇒ Object
Crossposts a message in a news channel.
-
#delete(reason = nil) ⇒ Object
Deletes this message.
-
#delete_all_reactions ⇒ Object
Removes all reactions from this message.
-
#delete_own_reaction(reaction) ⇒ Object
Deletes this client's reaction on this message.
-
#delete_reaction(user, reaction) ⇒ Object
Deletes a reaction made by a user on this message.
-
#edit(new_content, new_embeds = nil, new_components = nil, flags = 0) ⇒ Message
Edits this message to have the specified content instead.
-
#emoji ⇒ Array<Emoji>
The emotes that were used/mentioned in this message.
-
#emoji? ⇒ true, false
Check if any emoji were used in this message.
-
#forward(channel, must_exist: true, timeout: nil, flags: 0, nonce: nil, enforce_nonce: false) ⇒ Message?
Forward this message to another channel.
-
#from_bot? ⇒ true, false
Whether this message was sent by the current Bot.
-
#inspect ⇒ Object
The inspect method is overwritten to give more useful output.
-
#link ⇒ String
(also: #jump_link)
A URL that a user can use to navigate to this message in the client.
-
#mentions?(target) ⇒ true, false
Check if this message mentions a specific user or role.
-
#my_reactions ⇒ Array<Reaction>
Returns the reactions made by the current bot or user.
-
#pin(reason = nil) ⇒ Object
Pins this message.
-
#reacted_with(reaction, limit: 100) ⇒ Array<User>
Returns the list of users who reacted with a certain reaction.
-
#reactions? ⇒ true, false
Check if any reactions were used in this message.
-
#referenced_message ⇒ Message?
The Message this Message was sent in reply to.
-
#reply(content) ⇒ Message
deprecated
Deprecated.
Please use #respond.
-
#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.
-
#reply? ⇒ true, false
Whether or not this message was sent in reply to another message.
-
#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.
-
#suppress_embeds ⇒ Message
Removes embeds from the message.
-
#to_message ⇒ Discordrb::Message
(also: #message)
to_message -> self or message.
-
#to_reference(type: :reply, must_exist: true) ⇒ Hash
Convert this message to a hash that can be used to reference this message in a forward or a reply.
-
#unpin(reason = nil) ⇒ Object
Unpins this message.
-
#webhook? ⇒ true, false
Whether this message has been sent over a webhook.
Methods included from IDObject
#==, #creation_time, synthesise
Instance Attribute Details
#attachments ⇒ Array<Attachment> (readonly)
Returns the files attached to this message.
99 100 101 |
# File 'lib/discordrb/data/message.rb', line 99 def @attachments end |
#call ⇒ Call? (readonly)
Returns the call in a private channel that prompted this message.
149 150 151 |
# File 'lib/discordrb/data/message.rb', line 149 def call @call end |
#channel ⇒ Channel (readonly)
Returns the channel in which this message was sent.
83 84 85 |
# File 'lib/discordrb/data/message.rb', line 83 def channel @channel end |
#components ⇒ Array<Component> (readonly)
Returns Interaction components for this message.
137 138 139 |
# File 'lib/discordrb/data/message.rb', line 137 def components @components end |
#content ⇒ String (readonly) Also known as: text, to_s
Returns the content of this message.
78 79 80 |
# File 'lib/discordrb/data/message.rb', line 78 def content @content end |
#edited ⇒ true, false (readonly) Also known as: edited?
Returns whether the message was edited or not.
115 116 117 |
# File 'lib/discordrb/data/message.rb', line 115 def edited @edited end |
#edited_timestamp ⇒ Time (readonly) Also known as: edit_timestamp
Returns the timestamp at which this message was edited. nil
if the message was never edited.
89 90 91 |
# File 'lib/discordrb/data/message.rb', line 89 def @edited_timestamp end |
#embeds ⇒ Array<Embed> (readonly)
Returns the embed objects contained in this message.
102 103 104 |
# File 'lib/discordrb/data/message.rb', line 102 def @embeds end |
#flags ⇒ Integer (readonly)
Returns flags set on the message.
140 141 142 |
# File 'lib/discordrb/data/message.rb', line 140 def flags @flags end |
#mention_everyone ⇒ true, false (readonly) Also known as: mention_everyone?, mentions_everyone?
Returns whether the message mentioned everyone or not.
119 120 121 |
# File 'lib/discordrb/data/message.rb', line 119 def mention_everyone @mention_everyone end |
#mentions ⇒ Array<User> (readonly)
Returns the users that were mentioned in this message.
93 94 95 |
# File 'lib/discordrb/data/message.rb', line 93 def mentions @mentions end |
#nonce ⇒ String (readonly)
Returns used for validating a message was sent.
112 113 114 |
# File 'lib/discordrb/data/message.rb', line 112 def nonce @nonce end |
#pinned ⇒ true, false (readonly) Also known as: pinned?
Returns whether the message is pinned or not.
124 125 126 |
# File 'lib/discordrb/data/message.rb', line 124 def pinned @pinned end |
#pinned_at ⇒ Time? (readonly)
Returns the time at when this message was pinned. Only present on messages fetched via Channel#pins.
146 147 148 |
# File 'lib/discordrb/data/message.rb', line 146 def pinned_at @pinned_at end |
#reactions ⇒ Array<Reaction> (readonly)
Returns the reaction objects contained in this message.
105 106 107 |
# File 'lib/discordrb/data/message.rb', line 105 def reactions @reactions end |
#role_mentions ⇒ Array<Role> (readonly)
Returns the roles that were mentioned in this message.
96 97 98 |
# File 'lib/discordrb/data/message.rb', line 96 def role_mentions @role_mentions end |
#server ⇒ Server? (readonly)
Returns the server in which this message was sent.
131 132 133 |
# File 'lib/discordrb/data/message.rb', line 131 def server @server end |
#snapshots ⇒ Array<Snapshot> (readonly)
Returns the message snapshots included in this message.
152 153 154 |
# File 'lib/discordrb/data/message.rb', line 152 def snapshots @snapshots end |
#thread ⇒ Channel? (readonly)
Returns The thread that was started from this message, or nil.
143 144 145 |
# File 'lib/discordrb/data/message.rb', line 143 def thread @thread end |
#timestamp ⇒ Time (readonly)
Returns the timestamp at which this message was sent.
86 87 88 |
# File 'lib/discordrb/data/message.rb', line 86 def @timestamp end |
#tts ⇒ true, false (readonly) Also known as: tts?
Returns whether the message used Text-To-Speech (TTS) or not.
108 109 110 |
# File 'lib/discordrb/data/message.rb', line 108 def tts @tts end |
#type ⇒ Integer (readonly)
Returns what the type of the message is.
128 129 130 |
# File 'lib/discordrb/data/message.rb', line 128 def type @type end |
#webhook_id ⇒ Integer? (readonly)
Returns the webhook ID that sent this message, or nil
if it wasn't sent through a webhook.
134 135 136 |
# File 'lib/discordrb/data/message.rb', line 134 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.
451 452 453 454 |
# File 'lib/discordrb/data/message.rb', line 451 def all_reaction_users(limit: 100) all_reactions = @reactions.map { |r| { r.to_s => reacted_with(r, limit: limit) } } all_reactions.reduce({}, :merge) end |
#author ⇒ Member, 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).
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/discordrb/data/message.rb', line 238 def 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
328 329 330 |
# File 'lib/discordrb/data/message.rb', line 328 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.
334 335 336 |
# File 'lib/discordrb/data/message.rb', line 334 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
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.
341 342 343 |
# File 'lib/discordrb/data/message.rb', line 341 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.
347 348 349 |
# File 'lib/discordrb/data/message.rb', line 347 def await_reaction!(attributes = {}, &block) @bot.add_await!(Discordrb::Events::ReactionAddEvent, { message: @id }.merge(attributes), &block) end |
#buttons ⇒ Array<Components::Button>
504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/discordrb/data/message.rb', line 504 def results = @components.collect do |component| case component when Components::Button component when Components::ActionRow component. end end results.flatten.compact end |
#create_reaction(reaction) ⇒ Object Also known as: react
Reacts to a message.
408 409 410 411 412 |
# File 'lib/discordrb/data/message.rb', line 408 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 |
#crosspost ⇒ Object
Crossposts a message in a news channel.
320 321 322 323 |
# File 'lib/discordrb/data/message.rb', line 320 def crosspost response = API::Channel.(@bot.token, @channel.id, @id) Message.new(JSON.parse(response), @bot) end |
#delete(reason = nil) ⇒ Object
Deletes this message.
300 301 302 303 |
# File 'lib/discordrb/data/message.rb', line 300 def delete(reason = nil) API::Channel.(@bot.token, @channel.id, @id, reason) nil end |
#delete_all_reactions ⇒ Object
Removes all reactions from this message.
472 473 474 |
# File 'lib/discordrb/data/message.rb', line 472 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.
466 467 468 469 |
# File 'lib/discordrb/data/message.rb', line 466 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.
459 460 461 462 |
# File 'lib/discordrb/data/message.rb', line 459 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.
291 292 293 294 295 296 297 |
# File 'lib/discordrb/data/message.rb', line 291 def edit(new_content, = nil, new_components = nil, flags = 0) = (.instance_of?(Array) ? .map(&:to_hash) : [&.to_hash]).compact new_components = new_components.to_a response = API::Channel.(@bot.token, @channel.id, @id, new_content, [], , new_components, flags) Message.new(JSON.parse(response), @bot) end |
#emoji ⇒ Array<Emoji>
Returns the emotes that were used/mentioned in this message.
362 363 364 365 366 367 |
# File 'lib/discordrb/data/message.rb', line 362 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.
371 372 373 |
# File 'lib/discordrb/data/message.rb', line 371 def emoji? emoji&.empty? end |
#forward(channel, must_exist: true, timeout: nil, flags: 0, nonce: nil, enforce_nonce: false) ⇒ Message?
Forward this message to another channel.
555 556 557 558 559 |
# File 'lib/discordrb/data/message.rb', line 555 def forward(channel, must_exist: true, timeout: nil, flags: 0, nonce: nil, enforce_nonce: false) reference = to_reference(type: :forward, must_exist: must_exist) @bot.channel(channel).(reference: reference, timeout: timeout, flags: flags, nonce: nonce, enforce_nonce: enforce_nonce) end |
#from_bot? ⇒ true, false
Returns whether this message was sent by the current Bot.
352 353 354 |
# File 'lib/discordrb/data/message.rb', line 352 def from_bot? @author&.current_bot? end |
#inspect ⇒ Object
The inspect method is overwritten to give more useful output
477 478 479 |
# File 'lib/discordrb/data/message.rb', line 477 def inspect "<Message content=\"#{@content}\" id=#{@id} timestamp=#{@timestamp} author=#{@author} channel=#{@channel}>" end |
#link ⇒ String Also known as: jump_link
Returns a URL that a user can use to navigate to this message in the client.
482 483 484 |
# File 'lib/discordrb/data/message.rb', line 482 def link "https://discord.com/channels/#{@server&.id || '@me'}/#{@channel.id}/#{@id}" end |
#mentions?(target) ⇒ true, false
Check if this message mentions a specific user or role.
398 399 400 401 402 403 404 |
# File 'lib/discordrb/data/message.rb', line 398 def mentions?(target) mentions = (@mentions + role_mentions) mentions << server if @mention_everyone mentions.any?(target.resolve_id) end |
#my_reactions ⇒ Array<Reaction>
Returns the reactions made by the current bot or user.
383 384 385 |
# File 'lib/discordrb/data/message.rb', line 383 def my_reactions @reactions.select(&:me) end |
#pin(reason = nil) ⇒ Object
Pins this message
306 307 308 309 310 |
# File 'lib/discordrb/data/message.rb', line 306 def pin(reason = nil) API::Channel.(@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.
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/discordrb/data/message.rb', line 422 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.
377 378 379 |
# File 'lib/discordrb/data/message.rb', line 377 def reactions? !@reactions.empty? end |
#referenced_message ⇒ Message?
Returns the Message this Message was sent in reply to.
495 496 497 498 499 500 501 |
# File 'lib/discordrb/data/message.rb', line 495 def return @referenced_message if @referenced_message return nil unless @message_reference referenced_channel = @bot.channel(@message_reference['channel_id']) @referenced_message = referenced_channel&.(@message_reference['message_id']) end |
#reply(content) ⇒ Message
Please use #respond.
Replies to this message with the specified content.
257 258 259 |
# File 'lib/discordrb/data/message.rb', line 257 def reply(content) @channel.(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.
271 272 273 274 275 276 277 |
# File 'lib/discordrb/data/message.rb', line 271 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, , , allowed_mentions, self, components, flags) end |
#reply? ⇒ true, false
Whether or not this message was sent in reply to another message
490 491 492 |
# File 'lib/discordrb/data/message.rb', line 490 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.
280 281 282 |
# File 'lib/discordrb/data/message.rb', line 280 def respond(content, tts = false, = nil, = nil, allowed_mentions = nil, = nil, components = nil, flags = 0) @channel.(content, tts, , , allowed_mentions, , components, flags) end |
#suppress_embeds ⇒ Message
Removes embeds from the message
389 390 391 392 393 |
# File 'lib/discordrb/data/message.rb', line 389 def flags = @flags | (1 << 2) response = API::Channel.(@bot.token, @channel.id, @id, :undef, :undef, :undef, :undef, flags) Message.new(JSON.parse(response), @bot) end |
#to_message ⇒ Discordrb::Message Also known as: message
to_message -> self or message
519 520 521 |
# File 'lib/discordrb/data/message.rb', line 519 def self end |
#to_reference(type: :reply, must_exist: true) ⇒ Hash
Convert this message to a hash that can be used to reference this message in a forward or a reply.
541 542 543 544 545 |
# File 'lib/discordrb/data/message.rb', line 541 def to_reference(type: :reply, must_exist: true) type = (type == :reply ? 0 : 1) if type.is_a?(Symbol) { type: type, message_id: @id, channel_id: @channel.id, fail_if_not_exists: must_exist } end |
#unpin(reason = nil) ⇒ Object
Unpins this message
313 314 315 316 317 |
# File 'lib/discordrb/data/message.rb', line 313 def unpin(reason = nil) API::Channel.(@bot.token, @channel.id, @id, reason) @pinned = false nil end |
#webhook? ⇒ true, false
Returns whether this message has been sent over a webhook.
357 358 359 |
# File 'lib/discordrb/data/message.rb', line 357 def webhook? !@webhook_id.nil? end |