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'.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)


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

def attachments
  @attachments
end

#authorMember, User (readonly) Also known as: user, writer


2383
2384
2385
# File 'lib/discordrb/data.rb', line 2383

def author
  @author
end

#channelChannel (readonly)


2388
2389
2390
# File 'lib/discordrb/data.rb', line 2388

def channel
  @channel
end

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


2377
2378
2379
# File 'lib/discordrb/data.rb', line 2377

def content
  @content
end

#editedtrue, false (readonly) Also known as: edited?


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

def edited
  @edited
end

#edited_timestampTime (readonly) Also known as: edit_timestamp


2394
2395
2396
# File 'lib/discordrb/data.rb', line 2394

def edited_timestamp
  @edited_timestamp
end

#embedsArray<Embed> (readonly)


2407
2408
2409
# File 'lib/discordrb/data.rb', line 2407

def embeds
  @embeds
end

#mention_everyonetrue, false (readonly) Also known as: mention_everyone?, mentions_everyone?


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

def mention_everyone
  @mention_everyone
end

#mentionsArray<User> (readonly)


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

def mentions
  @mentions
end

#nonceString (readonly)


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

def nonce
  @nonce
end

#pinnedtrue, false (readonly) Also known as: pinned?


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

def pinned
  @pinned
end

#reactionsHash<String => Reaction> (readonly)


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

def reactions
  @reactions
end

#role_mentionsArray<Role> (readonly)


2401
2402
2403
# File 'lib/discordrb/data.rb', line 2401

def role_mentions
  @role_mentions
end

#timestampTime (readonly)


2391
2392
2393
# File 'lib/discordrb/data.rb', line 2391

def timestamp
  @timestamp
end

#ttstrue, false (readonly) Also known as: tts?


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

def tts
  @tts
end

#webhook_idInteger? (readonly)


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

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:


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

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:


2558
2559
2560
# File 'lib/discordrb/data.rb', line 2558

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.


2612
2613
2614
2615
2616
# File 'lib/discordrb/data.rb', line 2612

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.


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

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

#delete_all_reactionsObject

Removes all reactions from this message.


2647
2648
2649
# File 'lib/discordrb/data.rb', line 2647

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.


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

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.


2634
2635
2636
2637
# File 'lib/discordrb/data.rb', line 2634

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.


2524
2525
2526
2527
# File 'lib/discordrb/data.rb', line 2524

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>


2581
2582
2583
2584
2585
2586
2587
2588
2589
# File 'lib/discordrb/data.rb', line 2581

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.


2593
2594
2595
2596
# File 'lib/discordrb/data.rb', line 2593

def emoji?
  emoji = scan_for_emoji
  return true unless emoji.empty?
end

#from_bot?true, false


2563
2564
2565
# File 'lib/discordrb/data.rb', line 2563

def from_bot?
  @author && @author.current_bot?
end

#inspectObject

The inspect method is overwritten to give more useful output


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

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.


2606
2607
2608
# File 'lib/discordrb/data.rb', line 2606

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

#pinObject

Pins this message


2536
2537
2538
2539
2540
# File 'lib/discordrb/data.rb', line 2536

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}")

2625
2626
2627
2628
2629
# File 'lib/discordrb/data.rb', line 2625

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.


2600
2601
2602
# File 'lib/discordrb/data.rb', line 2600

def reactions?
  @reactions.any?
end

#reply(content) ⇒ Object

Replies to this message with the specified content.


2515
2516
2517
# File 'lib/discordrb/data.rb', line 2515

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

#unpinObject

Unpins this message


2543
2544
2545
2546
2547
# File 'lib/discordrb/data.rb', line 2543

def unpin
  API::Channel.unpin_message(@bot.token, @channel.id, @id)
  @pinned = false
  nil
end

#webhook?true, false


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

def webhook?
  !@webhook_id.nil?
end