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)

Returns the files attached to this message.

Returns:

  • (Array<Attachment>)

    the files attached to this message.



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

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)



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

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.



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

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.



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

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.



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

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.



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

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.



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?

Returns whether the message mentioned everyone or not.

Returns:

  • (true, false)

    whether the message mentioned everyone or not.



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

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.



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

def mentions
  @mentions
end

#nonceString (readonly)

Returns used for validating a message was sent.

Returns:

  • (String)

    used for validating a message was sent.



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

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.



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

def pinned
  @pinned
end

#reactionsArray<Reaction> (readonly)

Returns the reaction objects contained in this message.

Returns:

  • (Array<Reaction>)

    the reaction objects contained in this message.



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

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.



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

def role_mentions
  @role_mentions
end

#serverServer? (readonly)

Returns the server in which this message was sent.

Returns:

  • (Server, nil)

    the server in which this message was sent.



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

def server
  @server
end

#timestampTime (readonly)

Returns the timestamp at which this message was sent.

Returns:

  • (Time)

    the timestamp at which this message was sent.



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

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.



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

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.



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

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:



183
184
185
# File 'lib/discordrb/data/message.rb', line 183

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:



189
190
191
# File 'lib/discordrb/data/message.rb', line 189

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

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

Reacts to a message.

Parameters:

  • reaction (String, #to_reaction)

    the unicode emoji or Emoji



243
244
245
246
247
# File 'lib/discordrb/data/message.rb', line 243

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.



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

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

#delete_all_reactionsObject

Removes all reactions from this message.



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

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



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

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, String, Integer)

    the user or user ID who used this reaction

  • reaction (String, #to_reaction)

    the reaction to remove



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

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.



155
156
157
158
# File 'lib/discordrb/data/message.rb', line 155

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.



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

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



224
225
226
227
# File 'lib/discordrb/data/message.rb', line 224

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.



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

def from_bot?
  @author&.current_bot?
end

#inspectObject

The inspect method is overwritten to give more useful output



288
289
290
# File 'lib/discordrb/data/message.rb', line 288

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



293
294
295
# File 'lib/discordrb/data/message.rb', line 293

def link
  "https://discordapp.com/channels/#{@server&.id || '@me'}/#{@channel.id}/#{@id}"
end

#my_reactionsArray<Reaction>

Returns the reactions made by the current bot or user.

Returns:



237
238
239
# File 'lib/discordrb/data/message.rb', line 237

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

#pinObject

Pins this message



167
168
169
170
171
# File 'lib/discordrb/data/message.rb', line 167

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

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

Returns:

  • (Array<User>)

    the users who used this reaction



257
258
259
260
261
262
263
264
265
# File 'lib/discordrb/data/message.rb', line 257

def reacted_with(reaction, limit: 100)
  reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction)
  paginator = Paginator.new(limit, :down) do |last_page|
    after_id = last_page.last.id if last_page
    last_page = JSON.parse(API::Channel.get_reactions(@bot.token, @channel.id, @id, reaction, nil, after_id, limit))
    last_page.map { |d| User.new(d, @bot) }
  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



231
232
233
# File 'lib/discordrb/data/message.rb', line 231

def reactions?
  @reactions.empty?
end

#reply(content) ⇒ Object

Replies to this message with the specified content.



146
147
148
# File 'lib/discordrb/data/message.rb', line 146

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

#unpinObject

Unpins this message



174
175
176
177
178
# File 'lib/discordrb/data/message.rb', line 174

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.



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

def webhook?
  !@webhook_id.nil?
end