class Discorb::Message
Represents a message.
Attributes
@!visibility private
@return [Discorb::Message::Activity] The activity of the message.
@return [Discorb::Application] The application of the message.
@return [Array<Discorb::Attachment>] The attachments of the message.
@return [Array<Array<Discorb::Components>>] The components of the message.
@return [String] The content of the message.
@return [Time] The time the message was created.
@return [Boolean] Whether the message is deleted.
@return [Boolean] Whether the message is deleted.
@return [Time] The time the message was edited. @return [nil] If the message was not edited.
@return [Time] The time the message was edited. @return [nil] If the message was not edited.
@return [Array<Discorb::Embed>] The embeds of the message.
@return [Discorb::Message::Flag] The flag of the message. @see Discorb::Message::Flag
@return [Discorb::Snowflake] The ID of the message.
@return [Discorb::Message::Interaction] The interaction of the message.
@return [Boolean] Whether the message mentions everyone.
@return [Boolean] Whether the message mentions everyone.
@return [Discorb::Message::Reference] The reference of the message.
@return [Boolean] Whether the message is pinned.
@return [Boolean] Whether the message is pinned.
@return [Array<Discorb::Reaction>] The reactions of the message.
@return [Time] The time the message was created.
@return [Discorb::Message::Sticker] The sticker of the message.
@return [Discorb::ThreadChannel] The thread channel of the message.
@return [Time] The time the message was created.
@return [String] The content of the message.
@return [Boolean] Whether the message is tts.
@return [Boolean] Whether the message is tts.
@return [Symbol] The type of the message. 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`
-
`:application_command`
-
`:thread_starter_message`
-
`:guild_invite_reminder`
@return [Time] The time the message was edited. @return [nil] If the message was not edited.
@return [Discorb::Snowflake] The ID of the channel the message was sent in.
Public Class Methods
@!visibility private
# File lib/discorb/message.rb, line 188 def initialize(client, data, no_cache: false) @client = client @data = {} @no_cache = no_cache _set_data(data) @client.messages[@id] = self unless @no_cache end
Public Instance Methods
Add a reaction to the message. @macro async @macro http
@param [Discorb::Emoji] emoji The emoji to react with.
# File lib/discorb/message.rb, line 294 def add_reaction(emoji) Async do @client.http.put("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", nil).wait end end
# File lib/discorb/message.rb, line 196 def channel @dm || @client.channels[@channel_id] end
Delete the message.
@param [String] reason The reason for deleting the message.
# File lib/discorb/message.rb, line 239 def delete!(reason: nil) Async do channel.delete_message!(@id, reason: reason).wait end end
Edit the message.
@param [String] content The message content. @param [Discorb::Embed] embed The embed to send. @param [Array<Discord::Embed>] embeds The embeds to send. @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions. @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send. @param [Boolean] supress Whether to supress embeds.
# File lib/discorb/message.rb, line 226 def edit(message_id, content = nil, embed: nil, embeds: nil, allowed_mentions: nil, components: nil, supress: nil) Async do channel.edit_message(@id, message_id, content, embed: embed, embeds: embeds, allowed_mentions: allowed_mentions, components: components, supress: supress).wait end end
# File lib/discorb/message.rb, line 212 def edited? !@updated_at.nil? end
# File lib/discorb/message.rb, line 261 def embed @embeds[0] end
Fetch reacted users of reaction. @macro async @macro http
@param [Discorb::Emoji] emoji The emoji to fetch. @param [Integer, nil] limit The maximum number of users to fetch. `nil` for no limit. @param [Discorb::Snowflake, nil] after The ID of the user to start fetching from.
@return [Array<Discorb::User>] The users.
# File lib/discorb/message.rb, line 344 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
# File lib/discorb/message.rb, line 200 def guild @client.guilds[@guild_id] end
Meta
# File lib/discorb/message.rb, line 408 def inspect "#<#{self.class} #{@content.inspect} id=#{@id}>" end
# File lib/discorb/message.rb, line 208 def jump_url "https://discord.com/channels/#{@guild_id || "@me"}/#{@channel_id}/#{@id}" end
Pin the message. @macro async @macro http
@param [String] reason The reason for pinning the message.
# File lib/discorb/message.rb, line 374 def pin(reason: nil) Async do channel.pin_message(self, reason: reason).wait end end
Publish the message. @macro async @macro http
# File lib/discorb/message.rb, line 281 def publish Async do channel.post("/channels/#{@channel_id}/messages/#{@id}/crosspost", nil).wait end end
Remove a reaction from the message. @macro async @macro http
@param [Discorb::Emoji] emoji The emoji to remove.
# File lib/discorb/message.rb, line 309 def remove_reaction(emoji) Async do @client.http.delete("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me").wait end end
Remove other member's reaction from the message. @macro async @macro http
@param [Discorb::Emoji] emoji The emoji to remove. @param [Discorb::Member] member The member to remove the reaction from.
# File lib/discorb/message.rb, line 325 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 to the message. @macro async @macro http @param (see post) @return [Discorb::Message] The message.
# File lib/discorb/message.rb, line 270 def reply(*args, **kwargs) Async do channel.post(*args, reference: self, **kwargs).wait end end
Start thread from the message.
@param (see Discorb::Channel#start_thread)
@return [<Type>] <description>
# File lib/discorb/message.rb, line 398 def start_thread(*args, **kwargs) Async do channel.start_thread(*args, message: self, **kwargs).wait end end
Convert the message to reference object.
@param [Boolean] fail_if_not_exists Whether to raise an error if the message does not exist.
@return [Hash] The reference object.
# File lib/discorb/message.rb, line 252 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 the message. @macro async @macro http
# File lib/discorb/message.rb, line 385 def unpin Async do channel.unpin_message(self, reason: reason).wait end end
# File lib/discorb/message.rb, line 204 def webhook? @webhook_id != nil end
Private Instance Methods
# File lib/discorb/message.rb, line 510 def _set_data(data) @id = Snowflake.new(data[:id]) @channel_id = data[:channel_id] if data[:guild_id] @guild_id = data[:guild_id] @dm = nil else @dm = Discorb::DMChannel.new(@client, data[:channel_id]) @guild_id = nil end if data[:member].nil? && data[:webhook_id] @webhook_id = Snowflake.new(data[:webhook_id]) @author = Webhook::Message::Author.new(data[:author]) elsif data[:guild_id].nil? || data[:guild_id].empty? @author = @client.users[data[:author][:id]] || User.new(@client, data[:author]) else @author = guild.members[data[:author][:id]] || Member.new(@client, @guild_id, data[:author], data[:member]) end @content = data[:content] @created_at = Time.iso8601(data[:timestamp]) @updated_at = data[:edited_timestamp].nil? ? nil : Time.iso8601(data[:edited_timestamp]) @tts = data[:tts] @mention_everyone = data[:mention_everyone] @mention_roles = data[:mention_roles].map { |r| guild.roles[r] } @attachments = data[:attachments].map { |a| Attachment.new(a) } @embeds = data[:embeds] ? data[:embeds].map { |e| Embed.new(data: e) } : [] @reactions = data[:reactions] ? data[:reactions].map { |r| Reaction.new(self, r) } : [] @pinned = data[:pinned] @type = self.class.message_type[data[:type]] @activity = data[:activity] && Activity.new(data[:activity]) @application_id = data[:application_id] @message_reference = data[:message_reference] && Reference.from_hash(data[:message_reference]) @flag = Flag.new(0b111 - data[:flags]) @sticker_items = data[:sticker_items] ? data[:sticker_items].map { |s| Message::Sticker.new(s) } : [] # @referenced_message = data[:referenced_message] && Message.new(@client, data[:referenced_message]) @interaction = data[:interaction] && Message::Interaction.new(@client, data[:interaction]) @thread = data[:thread] && Channel.make_channel(@client, data[:thread]) @components = data[:components].map { |c| c[:components].map { |co| Component.from_hash(co) } } @data.update(data) @deleted = false end