Class: DiscordRDA::Message
- Defined in:
- lib/discord_rda/entity/message.rb
Overview
Represents a Discord message. Messages can contain text, embeds, attachments, and more.
Constant Summary collapse
- TYPES =
Message types
{ default: 0, recipient_add: 1, recipient_remove: 2, call: 3, channel_name_change: 4, channel_icon_change: 5, channel_pinned_message: 6, user_join: 7, guild_boost: 8, guild_boost_tier_1: 9, guild_boost_tier_2: 10, guild_boost_tier_3: 11, channel_follow_add: 12, guild_discovery_disqualified: 14, guild_discovery_requalified: 15, guild_discovery_grace_period_initial_warning: 16, guild_discovery_grace_period_final_warning: 17, thread_created: 18, reply: 19, chat_input_command: 20, thread_starter_message: 21, guild_invite_reminder: 22, context_menu_command: 23, auto_moderation_action: 24, role_subscription_purchase: 25, interaction_premium_upsell: 26, stage_start: 27, stage_end: 28, stage_speaker: 29, stage_topic: 31, guild_application_premium_subscription: 32, guild_incident_alert_mode_enabled: 36, guild_incident_alert_mode_disabled: 37, guild_incident_report_raid: 38, guild_incident_report_false_alarm: 39, purchase_notification: 44, poll_result: 46 }.freeze
Class Attribute Summary collapse
-
.api ⇒ Object
Returns the value of attribute api.
Attributes inherited from Entity
Instance Method Summary collapse
-
#application ⇒ Application?
Get application.
-
#attachments ⇒ Array<Attachment>
Get attachments.
-
#author ⇒ User, Member
Get the author as a User entity.
-
#created_at ⇒ Time
Get creation time.
-
#delete(reason: nil) ⇒ void
Delete this message.
-
#deleted? ⇒ Boolean
Check if message was deleted.
-
#edit(content = nil, **options) {|builder| ... } ⇒ Message
Edit this message.
-
#edited? ⇒ Boolean
Check if message was edited.
-
#edited_at ⇒ Time?
Get edit timestamp.
-
#embeds ⇒ Array<Embed>
Get embeds.
-
#has_attachments? ⇒ Boolean
Check if message has attachments.
-
#has_components? ⇒ Boolean
Check if message has components (buttons, select menus).
-
#has_embeds? ⇒ Boolean
Check if message has embeds.
-
#has_poll? ⇒ Boolean
Check if message has a poll.
-
#has_reactions? ⇒ Boolean
Check if message has reactions.
-
#jump_url ⇒ String
Get the jump URL for this message.
-
#mention_everyone? ⇒ Boolean
Check if message mentions everyone.
-
#mentioned_channels ⇒ Array<Channel>
Get mentioned channels.
-
#mentioned_roles ⇒ Array<Snowflake>
Get mentioned roles as snowflakes.
-
#mentioned_users ⇒ Array<User>
Get mentioned users.
-
#message_flags ⇒ MessageFlags
Get message flags.
-
#message_reference ⇒ Hash?
Get the message reference data.
-
#message_type ⇒ Symbol
Get message type as symbol.
-
#pin(reason: nil) ⇒ void
Pin this message.
-
#pinned? ⇒ Boolean
Check if message is pinned.
-
#position ⇒ Integer?
Get position in thread.
-
#react(emoji) ⇒ void
React to this message with an emoji.
-
#reaction_count ⇒ Integer
Get total reaction count.
-
#reactions ⇒ Array<Reaction>
Get reactions.
-
#referenced_message ⇒ Message?
Get the referenced (replied to) message.
-
#reply? ⇒ Boolean
Check if this is a reply to another message.
-
#resolved_data ⇒ ResolvedData?
Get resolved data for interactions.
-
#respond(content = nil, **options) {|builder| ... } ⇒ Message
Respond to this message (send reply).
-
#stickers ⇒ Array<Sticker>
Get sticker items.
-
#system? ⇒ Boolean
Check if message is a system message.
-
#thread ⇒ Channel?
Get thread associated with this message.
-
#tts? ⇒ Boolean
Check if message is TTS.
-
#unpin(reason: nil) ⇒ void
Unpin this message.
-
#webhook? ⇒ Boolean
Check if message is from a webhook.
Methods inherited from Entity
#==, attribute, from_hash, #hash, #initialize, #inspect, #to_h, #to_json
Constructor Details
This class inherits a constructor from DiscordRDA::Entity
Class Attribute Details
.api ⇒ Object
Returns the value of attribute api.
14 15 16 |
# File 'lib/discord_rda/entity/message.rb', line 14 def api @api end |
Instance Method Details
#application ⇒ Application?
Get application
283 284 285 286 287 |
# File 'lib/discord_rda/entity/message.rb', line 283 def application return nil unless @raw_data['application'] Application.new(@raw_data['application']) end |
#attachments ⇒ Array<Attachment>
Get attachments
131 132 133 |
# File 'lib/discord_rda/entity/message.rb', line 131 def (@raw_data['attachments'] || []).map { |a| Attachment.new(a) } end |
#author ⇒ User, Member
Get the author as a User entity
101 102 103 104 105 106 107 108 109 |
# File 'lib/discord_rda/entity/message.rb', line 101 def return nil unless @raw_data['author'] if @raw_data['member'] Member.new(@raw_data['author'].merge('member' => @raw_data['member'], 'guild_id' => @raw_data['guild_id'])) else User.new(@raw_data['author']) end end |
#created_at ⇒ Time
Get creation time
401 402 403 |
# File 'lib/discord_rda/entity/message.rb', line 401 def created_at end |
#delete(reason: nil) ⇒ void
This method returns an undefined value.
Delete this message
345 346 347 348 349 350 351 352 |
# File 'lib/discord_rda/entity/message.rb', line 345 def delete(reason: nil) raise 'API client not configured. Call Bot#initialize first.' unless self.class.api headers = {} headers['X-Audit-Log-Reason'] = CGI.escape(reason) if reason self.class.api.delete("/channels/#{channel_id}/messages/#{id}", headers: headers) end |
#deleted? ⇒ Boolean
Check if message was deleted
245 246 247 |
# File 'lib/discord_rda/entity/message.rb', line 245 def deleted? @raw_data['deleted'] || false end |
#edit(content = nil, **options) {|builder| ... } ⇒ Message
Edit this message
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/discord_rda/entity/message.rb', line 383 def edit(content = nil, **, &block) raise 'API client not configured. Call Bot#initialize first.' unless self.class.api payload = { content: content }.merge().compact # Execute builder block if given if block builder = MessageBuilder.new(payload) block.call(builder) payload = builder.to_h end data = self.class.api.patch("/channels/#{channel_id}/messages/#{id}", body: payload) Message.new(data) end |
#edited? ⇒ Boolean
Check if message was edited
167 168 169 |
# File 'lib/discord_rda/entity/message.rb', line 167 def edited? !.nil? end |
#edited_at ⇒ Time?
Get edit timestamp
173 174 175 |
# File 'lib/discord_rda/entity/message.rb', line 173 def edited_at end |
#embeds ⇒ Array<Embed>
Get embeds
137 138 139 |
# File 'lib/discord_rda/entity/message.rb', line 137 def (@raw_data['embeds'] || []).map { |e| Embed.new(e) } end |
#has_attachments? ⇒ Boolean
Check if message has attachments
221 222 223 |
# File 'lib/discord_rda/entity/message.rb', line 221 def .any? end |
#has_components? ⇒ Boolean
Check if message has components (buttons, select menus)
251 252 253 |
# File 'lib/discord_rda/entity/message.rb', line 251 def has_components? components.any? end |
#has_embeds? ⇒ Boolean
Check if message has embeds
215 216 217 |
# File 'lib/discord_rda/entity/message.rb', line 215 def .any? end |
#has_poll? ⇒ Boolean
Check if message has a poll
257 258 259 |
# File 'lib/discord_rda/entity/message.rb', line 257 def has_poll? !poll.nil? end |
#has_reactions? ⇒ Boolean
Check if message has reactions
227 228 229 |
# File 'lib/discord_rda/entity/message.rb', line 227 def has_reactions? reactions.any? end |
#jump_url ⇒ String
Get the jump URL for this message
198 199 200 201 202 203 204 205 |
# File 'lib/discord_rda/entity/message.rb', line 198 def jump_url guild_id = @raw_data['guild_id'] if guild_id "https://discord.com/channels/#{guild_id}/#{channel_id}/#{id}" else "https://discord.com/channels/@me/#{channel_id}/#{id}" end end |
#mention_everyone? ⇒ Boolean
Check if message mentions everyone
155 156 157 |
# File 'lib/discord_rda/entity/message.rb', line 155 def mention_everyone? mention_everyone end |
#mentioned_channels ⇒ Array<Channel>
Get mentioned channels
125 126 127 |
# File 'lib/discord_rda/entity/message.rb', line 125 def mentioned_channels (@raw_data['mention_channels'] || []).map { |c| Channel.new(c) } end |
#mentioned_roles ⇒ Array<Snowflake>
Get mentioned roles as snowflakes
119 120 121 |
# File 'lib/discord_rda/entity/message.rb', line 119 def mentioned_roles (@raw_data['mention_roles'] || []).map { |r| Snowflake.new(r) } end |
#mentioned_users ⇒ Array<User>
Get mentioned users
113 114 115 |
# File 'lib/discord_rda/entity/message.rb', line 113 def mentioned_users (@raw_data['mentions'] || []).map { |m| User.new(m) } end |
#message_flags ⇒ MessageFlags
Get message flags
269 270 271 |
# File 'lib/discord_rda/entity/message.rb', line 269 def MessageFlags.new(flags) end |
#message_reference ⇒ Hash?
Get the message reference data
192 193 194 |
# File 'lib/discord_rda/entity/message.rb', line 192 def @raw_data['message_reference'] end |
#message_type ⇒ Symbol
Get message type as symbol
95 96 97 |
# File 'lib/discord_rda/entity/message.rb', line 95 def TYPES.key(type) || :unknown end |
#pin(reason: nil) ⇒ void
This method returns an undefined value.
Pin this message
357 358 359 360 361 362 363 364 |
# File 'lib/discord_rda/entity/message.rb', line 357 def pin(reason: nil) raise 'API client not configured. Call Bot#initialize first.' unless self.class.api headers = {} headers['X-Audit-Log-Reason'] = CGI.escape(reason) if reason self.class.api.put("/channels/#{channel_id}/pins/#{id}", headers: headers) end |
#pinned? ⇒ Boolean
Check if message is pinned
161 162 163 |
# File 'lib/discord_rda/entity/message.rb', line 161 def pinned? pinned end |
#position ⇒ Integer?
Get position in thread
299 300 301 |
# File 'lib/discord_rda/entity/message.rb', line 299 def position @raw_data['position'] end |
#react(emoji) ⇒ void
This method returns an undefined value.
React to this message with an emoji
333 334 335 336 337 338 339 340 |
# File 'lib/discord_rda/entity/message.rb', line 333 def react(emoji) raise 'API client not configured. Call Bot#initialize first.' unless self.class.api emoji_string = emoji.respond_to?(:id) ? "#{emoji.name}:#{emoji.id}" : emoji.to_s emoji_encoded = CGI.escape(emoji_string) self.class.api.put("/channels/#{channel_id}/messages/#{id}/reactions/#{emoji_encoded}/@me") end |
#reaction_count ⇒ Integer
Get total reaction count
233 234 235 |
# File 'lib/discord_rda/entity/message.rb', line 233 def reaction_count reactions.sum(&:count) end |
#reactions ⇒ Array<Reaction>
Get reactions
143 144 145 |
# File 'lib/discord_rda/entity/message.rb', line 143 def reactions (@raw_data['reactions'] || []).map { |r| Reaction.new(r) } end |
#referenced_message ⇒ Message?
Get the referenced (replied to) message
185 186 187 188 |
# File 'lib/discord_rda/entity/message.rb', line 185 def ref = @raw_data['referenced_message'] Message.new(ref) if ref end |
#reply? ⇒ Boolean
Check if this is a reply to another message
179 180 181 |
# File 'lib/discord_rda/entity/message.rb', line 179 def reply? type == 19 || !.nil? end |
#resolved_data ⇒ ResolvedData?
Get resolved data for interactions
291 292 293 294 295 |
# File 'lib/discord_rda/entity/message.rb', line 291 def resolved_data return nil unless @raw_data['resolved'] ResolvedData.new(@raw_data['resolved']) end |
#respond(content = nil, **options) {|builder| ... } ⇒ Message
Respond to this message (send reply)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/discord_rda/entity/message.rb', line 308 def respond(content = nil, **, &block) raise 'API client not configured. Call Bot#initialize first.' unless self.class.api payload = { content: content }.merge().compact payload[:message_reference] = { message_id: id.to_s, channel_id: channel_id.to_s, guild_id: @raw_data['guild_id'], fail_if_not_exists: false } # Execute builder block if given if block builder = MessageBuilder.new(payload) block.call(builder) payload = builder.to_h end data = self.class.api.post("/channels/#{channel_id}/messages", body: payload) Message.new(data) end |
#stickers ⇒ Array<Sticker>
Get sticker items
263 264 265 |
# File 'lib/discord_rda/entity/message.rb', line 263 def stickers (@raw_data['sticker_items'] || @raw_data['stickers'] || []).map { |s| Sticker.new(s) } end |
#system? ⇒ Boolean
Check if message is a system message
239 240 241 |
# File 'lib/discord_rda/entity/message.rb', line 239 def system? type != 0 && type != 19 && type != 20 && type != 23 end |
#thread ⇒ Channel?
Get thread associated with this message
275 276 277 278 279 |
# File 'lib/discord_rda/entity/message.rb', line 275 def thread return nil unless @raw_data['thread'] Channel.new(@raw_data['thread']) end |
#tts? ⇒ Boolean
Check if message is TTS
149 150 151 |
# File 'lib/discord_rda/entity/message.rb', line 149 def tts? tts end |
#unpin(reason: nil) ⇒ void
This method returns an undefined value.
Unpin this message
369 370 371 372 373 374 375 376 |
# File 'lib/discord_rda/entity/message.rb', line 369 def unpin(reason: nil) raise 'API client not configured. Call Bot#initialize first.' unless self.class.api headers = {} headers['X-Audit-Log-Reason'] = CGI.escape(reason) if reason self.class.api.delete("/channels/#{channel_id}/pins/#{id}", headers: headers) end |
#webhook? ⇒ Boolean
Check if message is from a webhook
209 210 211 |
# File 'lib/discord_rda/entity/message.rb', line 209 def webhook? !webhook_id.nil? end |