class Discordrb::Message
A message on Discord that was sent to a text channel
Constants
- ZERO_DISCRIM
The discriminator that webhook user accounts have.
Attributes
@return [Array<Attachment>] the files attached to this message.
@return [Channel] the channel in which this message was sent.
@return [String] the content of this message.
@return [Time] the timestamp at which this message was edited. `nil` if the message was never edited.
@return [true, false] whether the message was edited or not.
@return [true, false] whether the message was edited or not.
@return [Time] the timestamp at which this message was edited. `nil` if the message was never edited.
@return [Array<Embed>] the embed objects contained in this message.
@return [true, false] whether the message mentioned everyone or not.
@return [true, false] whether the message mentioned everyone or not.
@return [Array<User>] the users that were mentioned in this message.
@return [true, false] whether the message mentioned everyone or not.
@return [String] used for validating a message was sent.
@return [true, false] whether the message is pinned or not.
@return [true, false] whether the message is pinned or not.
@return [Hash<String => Reaction>] the reaction objects attached to this message keyed by the name of the reaction
@return [Array<Role>] the roles that were mentioned in this message.
@return [String] the content of this message.
@return [Time] the timestamp at which this message was sent.
@return [String] the content of this message.
@return [true, false] whether the message used Text-To-Speech (TTS) or not.
@return [true, false] whether the message used Text-To-Speech (TTS) or not.
@return [Member, User] the user that sent this message. (Will be a {Member} most of the time, it should only be a
{User} for old messages when the author has left the server since then)
@return [Integer, nil] the webhook ID that sent this message, or `nil` if it wasn't sent through a webhook.
@return [Member, User] the user that sent this message. (Will be a {Member} most of the time, it should only be a
{User} for old messages when the author has left the server since then)
Public Class Methods
@!visibility private
# File lib/discordrb/data.rb, line 2461 def initialize(data, bot) @bot = bot @content = data['content'] @channel = bot.channel(data['channel_id'].to_i) @pinned = data['pinned'] @tts = data['tts'] @nonce = data['nonce'] @mention_everyone = data['mention_everyone'] @author = if data['author'] if data['author']['discriminator'] == ZERO_DISCRIM # This is a webhook user! It would be pointless to try to resolve a member here, so we just create # a User and return that instead. Discordrb::LOGGER.debug("Webhook user: #{data['author']['id']}") User.new(data['author'], @bot) elsif @channel.private? # Turn the message user into a recipient - we can't use the channel recipient # directly because the bot may also send messages to the channel Recipient.new(bot.user(data['author']['id'].to_i), @channel, bot) else member = @channel.server.member(data['author']['id'].to_i) unless member Discordrb::LOGGER.debug("Member with ID #{data['author']['id']} not cached (possibly left the server).") member = @bot.ensure_user(data['author']) end member end end @webhook_id = data['webhook_id'].to_i if data['webhook_id'] @timestamp = Time.parse(data['timestamp']) if data['timestamp'] @edited_timestamp = data['edited_timestamp'].nil? ? nil : Time.parse(data['edited_timestamp']) @edited = !@edited_timestamp.nil? @id = data['id'].to_i @emoji = [] @reactions = {} data['reactions']&.each do |element| @reactions[element['emoji']['name']] = Reaction.new(element) end @mentions = [] data['mentions']&.each do |element| @mentions << bot.ensure_user(element) end @role_mentions = [] # Role mentions can only happen on public servers so make sure we only parse them there if @channel.text? data['mention_roles']&.each do |element| @role_mentions << @channel.server.role(element.to_i) end end @attachments = [] @attachments = data['attachments'].map { |e| Attachment.new(e, self, @bot) } if data['attachments'] @embeds = [] @embeds = data['embeds'].map { |e| Embed.new(e, self) } if data['embeds'] end
Public Instance Methods
Add an {Await} for a message with the same user and channel. @see Bot#add_await
@deprecated Will be changed to blocking behavior in v4.0. Use {#await!} instead.
# File lib/discordrb/data.rb, line 2568 def await(key, attributes = {}, &block) @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @author.id, in: @channel.id }.merge(attributes), &block) end
Add a blocking {Await} for a message with the same user and channel. @see Bot#add_await!
# File lib/discordrb/data.rb, line 2574 def await!(attributes = {}) @bot.add_await!(Discordrb::Events::MessageEvent, { from: @author.id, in: @channel.id }.merge(attributes)) end
Reacts to a message. @param reaction [String, to_reaction] the unicode emoji or {Emoji}
# File lib/discordrb/data.rb, line 2628 def create_reaction(reaction) reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction) API::Channel.create_reaction(@bot.token, @channel.id, @id, reaction) nil end
Deletes this message.
# File lib/discordrb/data.rb, line 2546 def delete API::Channel.delete_message(@bot.token, @channel.id, @id) nil end
Removes all reactions from this message.
# File lib/discordrb/data.rb, line 2663 def delete_all_reactions API::Channel.delete_all_reactions(@bot.token, @channel.id, @id) end
Deletes this client's reaction on this message. @param reaction [String, to_reaction] the reaction to remove
# File lib/discordrb/data.rb, line 2657 def delete_own_reaction(reaction) reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction) API::Channel.delete_own_reaction(@bot.token, @channel.id, @id, reaction) end
Deletes a reaction made by a user on this message. @param user [User, resolve_id
] the user who used this reaction @param reaction [String, to_reaction] the reaction to remove
# File lib/discordrb/data.rb, line 2650 def delete_reaction(user, reaction) reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction) API::Channel.delete_user_reaction(@bot.token, @channel.id, @id, reaction, user.resolve_id) end
Edits this message to have the specified content instead. You can only edit your own messages. @param new_content [String] the new content the message should have. @param new_embed [Hash, Discordrb::Webhooks::Embed, nil] The new embed the message should have. If `nil` the message will be changed to have no embed. @return [Message] the resulting message.
# File lib/discordrb/data.rb, line 2540 def edit(new_content, new_embed = nil) response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content, [], new_embed ? new_embed.to_hash : nil) Message.new(JSON.parse(response), @bot) end
@return [Array<Emoji>] the emotes that were used/mentioned in this message.
# File lib/discordrb/data.rb, line 2597 def emoji return if @content.nil? emoji = scan_for_emoji emoji.each do |element| @emoji << @bot.parse_mention(element) end @emoji end
Check if any emoji were used in this message. @return [true, false] whether or not any emoji were used
# File lib/discordrb/data.rb, line 2609 def emoji? emoji = scan_for_emoji return true unless emoji.empty? end
@return [true, false] whether this message was sent by the current {Bot}.
# File lib/discordrb/data.rb, line 2579 def from_bot? @author&.current_bot? end
The inspect method is overwritten to give more useful output
# File lib/discordrb/data.rb, line 2668 def inspect "<Message content=\"#{@content}\" id=#{@id} timestamp=#{@timestamp} author=#{@author} channel=#{@channel}>" end
Returns the reactions made by the current bot or user. @return [Array<Reaction>] the reactions
# File lib/discordrb/data.rb, line 2622 def my_reactions @reactions.values.select(&:me) end
Pins this message
# File lib/discordrb/data.rb, line 2552 def pin API::Channel.pin_message(@bot.token, @channel.id, @id) @pinned = true nil end
Returns the list of users who reacted with a certain reaction. @param reaction [String, to_reaction] the unicode emoji or {Emoji} @example Get all the users that reacted with a thumbsup.
thumbs_up_reactions = message.reacted_with("\u{1F44D}")
@return [Array<User>] the users who used this reaction
# File lib/discordrb/data.rb, line 2641 def reacted_with(reaction) reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction) response = JSON.parse(API::Channel.get_reactions(@bot.token, @channel.id, @id, reaction)) response.map { |d| User.new(d, @bot) } end
Check if any reactions were used in this message. @return [true, false] whether or not this message has reactions
# File lib/discordrb/data.rb, line 2616 def reactions? @reactions.any? end
Replies to this message with the specified content. @see Channel#send_message
# File lib/discordrb/data.rb, line 2531 def reply(content) @channel.send_message(content) end
@!visibility private @return [Array<String>] the emoji mentions found in the message
# File lib/discordrb/data.rb, line 2590 def scan_for_emoji emoji = @content.split emoji = emoji.grep(/<(?<animated>a)?:(?<name>\w+):(?<id>\d+)>?/) emoji end
Unpins this message
# File lib/discordrb/data.rb, line 2559 def unpin API::Channel.unpin_message(@bot.token, @channel.id, @id) @pinned = false nil end
@return [true, false] whether this message has been sent over a webhook.
# File lib/discordrb/data.rb, line 2584 def webhook? !@webhook_id.nil? end