Class: DiscordRDA::Message

Inherits:
Entity
  • Object
show all
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

Attributes inherited from Entity

#id

Instance Method Summary collapse

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

.apiObject

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

#applicationApplication?

Get application

Returns:

  • (Application, nil)

    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

#attachmentsArray<Attachment>

Get attachments

Returns:



131
132
133
# File 'lib/discord_rda/entity/message.rb', line 131

def attachments
  (@raw_data['attachments'] || []).map { |a| Attachment.new(a) }
end

#authorUser, Member

Get the author as a User entity

Returns:



101
102
103
104
105
106
107
108
109
# File 'lib/discord_rda/entity/message.rb', line 101

def author
  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_atTime

Get creation time

Returns:

  • (Time)

    Message creation time



401
402
403
# File 'lib/discord_rda/entity/message.rb', line 401

def created_at
  timestamp
end

#delete(reason: nil) ⇒ void

This method returns an undefined value.

Delete this message

Parameters:

  • reason (String) (defaults to: nil)

    Audit log reason



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

Returns:

  • (Boolean)

    True if deleted (not present in data)



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

Parameters:

  • content (String) (defaults to: nil)

    New content

  • options (Hash)

    Additional options (embeds, components, etc.)

Yield Parameters:

  • builder (MessageBuilder)

    Optional builder block

Returns:



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, **options, &block)
  raise 'API client not configured. Call Bot#initialize first.' unless self.class.api

  payload = { content: content }.merge(options).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

Returns:

  • (Boolean)

    True if edited



167
168
169
# File 'lib/discord_rda/entity/message.rb', line 167

def edited?
  !edited_timestamp.nil?
end

#edited_atTime?

Get edit timestamp

Returns:

  • (Time, nil)

    Edit time



173
174
175
# File 'lib/discord_rda/entity/message.rb', line 173

def edited_at
  edited_timestamp
end

#embedsArray<Embed>

Get embeds

Returns:

  • (Array<Embed>)

    Embeds



137
138
139
# File 'lib/discord_rda/entity/message.rb', line 137

def embeds
  (@raw_data['embeds'] || []).map { |e| Embed.new(e) }
end

#has_attachments?Boolean

Check if message has attachments

Returns:

  • (Boolean)

    True if has attachments



221
222
223
# File 'lib/discord_rda/entity/message.rb', line 221

def has_attachments?
  attachments.any?
end

#has_components?Boolean

Check if message has components (buttons, select menus)

Returns:

  • (Boolean)

    True if has components



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

Returns:

  • (Boolean)

    True if has embeds



215
216
217
# File 'lib/discord_rda/entity/message.rb', line 215

def has_embeds?
  embeds.any?
end

#has_poll?Boolean

Check if message has a poll

Returns:

  • (Boolean)

    True if has 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

Returns:

  • (Boolean)

    True if has reactions



227
228
229
# File 'lib/discord_rda/entity/message.rb', line 227

def has_reactions?
  reactions.any?
end

#jump_urlString

Get the jump URL for this message

Returns:

  • (String)

    Jump URL



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

Returns:

  • (Boolean)

    True if mentions everyone



155
156
157
# File 'lib/discord_rda/entity/message.rb', line 155

def mention_everyone?
  mention_everyone
end

#mentioned_channelsArray<Channel>

Get mentioned channels

Returns:

  • (Array<Channel>)

    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_rolesArray<Snowflake>

Get mentioned roles as snowflakes

Returns:



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_usersArray<User>

Get mentioned users

Returns:

  • (Array<User>)

    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_flagsMessageFlags

Get message flags

Returns:



269
270
271
# File 'lib/discord_rda/entity/message.rb', line 269

def message_flags
  MessageFlags.new(flags)
end

#message_referenceHash?

Get the message reference data

Returns:

  • (Hash, nil)

    Message reference



192
193
194
# File 'lib/discord_rda/entity/message.rb', line 192

def message_reference
  @raw_data['message_reference']
end

#message_typeSymbol

Get message type as symbol

Returns:

  • (Symbol)

    Message type



95
96
97
# File 'lib/discord_rda/entity/message.rb', line 95

def message_type
  TYPES.key(type) || :unknown
end

#pin(reason: nil) ⇒ void

This method returns an undefined value.

Pin this message

Parameters:

  • reason (String) (defaults to: nil)

    Audit log reason



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

Returns:

  • (Boolean)

    True if pinned



161
162
163
# File 'lib/discord_rda/entity/message.rb', line 161

def pinned?
  pinned
end

#positionInteger?

Get position in thread

Returns:

  • (Integer, nil)

    Position



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

Parameters:

  • emoji (String, Emoji)

    Emoji to react with (can be unicode emoji or emoji ID string)



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_countInteger

Get total reaction count

Returns:

  • (Integer)

    Total reactions



233
234
235
# File 'lib/discord_rda/entity/message.rb', line 233

def reaction_count
  reactions.sum(&:count)
end

#reactionsArray<Reaction>

Get reactions

Returns:

  • (Array<Reaction>)

    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_messageMessage?

Get the referenced (replied to) message

Returns:

  • (Message, nil)

    Referenced message



185
186
187
188
# File 'lib/discord_rda/entity/message.rb', line 185

def referenced_message
  ref = @raw_data['referenced_message']
  Message.new(ref) if ref
end

#reply?Boolean

Check if this is a reply to another message

Returns:

  • (Boolean)

    True if reply



179
180
181
# File 'lib/discord_rda/entity/message.rb', line 179

def reply?
  type == 19 || !message_reference.nil?
end

#resolved_dataResolvedData?

Get resolved data for interactions

Returns:



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)

Parameters:

  • content (String) (defaults to: nil)

    Message content

  • options (Hash)

    Additional options (embeds, components, etc.)

Yield Parameters:

  • builder (MessageBuilder)

    Optional builder block

Returns:



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, **options, &block)
  raise 'API client not configured. Call Bot#initialize first.' unless self.class.api

  payload = { content: content }.merge(options).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

#stickersArray<Sticker>

Get sticker items

Returns:

  • (Array<Sticker>)

    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

Returns:

  • (Boolean)

    True if 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

#threadChannel?

Get thread associated with this message

Returns:

  • (Channel, nil)

    Thread if created from 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

Returns:

  • (Boolean)

    True if 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

Parameters:

  • reason (String) (defaults to: nil)

    Audit log reason



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

Returns:

  • (Boolean)

    True if webhook message



209
210
211
# File 'lib/discord_rda/entity/message.rb', line 209

def webhook?
  !webhook_id.nil?
end