Class Message

java.lang.Object
discord4j.core.object.entity.Message
All Implemented Interfaces:
DiscordObject, Entity

public final class Message extends Object implements Entity
A Discord message.

Message Object

  • Field Details

    • MAX_CONTENT_LENGTH

      public static final int MAX_CONTENT_LENGTH
      The maximum amount of characters that can be in the contents of a message.
      See Also:
    • MAX_TOTAL_EMBEDS_CHARACTER_LENGTH

      public static final int MAX_TOTAL_EMBEDS_CHARACTER_LENGTH
      The maximum amount of characters that can be present when combining all title, description, field.name, field.value, footer.text, and author.name fields of all embeds for this message.
      See Also:
  • Constructor Details

    • Message

      public Message(GatewayDiscordClient gateway, discord4j.discordjson.json.MessageData data)
      Constructs a Message with an associated GatewayDiscordClient and Discord data.
      Parameters:
      gateway - The GatewayDiscordClient associated to this object, must be non-null.
      data - The raw data as represented by Discord, must be non-null.
  • Method Details

    • getClient

      public GatewayDiscordClient getClient()
      Description copied from interface: DiscordObject
      Returns the GatewayDiscordClient that created this object. Methods in it are exclusively based on the entity cache or Store in use. Refer to calling getClient().rest() to access a DiscordClient that is capable of requesting entities directly from the REST API.
      Specified by:
      getClient in interface DiscordObject
      Returns:
      The GatewayDiscordClient associated to this object.
    • getId

      public Snowflake getId()
      Description copied from interface: Entity
      Gets the Snowflake that uniquely identifies this entity.
      Specified by:
      getId in interface Entity
      Returns:
      The Snowflake that uniquely identifies this entity.
    • getData

      public discord4j.discordjson.json.MessageData getData()
      Gets the data of the message.
      Returns:
      The data of the message.
    • getRestMessage

      public RestMessage getRestMessage()
      Returns:
      A RestMessage handle to execute REST API operations on this entity.
    • getRestChannel

      public RestChannel getRestChannel()
      Returns:
      A RestChannel handle to execute REST API operations on the channel of this message.
    • getChannelId

      public Snowflake getChannelId()
      Gets the ID of the channel the message was sent in.
      Returns:
      The ID of the channel the message was sent in.
    • getChannel

      public Mono<MessageChannel> getChannel()
      Requests to retrieve the channel the message was sent in.
      Returns:
      A Mono where, upon successful completion, emits the channel the message was sent in. If an error is received, it is emitted through the Mono.
    • getChannel

      public Mono<MessageChannel> getChannel(EntityRetrievalStrategy retrievalStrategy)
      Requests to retrieve the channel the message was sent in, using the given retrieval strategy.
      Parameters:
      retrievalStrategy - the strategy to use to get the channel
      Returns:
      A Mono where, upon successful completion, emits the channel the message was sent in. If an error is received, it is emitted through the Mono.
    • getWebhookId

      public Optional<Snowflake> getWebhookId()
      Gets the ID the webhook that generated this message, if present.
      Returns:
      The ID of the webhook that generated this message, if present.
    • getAuthor

      public Optional<User> getAuthor()
      Gets the author of this message, if present.
      Returns:
      The author of this message, if present.
    • getAuthorAsMember

      public Mono<Member> getAuthorAsMember()
      Requests to retrieve the author of this message as a member of the guild in which it was sent.
      Returns:
      A Mono where, upon successful completion, emits the author of this message as a member of the guild in which it was sent, if present. If an error is received, it is emitted through the Mono.
    • getUserData

      @Experimental public discord4j.discordjson.json.UserData getUserData()
      Gets the raw author data of this message.
      Returns:
      The raw author data of this message.
    • getContent

      public String getContent()
      Gets the contents of the message, if present.
      Returns:
      The contents of the message, if present.
    • getTimestamp

      public Instant getTimestamp()
      Gets when this message was sent.
      Returns:
      When this message was sent.
    • getEditedTimestamp

      public Optional<Instant> getEditedTimestamp()
      Gets when this message was edited, if present.
      Returns:
      When this message was edited, if present.
    • isTts

      public boolean isTts()
      Gets whether this was a TTS (Text-To-Speech) message.
      Returns:
      true if this message was a TTS (Text-To-Speech) message, false otherwise.
    • mentionsEveryone

      public boolean mentionsEveryone()
      Gets whether this message mentions everyone.
      Returns:
      true if this message mentions everyone, false otherwise.
    • getUserMentionIds

      public List<Snowflake> getUserMentionIds()
      Gets the IDs of the users specifically mentioned in this message, without duplication and with the same order as in the message.
      Returns:
      The IDs of the users specifically mentioned in this message, without duplication and with the same order as in the message.
    • getMemberMentions

      public List<PartialMember> getMemberMentions()
      Gets the partial members specifically mentioned in this message, without duplication and with the same order as in the message.
      Returns:
      The partial members specifically mentioned in this message, without duplication and with the same order as in the message.
    • getUserMentions

      public List<User> getUserMentions()
      Gets the users specifically mentioned in this message, without duplication and with the same order as in the message.
      Returns:
      The users specifically mentioned in this message, without duplication and with the same order as in the message.
    • getRoleMentionIds

      public List<Snowflake> getRoleMentionIds()
      Gets the IDs of the roles specifically mentioned in this message, without duplication and with the same order as in the message.
      Returns:
      The IDs of the roles specifically mentioned in this message, without duplication and with the same order as in the message.
    • getRoleMentions

      public Flux<Role> getRoleMentions()
      Requests to retrieve the roles specifically mentioned in this message.
      Returns:
      A Flux that continually emits roles specifically mentioned in this message. If an error is received, it is emitted through the Flux.
    • getRoleMentions

      public Flux<Role> getRoleMentions(EntityRetrievalStrategy retrievalStrategy)
      Requests to retrieve the roles specifically mentioned in this message, using the given retrieval strategy.
      Parameters:
      retrievalStrategy - the strategy to use to get the roles
      Returns:
      A Flux that continually emits roles specifically mentioned in this message. If an error is received, it is emitted through the Flux.
    • getAttachments

      public List<Attachment> getAttachments()
      Gets any attached files, with the same order as in the message.
      Returns:
      Any attached files, with the same order as in the message.
    • getEmbeds

      public List<Embed> getEmbeds()
      Gets any embedded content.
      Returns:
      Any embedded content.
    • getReactions

      public List<Reaction> getReactions()
      Gets the reactions to this message, the order is the same as in the message.
      Returns:
      The reactions to this message, the order is the same as in the message.
    • getReactors

      public Flux<User> getReactors(ReactionEmoji emoji)
      Requests to retrieve the reactors (users) for the specified emoji for this message.
      Parameters:
      emoji - The emoji to get the reactors (users) for this message.
      Returns:
      A Flux that continually emits the reactors for the specified emoji for this message. If an error is received, it is emitted through the Flux.
    • isPinned

      public boolean isPinned()
      Gets whether this message is pinned.
      Returns:
      true if this message is pinned, false otherwise.
    • getWebhook

      public Mono<Webhook> getWebhook()
      Requests to retrieve the webhook that generated this message, if present.
      Returns:
      A Mono where, upon successful completion, emits the webhook that generated this message, if present. If an error is received, it is emitted through the Mono.
    • getMessageReference

      public Optional<MessageReference> getMessageReference()
      Returns the MessageReference sent with crossposted messages and replies, if present.
      Returns:
      The MessageReference sent with crossposted messages and replies, if present.
    • getFlags

      public EnumSet<Message.Flag> getFlags()
      Returns the flags of this Message, describing its features.
      Returns:
      A EnumSet with the flags of this message.
    • getGuildId

      public Optional<Snowflake> getGuildId()
      Gets the ID of the guild this message is associated to, if this Message was built from Gateway data, like an incoming event. If requested from REST API, this field will be empty.
      Returns:
      The ID of the guild this message is associated to, if present.
    • getGuild

      public Mono<Guild> getGuild()
      Requests to retrieve the guild this message is associated to, if present.
      Returns:
      A Mono where, upon successful completion, emits the Guild this message is associated to, if present. If an error is received, it is emitted through the Mono.
    • getGuild

      public Mono<Guild> getGuild(EntityRetrievalStrategy retrievalStrategy)
      Requests to retrieve the guild this message is associated to, if present, using the given retrieval strategy.
      Parameters:
      retrievalStrategy - the strategy to use to get the guild
      Returns:
      A Mono where, upon successful completion, emits the Guild this message is associated to, if present. If an error is received, it is emitted through the Mono.
    • getType

      public Message.Type getType()
      Gets the type of message.
      Returns:
      The type of message.
    • getStickersItems

      @Experimental public List<PartialSticker> getStickersItems()
      Gets the partial stickers sent with the message.
      Returns:
      The partial stickers sent with the message.
    • getStickers

      @Deprecated public List<Sticker> getStickers()
      Deprecated.
      Gets the stickers sent with the message.
      Returns:
      The stickers sent with the message.
    • getReferencedMessage

      public Optional<Message> getReferencedMessage()
      Returns the message associated with the MessageReference, if present.
      Returns:
      The message associated with the MessageReference, if present.
    • getInteraction

      public Optional<MessageInteraction> getInteraction()
      Gets the interaction data, if the message is a response to an Interactions.
      Returns:
      The interaction data, if the message is a response to an Interactions.
    • getComponents

      public List<LayoutComponent> getComponents()
      Gets the components on the message.
      Returns:
      The components on the message.
    • edit

      @Deprecated public Mono<Message> edit(Consumer<? super LegacyMessageEditSpec> spec)
      Deprecated.
      use edit(MessageEditSpec) or edit() which offer an immutable approach to build specs
      Requests to edit this message.
      Parameters:
      spec - A Consumer that provides a "blank" LegacyMessageEditSpec to be operated on.
      Returns:
      A Mono where, upon successful completion, emits the edited Message. If an error is received, it is emitted through the Mono.
    • edit

      public Mono<Message> edit(discord4j.core.spec.MessageEditSpec spec)
      Requests to edit this message.

      To partially or completely replace attachments, see the docs for edit() for examples and adapt them to a standalone spec.

      Parameters:
      spec - an immutable object that specifies how to edit the message
      Returns:
      A Mono where, upon successful completion, emits the edited Message. If an error is received, it is emitted through the Mono.
    • edit

      public discord4j.core.spec.MessageEditMono edit()
      Requests to edit this message. Properties specifying how to edit this message can be set via the withXxx methods of the returned MessageEditMono.

      By default, this method will append any file added through withFiles. To replace or remove individual attachments, use withAttachments along with Attachment objects from the original message you want to keep. It is not required to include the new files as Attachment objects.

      For example, to replace all previous attachments, provide an empty withAttachments and your files:

      
        message.edit()
           .withContentOrNull("Replaced all attachments")
           .withFiles(getFile())
           .withComponents(row)
           .withAttachments();
       

      To replace a specific attachment, you need to pass the attachment details you want to keep. You could work from the original getAttachments() list and pass it to withAttachments and your files. The following example removes only the first attachment:

      
        message.edit()
               .withContentOrNull("Replaced the first attachment")
               .withFiles(getFile())
               .withComponents(row)
               .withAttachmentsOrNull(message.getAttachments()
                       .stream()
                       .skip(1)
                       .collect(Collectors.toList()));
       

      To clear all attachments, provide an empty withAttachments:

      
        message.edit()
           .withContentOrNull("Removed all attachments")
           .withComponents(row)
           .withAttachments();
       
      Returns:
      A MessageEditMono where, upon successful completion, emits the edited Message. If an error is received, it is emitted through the MessageEditMono.
      See Also:
    • delete

      public Mono<Void> delete()
      Requests to delete this message.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the message has been deleted. If an error is received, it is emitted through the Mono.
    • delete

      public Mono<Void> delete(@Nullable String reason)
      Requests to delete this message while optionally specifying a reason.
      Parameters:
      reason - The reason, if present.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the message has been deleted. If an error is received, it is emitted through the Mono.
    • suppressEmbeds

      @Experimental public Mono<Void> suppressEmbeds(boolean suppress)
      Requests to suppress all embeds in this message. If the message have the embeds suppressed then this action can undo the suppressed embeds.
      Parameters:
      suppress - Determine if you need suppress or not the embeds.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the process has been completed. If an error is received, it is emitted through the Mono.
    • addReaction

      public Mono<Void> addReaction(ReactionEmoji emoji)
      Requests to add a reaction on this message.
      Parameters:
      emoji - The reaction to add on this message.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the reaction was added on this message. If an error is received, it is emitted through the Mono.
    • removeReaction

      public Mono<Void> removeReaction(ReactionEmoji emoji, Snowflake userId)
      Requests to remove a reaction from a specified user on this message.
      Parameters:
      emoji - The reaction to remove on this message.
      userId - The user to remove the reaction on this message.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the reaction from the specified user was removed on this message. If an error is received, it is emitted through the Mono.
    • removeReactions

      public Mono<Void> removeReactions(ReactionEmoji emoji)
      Requests to remove all reactions of a specific emoji on this message.
      Parameters:
      emoji - The reaction to remove on this message
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the reaction from the specified user was removed on this message. If an error is received, it is emitted through the Mono.
    • removeSelfReaction

      public Mono<Void> removeSelfReaction(ReactionEmoji emoji)
      Requests to remove a reaction from the current user on this message.
      Parameters:
      emoji - The reaction to remove on this message.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the reaction from the current user was removed on this message. If an error is received, it is emitted through the Mono.
    • removeAllReactions

      public Mono<Void> removeAllReactions()
      Requests to remove all the reactions on this message.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating all the reactions on this message were removed. If an error is received, it is emitted through the Mono.
    • pin

      public Mono<Void> pin()
      Requests to pin this message.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the messaged was pinned. If an error is received, it is emitted through the Mono.
    • unpin

      public Mono<Void> unpin()
      Requests to unpin this message.
      Returns:
      A Mono where, upon successful completion, emits nothing; indicating the message was unpinned. If an error is received, it is emitted through the Mono.
    • publish

      public Mono<Message> publish()
      Requests to publish (crosspost) this message if the channel is of type 'news'. Requires 'SEND_MESSAGES' permission if the current user sent the message, or additionally the 'MANAGE_MESSAGES' permission, for all other messages, to be present for the current user.
      Returns:
      A Mono where, upon successful completion, emits the published Message in the guilds. If an error is received, it is emitted through the Mono.
    • startThread

      public Mono<ThreadChannel> startThread(discord4j.core.spec.StartThreadSpec spec)
      Creates a new thread from an existing message.
      Parameters:
      spec - an immutable object that specifies how to create the thread
      Returns:
      A Mono where, upon successful completion, emits the created ThreadChannel. If an error is received, it is emitted through the Mono.
    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object