Class: Discordrb::Message

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

Overview

A message on Discord that was sent to a text channel

Constant Summary collapse

ZERO_DISCRIM =

The discriminator that webhook user accounts have.

'0000'

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.



2426
2427
2428
# File 'lib/discordrb/data.rb', line 2426

def attachments
  @attachments
end

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



2405
2406
2407
# File 'lib/discordrb/data.rb', line 2405

def author
  @author
end

#channelChannel (readonly)

Returns the channel in which this message was sent.

Returns:

  • (Channel)

    the channel in which this message was sent.



2410
2411
2412
# File 'lib/discordrb/data.rb', line 2410

def channel
  @channel
end

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

Returns the content of this message.

Returns:

  • (String)

    the content of this message.



2399
2400
2401
# File 'lib/discordrb/data.rb', line 2399

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.



2442
2443
2444
# File 'lib/discordrb/data.rb', line 2442

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.



2416
2417
2418
# File 'lib/discordrb/data.rb', line 2416

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.



2429
2430
2431
# File 'lib/discordrb/data.rb', line 2429

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



2446
2447
2448
# File 'lib/discordrb/data.rb', line 2446

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.



2420
2421
2422
# File 'lib/discordrb/data.rb', line 2420

def mentions
  @mentions
end

#nonceString (readonly)

Returns used for validating a message was sent.

Returns:

  • (String)

    used for validating a message was sent.



2439
2440
2441
# File 'lib/discordrb/data.rb', line 2439

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.



2451
2452
2453
# File 'lib/discordrb/data.rb', line 2451

def pinned
  @pinned
end

#reactionsHash<String => Reaction> (readonly)

Returns the reaction objects attached to this message keyed by the name of the reaction.

Returns:

  • (Hash<String => Reaction>)

    the reaction objects attached to this message keyed by the name of the reaction



2432
2433
2434
# File 'lib/discordrb/data.rb', line 2432

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.



2423
2424
2425
# File 'lib/discordrb/data.rb', line 2423

def role_mentions
  @role_mentions
end

#timestampTime (readonly)

Returns the timestamp at which this message was sent.

Returns:

  • (Time)

    the timestamp at which this message was sent.



2413
2414
2415
# File 'lib/discordrb/data.rb', line 2413

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.



2435
2436
2437
# File 'lib/discordrb/data.rb', line 2435

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



2455
2456
2457
# File 'lib/discordrb/data.rb', line 2455

def webhook_id
  @webhook_id
end

Instance Method Details

#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:



2568
2569
2570
# File 'lib/discordrb/data.rb', line 2568

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

#await!(attributes = {}) ⇒ Object

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

See Also:



2574
2575
2576
# File 'lib/discordrb/data.rb', line 2574

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

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

Reacts to a message.

Parameters:

  • reaction (String, #to_reaction)

    the unicode emoji or Emoji



2628
2629
2630
2631
2632
# File 'lib/discordrb/data.rb', line 2628

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

#deleteObject

Deletes this message.



2546
2547
2548
2549
# File 'lib/discordrb/data.rb', line 2546

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

#delete_all_reactionsObject

Removes all reactions from this message.



2663
2664
2665
# File 'lib/discordrb/data.rb', line 2663

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



2657
2658
2659
2660
# File 'lib/discordrb/data.rb', line 2657

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, #resolve_id)

    the user who used this reaction

  • reaction (String, #to_reaction)

    the reaction to remove



2650
2651
2652
2653
# File 'lib/discordrb/data.rb', line 2650

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_embed = nil) ⇒ 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_embed (Hash, Discordrb::Webhooks::Embed, nil) (defaults to: nil)

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

Returns:

  • (Message)

    the resulting message.



2540
2541
2542
2543
# File 'lib/discordrb/data.rb', line 2540

def edit(new_content, new_embed = nil)
  response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content, [], new_embed ? new_embed.to_hash : nil)
  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.



2597
2598
2599
2600
2601
2602
2603
2604
2605
# File 'lib/discordrb/data.rb', line 2597

def emoji
  return if @content.nil?

  emoji = scan_for_emoji
  emoji.each do |element|
    @emoji << @bot.parse_mention(element)
  end
  @emoji
end

#emoji?true, false

Check if any emoji were used in this message.

Returns:

  • (true, false)

    whether or not any emoji were used



2609
2610
2611
2612
# File 'lib/discordrb/data.rb', line 2609

def emoji?
  emoji = scan_for_emoji
  return true unless 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.



2579
2580
2581
# File 'lib/discordrb/data.rb', line 2579

def from_bot?
  @author&.current_bot?
end

#inspectObject

The inspect method is overwritten to give more useful output



2668
2669
2670
# File 'lib/discordrb/data.rb', line 2668

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

#my_reactionsArray<Reaction>

Returns the reactions made by the current bot or user.

Returns:



2622
2623
2624
# File 'lib/discordrb/data.rb', line 2622

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

#pinObject

Pins this message



2552
2553
2554
2555
2556
# File 'lib/discordrb/data.rb', line 2552

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

#reacted_with(reaction) ⇒ Array<User>

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

Examples:

Get all the users that reacted with a thumbsup.

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

Parameters:

  • reaction (String, #to_reaction)

    the unicode emoji or Emoji

Returns:

  • (Array<User>)

    the users who used this reaction



2641
2642
2643
2644
2645
# File 'lib/discordrb/data.rb', line 2641

def reacted_with(reaction)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  response = JSON.parse(API::Channel.get_reactions(@bot.token, @channel.id, @id, reaction))
  response.map { |d| User.new(d, @bot) }
end

#reactions?true, false

Check if any reactions were used in this message.

Returns:

  • (true, false)

    whether or not this message has reactions



2616
2617
2618
# File 'lib/discordrb/data.rb', line 2616

def reactions?
  @reactions.any?
end

#reply(content) ⇒ Object

Replies to this message with the specified content.



2531
2532
2533
# File 'lib/discordrb/data.rb', line 2531

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

#unpinObject

Unpins this message



2559
2560
2561
2562
2563
# File 'lib/discordrb/data.rb', line 2559

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



2584
2585
2586
# File 'lib/discordrb/data.rb', line 2584

def webhook?
  !@webhook_id.nil?
end