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

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)



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

def attachments
  @attachments
end

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



15
16
17
# File 'lib/discordrb/data/message.rb', line 15

def author
  @author
end

#channelChannel (readonly)



20
21
22
# File 'lib/discordrb/data/message.rb', line 20

def channel
  @channel
end

#componentsArray<Component> (readonly)



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

def components
  @components
end

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



9
10
11
# File 'lib/discordrb/data/message.rb', line 9

def content
  @content
end

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



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

def edited
  @edited
end

#edited_timestampTime (readonly) Also known as: edit_timestamp



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

def edited_timestamp
  @edited_timestamp
end

#embedsArray<Embed> (readonly)



39
40
41
# File 'lib/discordrb/data/message.rb', line 39

def embeds
  @embeds
end

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



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

def mention_everyone
  @mention_everyone
end

#mentionsArray<User> (readonly)



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

def mentions
  @mentions
end

#nonceString (readonly)



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

def nonce
  @nonce
end

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



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

def pinned
  @pinned
end

#reactionsArray<Reaction> (readonly)



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

def reactions
  @reactions
end

#role_mentionsArray<Role> (readonly)



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

def role_mentions
  @role_mentions
end

#serverServer? (readonly)



65
66
67
# File 'lib/discordrb/data/message.rb', line 65

def server
  @server
end

#timestampTime (readonly)



23
24
25
# File 'lib/discordrb/data/message.rb', line 23

def timestamp
  @timestamp
end

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



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

def tts
  @tts
end

#webhook_idInteger? (readonly)



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

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


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

def all_reaction_users(limit: 100)
  all_reactions = @reactions.map { |r| { r.to_s => reacted_with(r, limit: limit) } }
  all_reactions.reduce({}, :merge)
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:



222
223
224
# File 'lib/discordrb/data/message.rb', line 222

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:



228
229
230
# File 'lib/discordrb/data/message.rb', line 228

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:



235
236
237
# File 'lib/discordrb/data/message.rb', line 235

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:



241
242
243
# File 'lib/discordrb/data/message.rb', line 241

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

#buttonsArray<Components::Button>



379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/discordrb/data/message.rb', line 379

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

  results.flatten.compact
end

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

Reacts to a message.



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

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

#delete(reason = nil) ⇒ Object

Deletes this message.



200
201
202
203
# File 'lib/discordrb/data/message.rb', line 200

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.



347
348
349
# File 'lib/discordrb/data/message.rb', line 347

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.



341
342
343
344
# File 'lib/discordrb/data/message.rb', line 341

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.



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

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, components = nil) ⇒ Message

Edits this message to have the specified content instead. You can only edit your own messages.



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

def edit(new_content, new_embed = nil, components = nil)
  response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content, [], new_embed ? new_embed.to_hash : nil, components)
  Message.new(JSON.parse(response), @bot)
end

#emojiArray<Emoji>



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

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.



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

def emoji?
  emoji&.empty?
end

#from_bot?true, false



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

def from_bot?
  @author&.current_bot?
end

#inspectObject

The inspect method is overwritten to give more useful output



352
353
354
# File 'lib/discordrb/data/message.rb', line 352

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


357
358
359
# File 'lib/discordrb/data/message.rb', line 357

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.



277
278
279
# File 'lib/discordrb/data/message.rb', line 277

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

#pin(reason = nil) ⇒ Object

Pins this message



206
207
208
209
210
# File 'lib/discordrb/data/message.rb', line 206

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


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/discordrb/data/message.rb', line 297

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



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

def reactions?
  !@reactions.empty?
end

#referenced_messageMessage?



370
371
372
373
374
375
376
# File 'lib/discordrb/data/message.rb', line 370

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

Deprecated.

Please use #respond.

Replies to this message with the specified content.



163
164
165
# File 'lib/discordrb/data/message.rb', line 163

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

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

Responds to this message as an inline reply.



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

def reply!(content, tts: false, embed: nil, attachments: nil, allowed_mentions: {}, mention_user: false, components: nil)
  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)
end

#reply?true, false

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



365
366
367
# File 'lib/discordrb/data/message.rb', line 365

def reply?
  !@referenced_message.nil?
end

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

Sends a message to this channel.



185
186
187
# File 'lib/discordrb/data/message.rb', line 185

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

#unpin(reason = nil) ⇒ Object

Unpins this message



213
214
215
216
217
# File 'lib/discordrb/data/message.rb', line 213

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

#webhook?true, false



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

def webhook?
  !@webhook_id.nil?
end