Class: OnyxCord::Message

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/onyxcord/models/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,
  is_components_v2: OnyxCord::MessageComponents::IS_COMPONENTS_V2,
  components_v2: OnyxCord::MessageComponents::IS_COMPONENTS_V2
}.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

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time, synthesise

Instance Attribute Details

#activityMessageActivity? (readonly)

Returns the rich-presence activity that prompted this message.

Returns:

  • (MessageActivity, nil)

    the rich-presence activity that prompted this message.



166
167
168
# File 'lib/onyxcord/models/message.rb', line 166

def activity
  @activity
end

#application_idInteger? (readonly)

Returns the ID of the application associated with this message.

Returns:

  • (Integer, nil)

    the ID of the application associated with this message.



163
164
165
# File 'lib/onyxcord/models/message.rb', line 163

def application_id
  @application_id
end

#attachmentsArray<Attachment> (readonly)

Returns the files attached to this message.

Returns:

  • (Array<Attachment>)

    the files attached to this message.



97
98
99
# File 'lib/onyxcord/models/message.rb', line 97

def attachments
  @attachments
end

#callCall? (readonly)

Returns the call in a private channel that prompted this message.

Returns:

  • (Call, nil)

    the call in a private channel that prompted this message.



144
145
146
# File 'lib/onyxcord/models/message.rb', line 144

def call
  @call
end

#channelChannel (readonly)

Returns the channel in which this message was sent.

Returns:

  • (Channel)

    the channel in which this message was sent.



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

def channel
  @channel
end

#componentsArray<Component> (readonly)

Returns Interaction components for this message.

Returns:

  • (Array<Component>)

    Interaction components for this message.



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

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.



82
83
84
# File 'lib/onyxcord/models/message.rb', line 82

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.



113
114
115
# File 'lib/onyxcord/models/message.rb', line 113

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.



90
91
92
# File 'lib/onyxcord/models/message.rb', line 90

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.



100
101
102
# File 'lib/onyxcord/models/message.rb', line 100

def embeds
  @embeds
end

#flagsInteger (readonly)

Returns flags set on the message.

Returns:

  • (Integer)

    flags set on the message.



135
136
137
# File 'lib/onyxcord/models/message.rb', line 135

def flags
  @flags
end

#interaction_metadataInteractions::Metadata? (readonly)

Returns the metadata about the interaction that prompted this message.

Returns:



169
170
171
# File 'lib/onyxcord/models/message.rb', line 169

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



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

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.



94
95
96
# File 'lib/onyxcord/models/message.rb', line 94

def mentions
  @mentions
end

#nonceString (readonly)

Returns used for validating a message was sent.

Returns:

  • (String)

    used for validating a message was sent.



110
111
112
# File 'lib/onyxcord/models/message.rb', line 110

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.



122
123
124
# File 'lib/onyxcord/models/message.rb', line 122

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.



141
142
143
# File 'lib/onyxcord/models/message.rb', line 141

def pinned_at
  @pinned_at
end

#pollPoll? (readonly)

Returns the poll that was sent with this message, or nil.

Returns:

  • (Poll, nil)

    the poll that was sent with this message, or nil.



156
157
158
# File 'lib/onyxcord/models/message.rb', line 156

def poll
  @poll
end

#poll_resultPoll::Result? (readonly) Also known as: poll_results

Returns the finalised results for a poll that prompted this message.

Returns:

  • (Poll::Result, nil)

    the finalised results for a poll that prompted this message.



159
160
161
# File 'lib/onyxcord/models/message.rb', line 159

def poll_result
  @poll_result
end

#positionInteger (readonly)

Returns a generally increasing integer that can be used to determine this message's position in a thread.

Returns:

  • (Integer)

    a generally increasing integer that can be used to determine this message's position in a thread.



153
154
155
# File 'lib/onyxcord/models/message.rb', line 153

def position
  @position
end

#reactionsArray<Reaction> (readonly)

Returns the reaction objects contained in this message.

Returns:

  • (Array<Reaction>)

    the reaction objects contained in this message.



