Class: Discorb::Message
- Inherits:
-
DiscordModel
- Object
- DiscordModel
- Discorb::Message
- Defined in:
- lib/discorb/message.rb
Overview
Represents a message.
Direct Known Subclasses
Defined Under Namespace
Classes: Activity, Flag, Interaction, Reference, Sticker
Instance Attribute Summary collapse
-
#activity -> Discorb::Message::Activity
readonly
The activity of the message.
-
#application_id -> Discorb::Application
readonly
The application of the message.
-
#attachments -> Array<Discorb::Attachment>
readonly
The attachments of the message.
-
#author -> Discorb::User, Discorb::Member
readonly
The user that sent the message.
- #channel -> nil, Discorb::Channel readonly
-
#components -> Array<Array<Discorb::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 -> Discorb::Embed? readonly
- #embed? -> Boolean readonly
-
#embeds -> Array<Discorb::Embed>
readonly
The embeds of the message.
- #guild -> nil, Discorb::Guild readonly
-
#guild? -> Boolean
readonly
Whether the message was sent in a guild.
-
#id -> Discorb::Snowflake
readonly
The ID of the message.
-
#interaction -> Discorb::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 -> Discorb::Message::Reference
readonly
The reference of the message.
-
#pinned -> Boolean
(also: #pinned?)
readonly
Whether the message is pinned.
-
#reactions -> Array<Discorb::Reaction>
readonly
The reactions of the message.
-
#reply? -> Boolean
readonly
Whether the message is a reply.
-
#stickers -> Discorb::Message::Sticker
readonly
The sticker of the message.
-
#thread -> Discorb::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 -> Discorb::Snowflake
readonly
The ID of the channel the message was sent in.
Instance Method Summary collapse
-
#add_reaction(emoji) -> Object
(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) -> Object
Delete the message.
-
#edit(content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) -> Object
Edit the message.
-
#fetch_reacted_users(emoji, limit: nil, after: 0) -> Async::Task<Array<Discorb::User>>
Fetch reacted users of reaction.
-
#flag -> Discorb::Message::Flag
The flag of the message.
-
#inspect -> Object
Meta.
-
#pin(reason: nil) -> Object
Pin the message.
-
#publish -> Object
Publish the message.
-
#remove_reaction(emoji) -> Object
(also: #delete_reaction)
Remove a reaction from the message.
-
#remove_reaction_of(emoji, member) -> Object
(also: #delete_reaction_of)
Remove other member's reaction from the message.
-
#reply(*args, **kwargs) -> Async::Task<Discorb::Message>
Reply to the message.
-
#start_thread(*args, **kwargs) -> Async::Task<<Type>>
(also: #create_thread)
Start thread from the message.
-
#to_reference(fail_if_not_exists: true) -> Hash
Convert the message to reference object.
-
#unpin(reason: nil) -> Object
Unpin the message.
Methods inherited from DiscordModel
Instance Attribute Details
#activity -> Discorb::Message::Activity (readonly)
Returns The activity of the message.
118 119 120 |
# File 'lib/discorb/message.rb', line 118 def activity @activity end |
#application_id -> Discorb::Application (readonly)
Returns The application of the message.
120 121 122 |
# File 'lib/discorb/message.rb', line 120 def application_id @application_id end |
#attachments -> Array<Discorb::Attachment> (readonly)
Returns The attachments of the message.
83 84 85 |
# File 'lib/discorb/message.rb', line 83 def @attachments end |
#author -> Discorb::User, Discorb::Member (readonly)
Returns The user that sent the message.
69 70 71 |
# File 'lib/discorb/message.rb', line 69 def @author end |
#channel -> nil, Discorb::Channel (readonly)
This method returns an object from client cache. it will return nil
if the object is not in cache.
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#components -> Array<Array<Discorb::Component>> (readonly)
Returns The components of the message.
133 134 135 |
# File 'lib/discorb/message.rb', line 133 def components @components end |
#content -> String (readonly) Also known as: to_s
Returns The content of the message.
71 72 73 |
# File 'lib/discorb/message.rb', line 71 def content @content end |
#created_at -> Time (readonly) Also known as: timestamp, sent_at
Returns The time the message was created.
74 75 76 |
# File 'lib/discorb/message.rb', line 74 def created_at @created_at end |
#deleted -> Boolean (readonly) Also known as: deleted?
Returns Whether the message is deleted.
135 136 137 |
# File 'lib/discorb/message.rb', line 135 def deleted @deleted end |
#dm? -> Boolean (readonly)
Returns Whether the message was sent in a DM.
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#edited? -> Boolean (readonly)
Returns Whether the message was edited.
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#embed -> Discorb::Embed? (readonly)
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#embed? -> Boolean (readonly)
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#embeds -> Array<Discorb::Embed> (readonly)
Returns The embeds of the message.
85 86 87 |
# File 'lib/discorb/message.rb', line 85 def @embeds end |
#guild -> nil, Discorb::Guild (readonly)
This method returns an object from client cache. it will return nil
if the object is not in cache.
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#guild? -> Boolean (readonly)
Returns Whether the message was sent in a guild.
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#id -> Discorb::Snowflake (readonly)
Returns The ID of the message.
67 68 69 |
# File 'lib/discorb/message.rb', line 67 def id @id end |
#interaction -> Discorb::Message::Interaction (readonly)
Returns The interaction of the message.
129 130 131 |
# File 'lib/discorb/message.rb', line 129 def interaction @interaction end |
#jump_url -> String (readonly)
Returns The URL to jump to the message.
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#mention_everyone -> Boolean (readonly) Also known as: mention_everyone?
Returns Whether the message mentions everyone.
141 142 143 |
# File 'lib/discorb/message.rb', line 141 def mention_everyone @mention_everyone end |
#message_reference -> Discorb::Message::Reference (readonly)
Returns The reference of the message.
122 123 124 |
# File 'lib/discorb/message.rb', line 122 def @message_reference end |
#pinned -> Boolean (readonly) Also known as: pinned?
Returns Whether the message is pinned.
144 145 146 |
# File 'lib/discorb/message.rb', line 144 def pinned @pinned end |
#reactions -> Array<Discorb::Reaction> (readonly)
Returns The reactions of the message.
87 88 89 |
# File 'lib/discorb/message.rb', line 87 def reactions @reactions end |
#reply? -> Boolean (readonly)
Returns Whether the message is a reply.
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#stickers -> Discorb::Message::Sticker (readonly)
Returns The sticker of the message.
127 128 129 |
# File 'lib/discorb/message.rb', line 127 def stickers @stickers end |
#thread -> Discorb::ThreadChannel (readonly)
Returns The thread channel of the message.
131 132 133 |
# File 'lib/discorb/message.rb', line 131 def thread @thread end |
#tts -> Boolean (readonly) Also known as: tts?
Returns Whether the message is tts.
138 139 140 |
# File 'lib/discorb/message.rb', line 138 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
116 117 118 |
# File 'lib/discorb/message.rb', line 116 def type @type end |
#updated_at -> Time? (readonly) Also known as: edited_at, edited_timestamp
79 80 81 |
# File 'lib/discorb/message.rb', line 79 def updated_at @updated_at end |
#webhook? -> Boolean (readonly)
Returns Whether the message was sent by a webhook.
197 198 199 |
# File 'lib/discorb/message.rb', line 197 def @embeds.any? end |
#webhook_id -> Discorb::Snowflake (readonly)
Returns The ID of the channel the message was sent in.
89 90 91 |
# File 'lib/discorb/message.rb', line 89 def webhook_id @webhook_id end |
Instance Method Details
#add_reaction(emoji) -> Object Also known as: react_with
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Add a reaction to the message.
369 370 371 372 373 |
# File 'lib/discorb/message.rb', line 369 def add_reaction(emoji) Async do @client.http.put("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", 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.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/discorb/message.rb', line 254 def clean_content(user: true, channel: true, role: true, emoji: true, everyone: true, codeblock: false) ret = @content.dup ret.gsub!(/<@!?(\d+)>/) do |match| member = guild&.members&.[]($1) member ||= @client.users[$1] member ? "@#{member.name}" : "@Unknown User" end if user ret.gsub!(/<#(\d+)>/) do |match| channel = @client.channels[$1] channel ? "<##{channel.id}>" : "#Unknown Channel" end ret.gsub!(/<@&(\d+)>/) do |match| role = guild&.roles&.[]($1) role ? "@#{role.name}" : "@Unknown Role" end if role ret.gsub!(/<a?:([a-zA-Z0-9_]+):\d+>/) do |match| $1 end if emoji ret.gsub!(/@(everyone|here)/, "@\u200b\\1") if everyone unless codeblock codeblocks = ret.split("```", -1) original_codeblocks = @content.scan(/```(.+?)```/m) res = [] max = codeblocks.length codeblocks.each_with_index do |codeblock, i| if max % 2 == 0 && i == max - 1 or i.even? res << codeblock else res << original_codeblocks[i / 2] end end res.join("```") else ret end end |
#delete!(reason: nil) -> Object
Delete the message.
314 315 316 317 318 |
# File 'lib/discorb/message.rb', line 314 def delete!(reason: nil) Async do channel.(@id, reason: reason).wait end end |
#edit(content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) -> Object
Edit the message.
301 302 303 304 305 306 307 |
# File 'lib/discorb/message.rb', line 301 def edit(content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) Async do channel.(@id, content, embed: , embeds: , allowed_mentions: allowed_mentions, components: components, supress: supress).wait end end |
#fetch_reacted_users(emoji, limit: nil, after: 0) -> Async::Task<Array<Discorb::User>>
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Fetch reacted users of reaction.
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/discorb/message.rb', line 419 def fetch_reacted_users(emoji, limit: nil, after: 0) Async do if limit.nil? || !limit.positive? after = 0 users = [] loop do _resp, data = @client.http.get("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=100&after=#{after}").wait break if data.empty? users += data.map { |r| guild&.members&.[](r[:id]) || @client.users[r[:id]] || User.new(@client, r) } break if data.length < 100 after = data[-1][:id] end next users else _resp, data = @client.http.get("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=#{limit}&after=#{after}").wait next data.map { |r| guild&.members&.[](r[:id]) || @client.users[r[:id]] || User.new(@client, r) } end end end |
#flag -> Discorb::Message::Flag
Returns The flag of the message.
125 126 127 |
# File 'lib/discorb/message.rb', line 125 def flag @flag end |
#inspect -> Object
Meta
485 486 487 |
# File 'lib/discorb/message.rb', line 485 def inspect "#<#{self.class} #{@content.inspect} id=#{@id}>" end |
#pin(reason: nil) -> Object
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Pin the message.
449 450 451 452 453 |
# File 'lib/discorb/message.rb', line 449 def pin(reason: nil) Async do channel.(self, reason: reason).wait end end |
#publish -> Object
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Publish the message.
356 357 358 359 360 |
# File 'lib/discorb/message.rb', line 356 def publish Async do channel.post("/channels/#{@channel_id}/messages/#{@id}/crosspost", nil).wait end end |
#remove_reaction(emoji) -> Object Also known as: delete_reaction
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Remove a reaction from the message.
384 385 386 387 388 |
# File 'lib/discorb/message.rb', line 384 def remove_reaction(emoji) Async do @client.http.delete("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me").wait end end |
#remove_reaction_of(emoji, member) -> Object Also known as: delete_reaction_of
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Remove other member's reaction from the message.
400 401 402 403 404 |
# File 'lib/discorb/message.rb', line 400 def remove_reaction_of(emoji, member) Async do @client.http.delete("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/#{member.is_a?(Member) ? member.id : member}").wait end end |
#reply(*args, **kwargs) -> Async::Task<Discorb::Message>
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Reply to the message.
345 346 347 348 349 |
# File 'lib/discorb/message.rb', line 345 def reply(*args, **kwargs) Async do channel.post(*args, reference: self, **kwargs).wait end end |
#start_thread(*args, **kwargs) -> Async::Task<<Type>> Also known as: create_thread
Start thread from the message.
475 476 477 478 479 |
# File 'lib/discorb/message.rb', line 475 def start_thread(*args, **kwargs) Async do channel.start_thread(*args, message: self, **kwargs).wait end end |
#to_reference(fail_if_not_exists: true) -> Hash
Convert the message to reference object.
327 328 329 330 331 332 333 334 |
# File 'lib/discorb/message.rb', line 327 def to_reference(fail_if_not_exists: true) { message_id: @id, channel_id: @channel_id, guild_id: @guild_id, fail_if_not_exists: fail_if_not_exists, } end |
#unpin(reason: nil) -> Object
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Unpin the message.
462 463 464 465 466 |
# File 'lib/discorb/message.rb', line 462 def unpin(reason: nil) Async do channel.(self, reason: reason).wait end end |