Interface Message

  • All Superinterfaces:
    java.util.Formattable, ISnowflake

    public interface Message
    extends ISnowflake, java.util.Formattable
    Represents a Text message received from Discord.
    This represents messages received from MessageChannels.

    This type is not updated. JDA does not keep track of changes to messages, it is advised to do this via events such as MessageUpdateEvent and similar.

    Message Differences

    There are 3 implementations of this interface in JDA.
    1. Received Message
      Messages received through events or history query. These messages hold information of existing messages and can be modified or deleted.
    2. System Message
      Specification of Received Messages that are generated by Discord on certain events. Commonly this is used in groups or to indicate a pin within a MessageChannel. The different types can be found in the MessageType enum.
    3. Data Message
      This type is produced by MessageBuilder and only holds sendable information such as content or nonce. These messages do not allow any modifications via RestActions or information that is generated when sent such as the id to be used.

    When a feature is not available it will throw an UnsupportedOperationException as per interface specifications.
    Specific operations may have specified information available in the throws javadoc.

    Formattable

    This interface extends Formattable and can be used with a Formatter such as used by String.format(String, Object...) or PrintStream.printf(String, Object...).

    This will use getContentDisplay() rather than Object.toString()!
    Supported Features:

    • Alternative
      - Using getContentRaw() (Example: %#s - uses getContentDisplay())
    • Width/Left-Justification
      - Ensures the size of a format (Example: %20s - uses at minimum 20 chars; %-10s - uses left-justified padding)
    • Precision
      - Cuts the content to the specified size (replacing last 3 chars with ...; Example: %.20s)

    More information on formatting syntax can be found in the format syntax documentation!

    • Method Detail

      • getMentionedUsers

        java.util.List<User> getMentionedUsers()
        An immutable list of all mentioned Users.
        If no user was mentioned, this list is empty.
        Returns:
        immutable list of mentioned users
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getMentionedChannels

        java.util.List<TextChannel> getMentionedChannels()
        A immutable list of all mentioned TextChannels.
        If none were mentioned, this list is empty.

        This may include TextChannels from other Guilds

        Returns:
        immutable list of mentioned TextChannels
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getMentionedRoles

        java.util.List<Role> getMentionedRoles()
        A immutable list of all mentioned Roles.
        If none were mentioned, this list is empty.

        This may include Roles from other Guilds

        Returns:
        immutable list of mentioned Roles
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getMentionedMembers

        java.util.List<Member> getMentionedMembers​(Guild guild)
        Creates an immutable list of Members representing the users of getMentionedUsers() in the specified Guild.
        This is only a convenience method and will skip all users that are not in the specified Guild.
        Parameters:
        guild - Non-null Guild that will be used to retrieve Members.
        Returns:
        Immutable list of mentioned Members
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
        java.lang.IllegalArgumentException - If the specified Guild is null
        Since:
        3.4.0
      • getMentionedMembers

        java.util.List<Member> getMentionedMembers()
        Creates an immutable list of Members representing the users of getMentionedUsers() in the Guild this Message was sent in.
        This is only a convenience method and will skip all users that are not in the specified Guild.
        It will provide the getGuild() output Guild to getMentionedMembers(Guild).
        Returns:
        Immutable list of mentioned Members
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
        java.lang.IllegalStateException - If this message was not sent in a TextChannel
        Since:
        3.4.0
      • getMentions

        java.util.List<IMentionable> getMentions​(Message.MentionType... types)
        Combines all instances of IMentionable filtered by the specified MentionType values.
        This does not include getMentionedMembers() to avoid duplicates.

        If no MentionType values are given this will fallback to all types.

        Parameters:
        types - Amount of MentionTypes to include in the list of mentions
        Returns:
        Immutable list of filtered IMentionable instances
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
        java.lang.IllegalArgumentException - If provided with null
        Since:
        3.4.0
      • mentionsEveryone

        boolean mentionsEveryone()
        Indicates if this Message mentions everyone using @everyone or @here.
        Returns:
        True, if message is mentioning everyone
      • isEdited

        boolean isEdited()
        Returns whether or not this Message has been edited before.
        Returns:
        True if this message has been edited.
      • getEditedTime

        java.time.OffsetDateTime getEditedTime()
        Provides the OffsetDateTime defining when this Message was last edited. If this Message has not been edited (isEdited() is false), then this method will return null.
        Returns:
        Time of the most recent edit, or null if the Message has never been edited.
      • getAuthor

        User getAuthor()
        The author of this Message
        Returns:
        Message author
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getMember

        Member getMember()
        Returns the author of this Message as a member.
        This is just a shortcut to getGuild().getMember(getAuthor()).
        This is only valid if the Message was actually sent in a TextChannel. This will return null if it was not sent from a TextChannel.
        You can check the type of channel this message was sent from using isFromType(ChannelType) or getChannelType().
        Returns:
        Message author, or null if the message was not sent from a TextChannel.
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getJumpUrl

        java.lang.String getJumpUrl()
        Returns the jump-to URL for the received message. Clicking this URL in the Discord client will cause the client to jump to the specified message.
        Returns:
        A String representing the jump-to URL for the message
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getContentDisplay

        java.lang.String getContentDisplay()
        The textual content of this message in the format that would be shown to the Discord client. All IMentionable entities will be resolved to the format shown by the Discord client instead of the <id> format.

        This includes resolving:
        Users / Members to their @Username/@Nickname format,
        TextChannels to their #ChannelName format,
        Roles to their @RoleName format
        Emotes (not emojis!) to their :name: format.

        If you want the actual Content (mentions as <@id>), use getContentRaw() instead

        Returns:
        The textual content of the message with mentions resolved to be visually like the Discord client.
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getContentRaw

        java.lang.String getContentRaw()
        The raw textual content of this message. Does not resolve IMentionable entities like getContentDisplay() does. This means that this is the completely raw textual content of the message received from Discord and can contain mentions specified by Discord's Message Formatting.
        Returns:
        The raw textual content of the message, containing unresolved Discord message formatting.
      • getContentStripped

        java.lang.String getContentStripped()
        Gets the textual content of this message using getContentDisplay() and then strips it of all markdown characters like *, **, __, ~~ that provide text formatting. Any characters that match these but are not being used for formatting are escaped to prevent possible formatting.
        Returns:
        The textual content from getContentDisplay() with all text formatting characters removed or escaped.
      • getInvites

        java.util.List<java.lang.String> getInvites()
        Creates an immutable List of Invite codes that are included in this Message.
        This will use the Pattern provided under INVITE_PATTERN to construct a Matcher that will parse the getContentRaw() output and include all codes it finds in a list.

        You can use the codes to retrieve/validate invites via Invite.resolve(JDA, String)

        Returns:
        Immutable list of invite codes
      • isFromType

        boolean isFromType​(ChannelType type)
        Used to determine if this Message was received from a MessageChannel of the ChannelType specified.
        This will always be false for ChannelType.VOICE as Messages can't be sent to VoiceChannels.

        Useful for restricting functionality to a certain type of channels.

        Parameters:
        type - The ChannelType to check against.
        Returns:
        True if the ChannelType which this message was received from is the same as the one specified by type.
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getChannelType

        ChannelType getChannelType()
        Gets the ChannelType that this message was received from.
        This will never be ChannelType.VOICE as Messages can't be sent to VoiceChannels.
        Returns:
        The ChannelType which this message was received from.
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • isWebhookMessage

        boolean isWebhookMessage()
        Indicates if this Message was sent by a Webhook instead of a User.
        Useful if you want to ignore non-users.
        Returns:
        True if this message was sent by a Webhook.
      • getChannel

        MessageChannel getChannel()
        Returns the MessageChannel that this message was sent in.
        Returns:
        The MessageChannel of this Message
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getPrivateChannel

        PrivateChannel getPrivateChannel()
        Returns the PrivateChannel that this message was sent in.
        This is only valid if the Message was actually sent in a PrivateChannel. This will return null if it was not sent from a PrivateChannel.
        You can check the type of channel this message was sent from using isFromType(ChannelType) or getChannelType().

        Use getChannel() for an ambiguous MessageChannel if you do not need functionality specific to PrivateChannel.

        Returns:
        The PrivateChannel this message was sent in, or null if it was not sent from a PrivateChannel.
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getGroup

        Group getGroup()
        Returns the Group that this message was sent in.
        This is only valid if the Message was actually sent in a Group. This will return null if it was not sent from a Group.
        You can check the type of channel this message was sent from using isFromType(ChannelType) or getChannelType().

        Use getChannel() for an ambiguous MessageChannel if you do not need functionality specific to Group.

        Returns:
        The Group this message was sent in, or null if it was not sent from a Group.
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getTextChannel

        TextChannel getTextChannel()
        Returns the TextChannel that this message was sent in.
        This is only valid if the Message was actually sent in a TextChannel. This will return null if it was not sent from a TextChannel.
        You can check the type of channel this message was sent from using isFromType(ChannelType) or getChannelType().

        Use getChannel() for an ambiguous MessageChannel if you do not need functionality specific to TextChannel.

        Returns:
        The TextChannel this message was sent in, or null if it was not sent from a TextChannel.
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getCategory

        Category getCategory()
        The Category this message was sent in. This will always be null for DMs and Groups.
        Equivalent to getTextChannel().getParent().
        Returns:
        Category for this message
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getGuild

        Guild getGuild()
        Returns the Guild that this message was sent in.
        This is just a shortcut to getTextChannel().getGuild().
        This is only valid if the Message was actually sent in a TextChannel. This will return null if it was not sent from a TextChannel.
        You can check the type of channel this message was sent from using isFromType(ChannelType) or getChannelType().
        Returns:
        The Guild this message was sent in, or null if it was not sent from a TextChannel.
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getEmbeds

        java.util.List<MessageEmbed> getEmbeds()
        An unmodifiable list of MessageEmbeds that are part of this Message.
        Returns:
        Unmodifiable list of all given MessageEmbeds.
      • getEmotes

        java.util.List<Emote> getEmotes()
        All Emotes used in this Message.
        This only includes Custom Emotes, not UTF8 Emojis. JDA classifies Emotes as the Custom Emojis uploaded to a Guild and retrievable with Guild.getEmotes(). These are not the same as the UTF8 emojis that Discord also supports.

        This may or may not contain fake Emotes which means they can be displayed but not used by the logged in account. To check whether an Emote is fake you can test if IFakeable.isFake() returns true.

        Unicode emojis are not included as Emote!

        Returns:
        An immutable list of the Emotes used in this message (example match <:jda:230988580904763393>)
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • getReactions

        java.util.List<MessageReaction> getReactions()
        All MessageReactions that are on this Message.
        Returns:
        immutable list of all MessageReactions on this message.
      • isTTS

        boolean isTTS()
        Defines whether or not this Message triggers TTS (Text-To-Speech).
        Returns:
        If this message is TTS.
      • editMessage

        @CheckReturnValue
        MessageAction editMessage​(java.lang.CharSequence newContent)
        Edits this Message's content to the provided String.
        Messages can only be edited by the account that sent them!.

        This message instance will not be updated by this operation, please use the response message instead.

        The following ErrorResponses are possible:

        Parameters:
        newContent - the new content of the Message
        Returns:
        MessageAction
        The Message with the updated content
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
        java.lang.IllegalStateException - If the message attempting to be edited was not created by the currently logged in account, or if newContent's length is 0 or greater than 2000.
      • editMessage

        @CheckReturnValue
        MessageAction editMessage​(MessageEmbed newContent)
        Edits this Message's content to the provided MessageEmbed.
        Messages can only be edited by the account that sent them!.

        This message instance will not be updated by this operation, please use the response message instead.

        The following ErrorResponses are possible:

        Parameters:
        newContent - the new content of the Message
        Returns:
        MessageAction
        The Message with the updated content
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
        java.lang.IllegalStateException - If the message attempting to be edited was not created by the currently logged in account, or if the passed-in embed is null or not sendable
      • editMessageFormat

        @CheckReturnValue
        MessageAction editMessageFormat​(java.lang.String format,
                                        java.lang.Object... args)
        Edits this Message's content to the provided format.
        Shortcut for MessageBuilder.appendFormat(String, Object...).
        Messages can only be edited by the account that sent them!.

        This message instance will not be updated by this operation, please use the response message instead.

        The following ErrorResponses are possible:

        Parameters:
        format - Format String used to generate the Message's content via MessageBuilder.appendFormat(String, Object...) specification
        args - The arguments to use in order to be converted in the format string
        Returns:
        MessageAction
        The Message with the updated content
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
        java.lang.IllegalArgumentException - If the provided format String is null or blank, or if the created message exceeds the 2000 character limit
        java.util.IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
        java.lang.IllegalStateException - If the message attempting to be edited was not created by the currently logged in account
      • editMessage

        @CheckReturnValue
        MessageAction editMessage​(Message newContent)
        Edits this Message's content to the provided Message.
        Messages can only be edited by the account that sent them!.

        This message instance will not be updated by this operation, please use the response message instead.

        The following ErrorResponses are possible:

        Parameters:
        newContent - the new content of the Message
        Returns:
        MessageAction
        The Message with the updated content
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
        java.lang.IllegalStateException -
        • If the message attempting to be edited was not created by the currently logged in account
        • If the message contains a MessageEmebd that is not sendable
      • getJDA

        JDA getJDA()
        Returns the JDA instance related to this Message.
        Returns:
        the corresponding JDA instance
        Throws:
        java.lang.UnsupportedOperationException - If this is not a Received Message from MessageType.DEFAULT
      • isPinned

        boolean isPinned()
        Whether or not this Message has been pinned in its parent channel.
        Returns:
        True - if this message has been pinned.
      • getType

        MessageType getType()
        This specifies the MessageType of this Message.

        Messages can represent more than just simple text sent by Users, they can also be special messages that inform about events occurs. A few examples are the system message informing that a message has been pinned. Another would be the system message informing that a call has been started or ended in a group.

        Returns:
        The MessageType of this message.