103
104
105
# File 'lib/onyxcord/models/message.rb', line 103

def reactions
  @reactions
end

#role_subscriptionRoleSubscriptionData? (readonly)

Returns the role subscription purchase or renewal that prompted this message.

Returns:

  • (RoleSubscriptionData, nil)

    the role subscription purchase or renewal that prompted this message.



150
151
152
# File 'lib/onyxcord/models/message.rb', line 150

def role_subscription
  @role_subscription
end

#snapshotsArray<Snapshot> (readonly)

Returns the message snapshots included in this message.

Returns:

  • (Array<Snapshot>)

    the message snapshots included in this message.



147
148
149
# File 'lib/onyxcord/models/message.rb', line 147

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



138
139
140
# File 'lib/onyxcord/models/message.rb', line 138

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



106
107
108
# File 'lib/onyxcord/models/message.rb', line 106

def tts
  @tts
end

#typeInteger (readonly)

Returns what the type of the message is.

Returns:

  • (Integer)

    what the type of the message is



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

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.



129
130
131
# File 'lib/onyxcord/models/message.rb', line 129

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.



485
486
487
# File 'lib/onyxcord/models/message.rb', line 485

def all_reaction_users(limit: 100)
  @reactions.to_h { |reaction| [reaction.to_s, reacted_with(reaction, limit: limit)] }
end

#authorMember, User Also known as: user, writer

Returns the user that sent this message. (Will be a OnyxCord::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 OnyxCord::Member most of the time, it should only be a User for old messages when the author has left the server since then)



234
235
236
237
238
239
240
241
242
243
# File 'lib/onyxcord/models/message.rb', line 234

def author
  return @author if @author

  unless @channel.private?
    @author = @channel.server.member(@author_id)
    OnyxCord::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.



356
357
358
# File 'lib/onyxcord/models/message.rb', line 356

