Class: Rubycord::Message
- Inherits:
-
Object
- Object
- Rubycord::Message
- Includes:
- IDObject
- Defined in:
- lib/rubycord/data/message.rb
Overview
A message on Discord that was sent to a text channel
Instance Attribute Summary collapse
-
#attachments ⇒ Array<Attachment>
readonly
The files attached to this message.
-
#author ⇒ Member, User
(also: #user, #writer)
readonly
The user that sent this message.
-
#channel ⇒ Channel
readonly
The channel in which this message was sent.
- #components ⇒ Array<Component> readonly
-
#content ⇒ String
(also: #text, #to_s)
readonly
The content of this message.
-
#edited ⇒ true, false
(also: #edited?)
readonly
Whether the message was edited or not.
-
#edited_timestamp ⇒ Time
(also: #edit_timestamp)
readonly
The timestamp at which this message was edited.
-
#embeds ⇒ Array<Embed>
readonly
The embed objects contained in this message.
-
#mention_everyone ⇒ true, false
(also: #mention_everyone?, #mentions_everyone?)
readonly
Whether the message mentioned everyone or not.
-
#mentions ⇒ Array<User>
readonly
The users that were mentioned in this message.
-
#nonce ⇒ String
readonly
Used for validating a message was sent.
-
#pinned ⇒ true, false
(also: #pinned?)
readonly
Whether the message is pinned or not.
-
#reactions ⇒ Array<Reaction>
readonly
The reaction objects contained in this message.
-
#role_mentions ⇒ Array<Role>
readonly
The roles that were mentioned in this message.
-
#server ⇒ Server?
readonly
The server in which this message was sent.
-
#timestamp ⇒ Time
readonly
The timestamp at which this message was sent.
-
#tts ⇒ true, false
(also: #tts?)
readonly
Whether the message used Text-To-Speech (TTS) or not.
-
#type ⇒ Integer
readonly
What the type of the message is.
-
#webhook_id ⇒ Integer?
readonly
The webhook ID that sent this message, or
nil
if it wasn't sent through a webhook.
Attributes included from IDObject
Instance Method Summary collapse
-
#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.
-
#await(key, attributes = {}) ⇒ Object
deprecated
Deprecated.
Will be changed to blocking behavior in v4.0. Use #await! instead.
-
#await!(attributes = {}) ⇒ Object
Add a blocking Await for a message with the same user and channel.
-
#await_reaction(key, attributes = {}) ⇒ Object
deprecated
Deprecated.
Will be changed to blocking behavior in v4.0. Use #await_reaction! instead.
-
#await_reaction!(attributes = {}) ⇒ Object
Add a blocking Await for a reaction to be added on this message.
- #buttons ⇒ Array<Components::Button>
-
#chat_input_command? ⇒ true, false
Whether or not this message was of type "CHAT_INPUT_COMMAND".
-
#create_reaction(reaction) ⇒ Object
(also: #react)
Reacts to a message.
-
#delete(reason = nil) ⇒ Object
Deletes this message.
-
#delete_all_reactions ⇒ Object
Removes all reactions from this message.
-
#delete_own_reaction(reaction) ⇒ Object
Deletes this client's reaction on this message.
-
#delete_reaction(user, reaction) ⇒ Object
Deletes a reaction made by a user on this message.
-
#edit(new_content, new_embeds = nil, new_components = nil) ⇒ Message
Edits this message to have the specified content instead.
-
#emoji ⇒ Array<Emoji>
The emotes that were used/mentioned in this message.
-
#emoji? ⇒ true, false
Check if any emoji were used in this message.
-
#from_bot? ⇒ true, false
Whether this message was sent by the current Bot.
-
#inspect ⇒ Object
The inspect method is overwritten to give more useful output.
-
#link ⇒ String
(also: #jump_link)
A URL that a user can use to navigate to this message in the client.
-
#my_reactions ⇒ Array<Reaction>
Returns the reactions made by the current bot or user.
-
#pin(reason = nil) ⇒ Object
Pins this message.
-
#reacted_with(reaction, limit: 100) ⇒ Array<User>
Returns the list of users who reacted with a certain reaction.
-
#reactions? ⇒ true, false
Check if any reactions were used in this message.
-
#referenced_message ⇒ Message?
The Message this Message was sent in reply to.
-
#reply(content) ⇒ Message
deprecated
Deprecated.
Please use #respond.
-
#reply!(content, tts: false, embed: nil, attachments: nil, allowed_mentions: {}, mention_user: false, components: nil) ⇒ Message
Responds to this message as an inline reply.
-
#reply? ⇒ true, false
Whether or not this message was sent in reply to another message.
-
#respond(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil) ⇒ Message
Sends a message to this channel.
-
#to_message ⇒ Rubycord::Message
(also: #message)
to_message -> self or message.
-
#unpin(reason = nil) ⇒ Object
Unpins this message.
-
#webhook? ⇒ true, false
Whether this message has been sent over a webhook.
Methods included from IDObject
#==, #creation_time, synthesise
Instance Attribute Details
#attachments ⇒ Array<Attachment> (readonly)
Returns the files attached to this message.
34 35 36 |
# File 'lib/rubycord/data/message.rb', line 34 def @attachments end |
#author ⇒ Member, User (readonly) Also known as: user, writer
Returns the user that sent this message. (Will be a Rubycord::Member most of the time, it should only be a User for old messages when the author has left the server since then).
13 14 15 |
# File 'lib/rubycord/data/message.rb', line 13 def @author end |
#channel ⇒ Channel (readonly)
Returns the channel in which this message was sent.
18 19 20 |
# File 'lib/rubycord/data/message.rb', line 18 def channel @channel end |
#components ⇒ Array<Component> (readonly)
72 73 74 |
# File 'lib/rubycord/data/message.rb', line 72 def components @components end |
#content ⇒ String (readonly) Also known as: text, to_s
Returns the content of this message.
7 8 9 |
# File 'lib/rubycord/data/message.rb', line 7 def content @content end |
#edited ⇒ true, false (readonly) Also known as: edited?
Returns whether the message was edited or not.
50 51 52 |
# File 'lib/rubycord/data/message.rb', line 50 def edited @edited end |
#edited_timestamp ⇒ Time (readonly) Also known as: edit_timestamp
Returns the timestamp at which this message was edited. nil
if the message was never edited.
24 25 26 |
# File 'lib/rubycord/data/message.rb', line 24 def @edited_timestamp end |
#embeds ⇒ Array<Embed> (readonly)
Returns the embed objects contained in this message.
37 38 39 |
# File 'lib/rubycord/data/message.rb', line 37 def @embeds end |
#mention_everyone ⇒ true, false (readonly) Also known as: mention_everyone?, mentions_everyone?
Returns whether the message mentioned everyone or not.
54 55 56 |
# File 'lib/rubycord/data/message.rb', line 54 def mention_everyone @mention_everyone end |
#mentions ⇒ Array<User> (readonly)
Returns the users that were mentioned in this message.
28 29 30 |
# File 'lib/rubycord/data/message.rb', line 28 def mentions @mentions end |
#nonce ⇒ String (readonly)
Returns used for validating a message was sent.
47 48 49 |
# File 'lib/rubycord/data/message.rb', line 47 def nonce @nonce end |
#pinned ⇒ true, false (readonly) Also known as: pinned?
Returns whether the message is pinned or not.
59 60 61 |
# File 'lib/rubycord/data/message.rb', line 59 def pinned @pinned end |
#reactions ⇒ Array<Reaction> (readonly)
Returns the reaction objects contained in this message.
40 41 42 |
# File 'lib/rubycord/data/message.rb', line 40 def reactions @reactions end |
#role_mentions ⇒ Array<Role> (readonly)
Returns the roles that were mentioned in this message.
31 32 33 |
# File 'lib/rubycord/data/message.rb', line 31 def role_mentions @role_mentions end |
#server ⇒ Server? (readonly)
Returns the server in which this message was sent.
66 67 68 |
# File 'lib/rubycord/data/message.rb', line 66 def server @server end |
#timestamp ⇒ Time (readonly)
Returns the timestamp at which this message was sent.
21 22 23 |
# File 'lib/rubycord/data/message.rb', line 21 def @timestamp end |
#tts ⇒ true, false (readonly) Also known as: tts?
Returns whether the message used Text-To-Speech (TTS) or not.
43 44 45 |
# File 'lib/rubycord/data/message.rb', line 43 def tts @tts end |
#type ⇒ Integer (readonly)
Returns what the type of the message is.
63 64 65 |
# File 'lib/rubycord/data/message.rb', line 63 def type @type end |
#webhook_id ⇒ Integer? (readonly)
Returns the webhook ID that sent this message, or nil
if it wasn't sent through a webhook.
69 70 71 |
# File 'lib/rubycord/data/message.rb', line 69 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.
332 333 334 335 |
# File 'lib/rubycord/data/message.rb', line 332 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 = {}) ⇒ Object
228 229 230 |
# File 'lib/rubycord/data/message.rb', line 228 def await(key, attributes = {}, &) @bot.add_await(key, Rubycord::Events::MessageEvent, {from: @author.id, in: @channel.id}.merge(attributes), &) end |
#await!(attributes = {}) ⇒ Object
Add a blocking Await for a message with the same user and channel.
234 235 236 |
# File 'lib/rubycord/data/message.rb', line 234 def await!(attributes = {}, &) @bot.add_await!(Rubycord::Events::MessageEvent, {from: @author.id, in: @channel.id}.merge(attributes), &) end |
#await_reaction(key, attributes = {}) ⇒ Object
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.
241 242 243 |
# File 'lib/rubycord/data/message.rb', line 241 def await_reaction(key, attributes = {}, &) @bot.add_await(key, Rubycord::Events::ReactionAddEvent, {message: @id}.merge(attributes), &) end |
#await_reaction!(attributes = {}) ⇒ Object
Add a blocking Await for a reaction to be added on this message.
247 248 249 |
# File 'lib/rubycord/data/message.rb', line 247 def await_reaction!(attributes = {}, &) @bot.add_await!(Rubycord::Events::ReactionAddEvent, {message: @id}.merge(attributes), &) end |
#buttons ⇒ Array<Components::Button>
391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/rubycord/data/message.rb', line 391 def results = @components.collect do |component| case component when Components::Button component when Components::ActionRow component. end end results.flatten.compact end |
#chat_input_command? ⇒ true, false
Whether or not this message was of type "CHAT_INPUT_COMMAND"
377 378 379 |
# File 'lib/rubycord/data/message.rb', line 377 def chat_input_command? @type == 20 end |
#create_reaction(reaction) ⇒ Object Also known as: react
Reacts to a message.
289 290 291 292 293 |
# File 'lib/rubycord/data/message.rb', line 289 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.
206 207 208 209 |
# File 'lib/rubycord/data/message.rb', line 206 def delete(reason = nil) API::Channel.(@bot.token, @channel.id, @id, reason) nil end |
#delete_all_reactions ⇒ Object
Removes all reactions from this message.
353 354 355 |
# File 'lib/rubycord/data/message.rb', line 353 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.
347 348 349 350 |
# File 'lib/rubycord/data/message.rb', line 347 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.
340 341 342 343 |
# File 'lib/rubycord/data/message.rb', line 340 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_embeds = nil, new_components = nil) ⇒ Message
Edits this message to have the specified content instead. You can only edit your own messages.
197 198 199 200 201 202 203 |
# File 'lib/rubycord/data/message.rb', line 197 def edit(new_content, = nil, new_components = nil) = (.instance_of?(Array) ? .map(&:to_hash) : [&.to_hash]).compact new_components = new_components.to_a response = API::Channel.(@bot.token, @channel.id, @id, new_content, [], , new_components) Message.new(JSON.parse(response), @bot) end |
#emoji ⇒ Array<Emoji>
Returns the emotes that were used/mentioned in this message.
262 263 264 265 266 267 |
# File 'lib/rubycord/data/message.rb', line 262 def emoji return if @content.nil? return @emoji unless @emoji.empty? @emoji = @bot.parse_mentions(@content).select { |el| el.is_a? Rubycord::Emoji } end |
#emoji? ⇒ true, false
Check if any emoji were used in this message.
271 272 273 |
# File 'lib/rubycord/data/message.rb', line 271 def emoji? emoji&.empty? end |
#from_bot? ⇒ true, false
Returns whether this message was sent by the current Bot.
252 253 254 |
# File 'lib/rubycord/data/message.rb', line 252 def from_bot? @author&.current_bot? end |
#inspect ⇒ Object
The inspect method is overwritten to give more useful output
358 359 360 |
# File 'lib/rubycord/data/message.rb', line 358 def inspect "<Message content=\"#{@content}\" id=#{@id} timestamp=#{@timestamp} author=#{@author} channel=#{@channel}>" end |
#link ⇒ String Also known as: jump_link
Returns a URL that a user can use to navigate to this message in the client.
363 364 365 |
# File 'lib/rubycord/data/message.rb', line 363 def link "https://discord.com/channels/#{@server&.id || "@me"}/#{@channel.id}/#{@id}" end |
#my_reactions ⇒ Array<Reaction>
Returns the reactions made by the current bot or user.
283 284 285 |
# File 'lib/rubycord/data/message.rb', line 283 def my_reactions @reactions.select(&:me) end |
#pin(reason = nil) ⇒ Object
Pins this message
212 213 214 215 216 |
# File 'lib/rubycord/data/message.rb', line 212 def pin(reason = nil) API::Channel.(@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.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/rubycord/data/message.rb', line 303 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.
277 278 279 |
# File 'lib/rubycord/data/message.rb', line 277 def reactions? !@reactions.empty? end |
#referenced_message ⇒ Message?
Returns the Message this Message was sent in reply to.
382 383 384 385 386 387 388 |
# File 'lib/rubycord/data/message.rb', line 382 def return @referenced_message if @referenced_message return nil unless @message_reference referenced_channel = @bot.channel(@message_reference["channel_id"]) @referenced_message = referenced_channel.(@message_reference["message_id"]) end |
#reply(content) ⇒ Message
Please use #respond.
Replies to this message with the specified content.
165 166 167 |
# File 'lib/rubycord/data/message.rb', line 165 def reply(content) @channel.(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.
178 179 180 181 182 183 184 |
# File 'lib/rubycord/data/message.rb', line 178 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, , , allowed_mentions, self, components) end |
#reply? ⇒ true, false
Whether or not this message was sent in reply to another message
371 372 373 |
# File 'lib/rubycord/data/message.rb', line 371 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.
187 188 189 |
# File 'lib/rubycord/data/message.rb', line 187 def respond(content, tts = false, = nil, = nil, allowed_mentions = nil, = nil, components = nil) @channel.(content, tts, , , allowed_mentions, , components) end |
#to_message ⇒ Rubycord::Message Also known as: message
to_message -> self or message
406 407 408 |
# File 'lib/rubycord/data/message.rb', line 406 def self end |
#unpin(reason = nil) ⇒ Object
Unpins this message
219 220 221 222 223 |
# File 'lib/rubycord/data/message.rb', line 219 def unpin(reason = nil) API::Channel.(@bot.token, @channel.id, @id, reason) @pinned = false nil end |
#webhook? ⇒ true, false
Returns whether this message has been sent over a webhook.
257 258 259 |
# File 'lib/rubycord/data/message.rb', line 257 def webhook? !@webhook_id.nil? end |