Class: RubyCord::Message
- Inherits:
-
DiscordModel
- Object
- DiscordModel
- RubyCord::Message
- Defined in:
- lib/rubycord/message.rb,
lib/rubycord/message_meta.rb
Overview
Represents a message in Discord.
Direct Known Subclasses
Defined Under Namespace
Classes: Activity, Flag, Interaction, Reference, Sticker
Instance Attribute Summary collapse
-
#activity ⇒ RubyCord::Message::Activity
readonly
The activity of the message.
-
#application_id ⇒ RubyCord::Application
readonly
The application of the message.
-
#attachments ⇒ Array<RubyCord::Attachment>
readonly
The attachments of the message.
-
#author ⇒ RubyCord::User, ...
readonly
The user that sent the message.
- #channel ⇒ nil, RubyCord::Channel readonly
-
#components ⇒ Array<Array<RubyCord::Component>>
readonly
The components of the message.
-
#content ⇒ String
(also: #to_s)
readonly
The content of the message.
-
#created_at ⇒ Time
(also: #timestamp, #sent_at)
readonly
The time the message was created.
-
#deleted ⇒ Boolean
(also: #deleted?)
readonly
Whether the message is deleted.
-
#dm? ⇒ Boolean
readonly
Whether the message was sent in a DM.
-
#edited? ⇒ Boolean
readonly
Whether the message was edited.
- #embed ⇒ RubyCord::Embed? readonly
- #embed? ⇒ Boolean readonly
-
#embeds ⇒ Array<RubyCord::Embed>
readonly
The embeds of the message.
-
#flag ⇒ RubyCord::Message::Flag
readonly
The flag of the message.
- #guild ⇒ nil, RubyCord::Guild readonly
-
#guild? ⇒ Boolean
readonly
Whether the message was sent in a guild.
-
#id ⇒ RubyCord::Snowflake
readonly
The ID of the message.
-
#interaction ⇒ RubyCord::Message::Interaction
readonly
The interaction of the message.
-
#jump_url ⇒ String
readonly
The URL to jump to the message.
-
#mention_everyone ⇒ Boolean
(also: #mention_everyone?)
readonly
Whether the message mentions everyone.
-
#message_reference ⇒ RubyCord::Message::Reference
readonly
The reference of the message.
-
#pinned ⇒ Boolean
(also: #pinned?)
readonly
Whether the message is pinned.
-
#reactions ⇒ Array<RubyCord::Reaction>
readonly
The reactions of the message.
-
#reply? ⇒ Boolean
readonly
Whether the message is a reply.
-
#stickers ⇒ RubyCord::Message::Sticker
readonly
The sticker of the message.
-
#thread ⇒ RubyCord::Guild::ThreadChannel
readonly
The thread channel of the message.
-
#tts ⇒ Boolean
(also: #tts?)
readonly
Whether the message is tts.
-
#type ⇒ Symbol
readonly
Currently, this will be one of:.
- #updated_at ⇒ Time? (also: #edited_at, #edited_timestamp) readonly
-
#webhook? ⇒ Boolean
readonly
Whether the message was sent by a webhook.
-
#webhook_id ⇒ RubyCord::Snowflake
readonly
The ID of the channel the message was sent in.
Instance Method Summary collapse
-
#add_reaction(emoji) ⇒ Async::Task<void>
(also: #react_with)
Add a reaction to the message.
-
#clean_content(user: true, channel: true, role: true, emoji: true, everyone: true, codeblock: false) ⇒ String
Removes the mentions from the message.
-
#delete(reason: nil) ⇒ Async::Task<void>
Delete the message.
-
#edit(content = RubyCord::Unset, embed: RubyCord::Unset, embeds: RubyCord::Unset, allowed_mentions: RubyCord::Unset, attachments: RubyCord::Unset, components: RubyCord::Unset, supress: RubyCord::Unset) ⇒ Async::Task<void>
Edit the message.
-
#fetch_reacted_users(emoji, limit: nil, after: RubyCord::Snowflake.new("0")) ⇒ Async::Task<Array<RubyCord::User>>
Fetch reacted users of reaction.
-
#inspect ⇒ Object
Meta.
-
#pin(reason: nil) ⇒ Async::Task<void>
Pin the message.
-
#publish ⇒ Async::Task<void>
Publish the message.
-
#remove_reaction(emoji) ⇒ Async::Task<void>
(also: #delete_reaction)
Remove a reaction from the message.
-
#remove_reaction_of(emoji, member) ⇒ Async::Task<void>
(also: #delete_reaction_of)
Remove other member's reaction from the message.
-
#reply(*args, **kwargs) ⇒ Async::Task<RubyCord::Message>
Reply to the message.
-
#start_thread(*args, **kwargs) ⇒ Async::Task<RubyCord::Guild::ThreadChannel>
(also: #create_thread)
Start thread from the message.
-
#to_reference(fail_if_not_exists: true) ⇒ RubyCord::Message::Reference
Convert the message to reference object.
-
#unpin(reason: nil) ⇒ Async::Task<void>
Unpin the message.
Methods inherited from DiscordModel
Instance Attribute Details
#activity ⇒ RubyCord::Message::Activity (readonly)
Returns The activity of the message.
61 62 63 |
# File 'lib/rubycord/message.rb', line 61 def activity @activity end |
#application_id ⇒ RubyCord::Application (readonly)
Returns The application of the message.
63 64 65 |
# File 'lib/rubycord/message.rb', line 63 def application_id @application_id end |
#attachments ⇒ Array<RubyCord::Attachment> (readonly)
Returns The attachments of the message.
26 27 28 |
# File 'lib/rubycord/message.rb', line 26 def @attachments end |
#author ⇒ RubyCord::User, ... (readonly)
Returns The user that sent the message.
12 13 14 |
# File 'lib/rubycord/message.rb', line 12 def @author end |
#channel ⇒ nil, RubyCord::Channel (readonly)
This method returns an object from client cache. it will return nil
if the object is not in cache.
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#components ⇒ Array<Array<RubyCord::Component>> (readonly)
Returns The components of the message.
76 77 78 |
# File 'lib/rubycord/message.rb', line 76 def components @components end |
#content ⇒ String (readonly) Also known as: to_s
Returns The content of the message.
14 15 16 |
# File 'lib/rubycord/message.rb', line 14 def content @content end |
#created_at ⇒ Time (readonly) Also known as: timestamp, sent_at
Returns The time the message was created.
17 18 19 |
# File 'lib/rubycord/message.rb', line 17 def created_at @created_at end |
#deleted ⇒ Boolean (readonly) Also known as: deleted?
Returns Whether the message is deleted.
78 79 80 |
# File 'lib/rubycord/message.rb', line 78 def deleted @deleted end |
#dm? ⇒ Boolean (readonly)
Returns Whether the message was sent in a DM.
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#edited? ⇒ Boolean (readonly)
Returns Whether the message was edited.
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#embed ⇒ RubyCord::Embed? (readonly)
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#embed? ⇒ Boolean (readonly)
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#embeds ⇒ Array<RubyCord::Embed> (readonly)
Returns The embeds of the message.
28 29 30 |
# File 'lib/rubycord/message.rb', line 28 def @embeds end |
#flag ⇒ RubyCord::Message::Flag (readonly)
Returns The flag of the message.
68 69 70 |
# File 'lib/rubycord/message.rb', line 68 def flag @flag end |
#guild ⇒ nil, RubyCord::Guild (readonly)
This method returns an object from client cache. it will return nil
if the object is not in cache.
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#guild? ⇒ Boolean (readonly)
Returns Whether the message was sent in a guild.
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#id ⇒ RubyCord::Snowflake (readonly)
Returns The ID of the message.
10 11 12 |
# File 'lib/rubycord/message.rb', line 10 def id @id end |
#interaction ⇒ RubyCord::Message::Interaction (readonly)
Returns The interaction of the message.
72 73 74 |
# File 'lib/rubycord/message.rb', line 72 def interaction @interaction end |
#jump_url ⇒ String (readonly)
Returns The URL to jump to the message.
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#mention_everyone ⇒ Boolean (readonly) Also known as: mention_everyone?
Returns Whether the message mentions everyone.
84 85 86 |
# File 'lib/rubycord/message.rb', line 84 def mention_everyone @mention_everyone end |
#message_reference ⇒ RubyCord::Message::Reference (readonly)
Returns The reference of the message.
65 66 67 |
# File 'lib/rubycord/message.rb', line 65 def @message_reference end |
#pinned ⇒ Boolean (readonly) Also known as: pinned?
Returns Whether the message is pinned.
87 88 89 |
# File 'lib/rubycord/message.rb', line 87 def pinned @pinned end |
#reactions ⇒ Array<RubyCord::Reaction> (readonly)
Returns The reactions of the message.
30 31 32 |
# File 'lib/rubycord/message.rb', line 30 def reactions @reactions end |
#reply? ⇒ Boolean (readonly)
Returns Whether the message is a reply.
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#stickers ⇒ RubyCord::Message::Sticker (readonly)
Returns The sticker of the message.
70 71 72 |
# File 'lib/rubycord/message.rb', line 70 def stickers @stickers end |
#thread ⇒ RubyCord::Guild::ThreadChannel (readonly)
Returns The thread channel of the message.
74 75 76 |
# File 'lib/rubycord/message.rb', line 74 def thread @thread end |
#tts ⇒ Boolean (readonly) Also known as: tts?
Returns Whether the message is tts.
81 82 83 |
# File 'lib/rubycord/message.rb', line 81 def tts @tts end |
#type ⇒ Symbol (readonly)
Currently, this will be one of:
:default
:recipient_add
:recipient_remove
:call
:channel_name_change
:channel_icon_change
:channel_pinned_message
:guild_member_join
:user_premium_guild_subscription
:user_premium_guild_subscription_tier_1
:user_premium_guild_subscription_tier_2
:user_premium_guild_subscription_tier_3
:channel_follow_add
:guild_discovery_disqualified
:guild_discovery_requalified
:guild_discovery_grace_period_initial_warning
:guild_discovery_grace_period_final_warning
:thread_created
:reply
:chat_input_command
:thread_starter_message
:guild_invite_reminder
:context_menu_command
59 60 61 |
# File 'lib/rubycord/message.rb', line 59 def type @type end |
#updated_at ⇒ Time? (readonly) Also known as: edited_at, edited_timestamp
22 23 24 |
# File 'lib/rubycord/message.rb', line 22 def updated_at @updated_at end |
#webhook? ⇒ Boolean (readonly)
Returns Whether the message was sent by a webhook.
142 143 144 |
# File 'lib/rubycord/message.rb', line 142 def @embeds.any? end |
#webhook_id ⇒ RubyCord::Snowflake (readonly)
Returns The ID of the channel the message was sent in.
32 33 34 |
# File 'lib/rubycord/message.rb', line 32 def webhook_id @webhook_id end |
Instance Method Details
#add_reaction(emoji) ⇒ Async::Task<void> Also known as: react_with
Add a reaction to the message.
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/rubycord/message.rb', line 356 def add_reaction(emoji) Async do @client .http .request( RubyCord::Internal::Route.new( "/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", "//channels/:channel_id/messages/:message_id/reactions/:emoji/@me", :put ), nil ) .wait end end |
#clean_content(user: true, channel: true, role: true, emoji: true, everyone: true, codeblock: false) ⇒ String
Removes the mentions from the message.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/rubycord/message.rb', line 206 def clean_content( user: true, channel: true, role: true, emoji: true, everyone: true, codeblock: false ) ret = @content.dup if user ret.gsub!(/<@!?(\d+)>/) do |_match| member = guild&.members&.[](Regexp.last_match(1)) member ||= @client.users[Regexp.last_match(1)] member ? "@#{member.name}" : "@Unknown User" end end ret.gsub!(/<#(\d+)>/) do |_match| channel = @client.channels[Regexp.last_match(1)] channel ? "<##{channel.id}>" : "#Unknown Channel" end if role ret.gsub!(/<@&(\d+)>/) do |_match| r = guild&.roles&.[](Regexp.last_match(1)) r ? "@#{r.name}" : "@Unknown Role" end end if emoji ret.gsub!(/<a?:([a-zA-Z0-9_]+):\d+>/) { |_match| Regexp.last_match(1) } end ret.gsub!(/@(everyone|here)/, "@\u200b\\1") if everyone if codeblock ret else codeblocks = ret.split("```", -1) original_codeblocks = @content.scan(/```(.+?)```/m) res = [] max = codeblocks.length codeblocks.each_with_index do |single_codeblock, i| res << if (max.even? && i == max - 1) || i.even? single_codeblock else original_codeblocks[i / 2] end end res.join("```") end end |
#delete(reason: nil) ⇒ Async::Task<void>
Delete the message.
299 300 301 |
# File 'lib/rubycord/message.rb', line 299 def delete(reason: nil) Async { channel.(@id, reason:).wait } end |
#edit(content = RubyCord::Unset, embed: RubyCord::Unset, embeds: RubyCord::Unset, allowed_mentions: RubyCord::Unset, attachments: RubyCord::Unset, components: RubyCord::Unset, supress: RubyCord::Unset) ⇒ Async::Task<void>
Edit the message.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/rubycord/message.rb', line 268 def edit( content = RubyCord::Unset, embed: RubyCord::Unset, embeds: RubyCord::Unset, allowed_mentions: RubyCord::Unset, attachments: RubyCord::Unset, components: RubyCord::Unset, supress: RubyCord::Unset ) Async do channel.( @id, content, embed:, embeds:, allowed_mentions:, attachments:, components:, supress: ).wait end end |
#fetch_reacted_users(emoji, limit: nil, after: RubyCord::Snowflake.new("0")) ⇒ Async::Task<Array<RubyCord::User>>
Fetch reacted users of reaction.
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/rubycord/message.rb', line 437 def fetch_reacted_users( emoji, limit: nil, after: RubyCord::Snowflake.new("0") ) Async do if limit.nil? || !limit.positive? after = RubyCord::Snowflake.new("0") users = [] loop do _resp, data = @client .http .request( RubyCord::Internal::Route.new( "/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=100&after=#{after}", "//channels/:channel_id/messages/:message_id/reactions/:emoji", :get ) ) .wait break if data.empty? users += data.map do |r| guild&.members&.[](r[:id]) || @client.users[r[:id]] || User.new(@client, r) end break if data.length < 100 after = data[-1][:id] end next users else _resp, data = @client .http .request( RubyCord::Internal::Route.new( "/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=#{limit}&after=#{after}", "//channels/:channel_id/messages/:message_id/reactions/:emoji", :get ) ) .wait next( data.map do |r| guild&.members&.[](r[:id]) || @client.users[r[:id]] || User.new(@client, r) end ) end end end |
#inspect ⇒ Object
Meta
533 534 535 |
# File 'lib/rubycord/message.rb', line 533 def inspect "#<#{self.class} #{@content.inspect} id=#{@id}>" end |
#pin(reason: nil) ⇒ Async::Task<void>
Pin the message.
501 502 503 |
# File 'lib/rubycord/message.rb', line 501 def pin(reason: nil) Async { channel.(self, reason:).wait } end |
#publish ⇒ Async::Task<void>
Publish the message.
339 340 341 342 343 344 345 346 |
# File 'lib/rubycord/message.rb', line 339 def publish Async do channel.post( "/channels/#{@channel_id}/messages/#{@id}/crosspost", nil ).wait end end |
#remove_reaction(emoji) ⇒ Async::Task<void> Also known as: delete_reaction
Remove a reaction from the message.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/rubycord/message.rb', line 382 def remove_reaction(emoji) Async do @client .http .request( RubyCord::Internal::Route.new( "/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", "//channels/:channel_id/messages/:message_id/reactions/:emoji/@me", :delete ) ) .wait end end |
#remove_reaction_of(emoji, member) ⇒ Async::Task<void> Also known as: delete_reaction_of
Remove other member's reaction from the message.
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/rubycord/message.rb', line 408 def remove_reaction_of(emoji, member) Async do @client .http .request( RubyCord::Internal::Route.new( "/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/#{ member.is_a?(Member) ? member.id : member }", "//channels/:channel_id/messages/:message_id/reactions/:emoji/:user_id", :delete ) ) .wait end end |
#reply(*args, **kwargs) ⇒ Async::Task<RubyCord::Message>
Reply to the message.
329 330 331 |
# File 'lib/rubycord/message.rb', line 329 def reply(*args, **kwargs) Async { channel.post(*args, reference: self, **kwargs).wait } end |
#start_thread(*args, **kwargs) ⇒ Async::Task<RubyCord::Guild::ThreadChannel> Also known as: create_thread
Start thread from the message.
525 526 527 |
# File 'lib/rubycord/message.rb', line 525 def start_thread(*args, **kwargs) Async { channel.start_thread(*args, message: self, **kwargs).wait } end |
#to_reference(fail_if_not_exists: true) ⇒ RubyCord::Message::Reference
Convert the message to reference object.
310 311 312 313 314 315 316 317 318 319 |
# File 'lib/rubycord/message.rb', line 310 def to_reference(fail_if_not_exists: true) Reference.from_hash( { message_id: @id, channel_id: @channel_id, guild_id: @guild_id, fail_if_not_exists: } ) end |
#unpin(reason: nil) ⇒ Async::Task<void>
Unpin the message.
513 514 515 |
# File 'lib/rubycord/message.rb', line 513 def unpin(reason: nil) Async { channel.(self, reason:).wait } end |