def await(key, attributes = {}, &block)
  @bot.add_await(key, OnyxCord::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.



362
363
364
# File 'lib/onyxcord/models/message.rb', line 362

def await!(attributes = {}, &block)
  @bot.add_await!(OnyxCord::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.



369
370
371
# File 'lib/onyxcord/models/message.rb', line 369

def await_reaction(key, attributes = {}, &block)
  @bot.add_await(key, OnyxCord::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.



375
376
377
# File 'lib/onyxcord/models/message.rb', line 375

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

#buttonsArray<Components::Button>

Returns:



544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/onyxcord/models/message.rb', line 544

def buttons
  buttons = @components.flat_map do |component|
    case component
    when Components::Button
      component
    when Components::Section
      component.accessory if component.accessory.is_a?(Components::Button)
    when Components::ActionRow, Components::Container
      component.buttons
    end
  end

  buttons.compact
end

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

Reacts to a message.

Parameters:

  • reaction (String, #to_reaction)

    the unicode emoji or Emoji

Returns:

  • (nil)


440
441
442
443
444
# File 'lib/onyxcord/models/message.rb', line 440

def create_reaction(reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  REST::Channel.create_reaction(@bot.token, @channel.id, @id, reaction)
  nil
end

#crosspostMessage

Crossposts a message in a news channel.

Returns:

  • (Message)

    the updated message object.



348
349
350
351
# File 'lib/onyxcord/models/message.rb', line 348

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

#delete(reason = nil) ⇒ nil

Deletes this message.

Returns:

  • (nil)


325
326
327
328
# File 'lib/onyxcord/models/message.rb', line 325

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

#delete_all_reactionsObject

Removes all reactions from this message.



505
506
507
# File 'lib/onyxcord/models/message.rb', line 505

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

#delete_all_reactions_for_emoji(reaction) ⇒ Object

Removes all reactions for a single emoji.

Parameters:

  • reaction (String, #to_reaction)

    the reaction to remove.



511
512
513
514
# File 'lib/onyxcord/models/message.rb', line 511

def delete_all_reactions_for_emoji(reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  REST::Channel.delete_all_emoji_reactions(@bot.token, @channel.id, @id, reaction.to_s)
end

#delete_own_reaction(reaction) ⇒ Object

Deletes this client's reaction on this message.

Parameters:

  • reaction (String, #to_reaction)

    the reaction to remove



499
500
501
502
# File 'lib/onyxcord/models/message.rb', line 499

def delete_own_reaction(reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  REST::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



492
493
494
495
# File 'lib/onyxcord/models/message.rb', line 492

def delete_reaction(user, reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  REST::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, OnyxCord::Webhooks::Embed, Array<Hash>, Array<OnyxCord::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.



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/onyxcord/models/message.rb', line 306

def edit(new_content, new_embeds = nil, new_components = nil, flags = 0)
  new_embeds = if new_embeds == OnyxCord::MessagePayload::KEEP
                 new_embeds
               else
                 (if new_embeds.instance_of?(Array)
                    new_embeds.map { |embed| embed.respond_to?(:to_hash) ? embed.to_hash : embed }
                  else
                    [new_embeds&.to_hash]
                  end).compact
               end
  new_components = new_components.to_a
  flags = OnyxCord::MessageComponents.apply_v2_flag(flags, new_components)

  response = REST::Channel.edit_message(@bot.token, @channel.id, @id, new_content, :undef, new_embeds, new_components, flags)
  Message.new(OnyxCord::Internal::JSON.parse(response), @bot)
end

#emojiArray<Emoji> Also known as: emojis

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

Returns:

  • (Array<Emoji>)

    the emotes that were used/mentioned in this message.



390
391
392
393
394
# File 'lib/onyxcord/models/message.rb', line 390

def emoji
  return [] if @content.empty? || @content.nil?

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

#emoji?true, false Also known as: emojis?

Check if any emoji were used in this message.

Returns:

  • (true, false)

    whether or not any emoji were used



400
401
402
# File 'lib/onyxcord/models/message.rb', line 400

def emoji?
  emoji.any?
end

#forward(channel, must_exist: true, timeout: nil, flags: 0, nonce: nil, enforce_nonce: false) ⇒ Message?

Forward this message to another channel.

Parameters:

  • channel (Integer, String, Channel)

    The target channel to forward this message to.

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

    Whether to raise an error if this message was deleted when sending it.

  • timeout (Float, nil) (defaults to: nil)

    The amount of time in seconds after which the message sent will be deleted.

  • flags (Integer, Symbol, Array<Integer, Symbol>) (defaults to: 0)

    The message flags to set on the forwarded message.

  • nonce (String, Integer, nil) (defaults to: nil)

    The 25 character optional nonce that should be used when forwarding this message.

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

    Whether the provided nonce should be enforced and used for message de-duplication.

Returns:

  • (Message, nil)

    the message that was created from forwarding this one, or nil if this is a temporary message.



597
598
599
600
601
# File 'lib/onyxcord/models/message.rb', line 597

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).send_message!(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.

Returns:

  • (true, false)

    whether this message was sent by the current Bot.



380
381
382
# File 'lib/onyxcord/models/message.rb', line 380

def from_bot?
  (@author_id || @author&.id) == @bot.profile.id
end

#inspectObject

The inspect method is overwritten to give more useful output



517
518
519
# File 'lib/onyxcord/models/message.rb', line 517

def inspect
  "<Message content=\"#{@content}\" id=#{@id} timestamp=#{creation_time} 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



522
523
524
# File 'lib/onyxcord/models/message.rb', line 522

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.

Parameters:

Returns:

  • (true, false)

    whether or not this message mentions the target.



429
430
431
432
433
434
435
# File 'lib/onyxcord/models/message.rb', line 429

def mentions?(target)
  mentions = (@mentions + role_mentions)

  mentions << server if @mention_everyone

  mentions.any?(target.resolve_id)
end

#my_reactionsArray<Reaction>

Returns the reactions made by the current bot or user.

Returns:



414
415
416
# File 'lib/onyxcord/models/message.rb', line 414

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

#pin(reason = nil) ⇒ nil

Pins this message

Returns:

  • (nil)


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

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

#reacted_with(reaction, limit: 100, type: :normal) ⇒ 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

  • type (Integer, Symbol) (defaults to: :normal)

    the type of reaction to get. See Reaction::TYPES

Returns:

  • (Array<User>)

    the users who used this reaction



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/onyxcord/models/message.rb', line 455

def reacted_with(reaction, limit: 100, type: :normal)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  reaction = reaction.to_s if reaction.respond_to?(:to_s)
  type = Reaction::TYPES[type] || type

  get_reactions = proc do |fetch_limit, after_id = nil|
    resp = REST::Channel.get_reactions(@bot.token, @channel.id, @id, reaction, nil, after_id, fetch_limit, type)
    OnyxCord::Internal::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



408
409
410
# File 'lib/onyxcord/models/message.rb', line 408

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.



535
536
537
538
539
540
541
# File 'lib/onyxcord/models/message.rb', line 535

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:



272
273
274
# File 'lib/onyxcord/models/message.rb', line 272

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, OnyxCord::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, OnyxCord::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.



286
287
288
289
290
291
292
# File 'lib/onyxcord/models/message.rb', line 286

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)


530
531
532
# File 'lib/onyxcord/models/message.rb', line 530

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, OnyxCord::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, OnyxCord::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), SUPPRESS_NOTIFICATIONS (1 << 12), and IS_COMPONENTS_V2 (1 << 15) can be set.

Returns:

  • (Message)

    the message that was sent.



295
296
297
# File 'lib/onyxcord/models/message.rb', line 295

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

#role_mentionsArray<Role>

Get the roles that were mentioned in this message.

Returns:

  • (Array<Role>)

    the roles that were mentioned in this message.

Raises:



261
262
263
264
265
# File 'lib/onyxcord/models/message.rb', line 261

def role_mentions
  return [] if @channel.private? || @mention_roles.empty?

  @role_mentions ||= @mention_roles.map { |id| server.role(id) }
end

#serverServer?

Returns the server this message was sent in. If this message was sent in a PM channel, it will be nil.

Returns:

  • (Server, nil)

    the server this message was sent in. If this message was sent in a PM channel, it will be nil.

Raises:



251
252
253
254
255
# File 'lib/onyxcord/models/message.rb', line 251

def server
  return if @channel.private?

  @server ||= @channel.server
end

#suppress_embedsMessage

Removes embeds from the message

Returns:

  • (Message)

    the resulting message.



420
421
422
423
424
# File 'lib/onyxcord/models/message.rb', line 420

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

#timestampsArray<TimestampMarkdown>

Get the formatted timestamps contained in the message content.

Returns:



605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/onyxcord/models/message.rb', line 605

def timestamps
  return (@timestamps || []) if @timestamps || !@content || @content.empty?

  @timestamps = []

  @content.scan(/<t:(-?\d{1,13})(?::(t|T|d|D|f|F|s|S|R))?>/) do |time, style|
    # If it's not between these values, Discord won't show it, so don't bother.
    if (time = time.to_i).between?(-8_640_000_000_000, 8_640_000_000_000)
      @timestamps << TimestampMarkdown.new(Time.at(time), style)
    end
  end

  @timestamps
end

#to_messageOnyxCord::Message Also known as: message

to_message -> self or message

Returns:



561
562
563
# File 'lib/onyxcord/models/message.rb', line 561

def to_message
  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.

Parameters:

  • type (Integer, Symbol) (defaults to: :reply)

    The reference type to set. Can either be one of :reply or :forward.

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

    Whether to raise an error if this message was deleted when sending it.

Returns:

  • (Hash)

    the message as a hash representation that can be used in a forwarded message or a reply.



583
584
585
586
587
# File 'lib/onyxcord/models/message.rb', line 583

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) ⇒ nil

Unpins this message

Returns:

  • (nil)


340
341
342
343
344
# File 'lib/onyxcord/models/message.rb', line 340

def unpin(reason = nil)
  REST::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.



385
386
387
# File 'lib/onyxcord/models/message.rb', line 385

def webhook?
  !@webhook_id.nil?
end