Class MessageBuilder


  • public class MessageBuilder
    extends java.lang.Object
    This class can help you to create messages.
    • Constructor Detail

      • MessageBuilder

        public MessageBuilder()
        Class constructor.
    • Method Detail

      • fromMessage

        public static MessageBuilder fromMessage​(Message message)
        Creates a message builder from a message.
        Parameters:
        message - The message to copy.
        Returns:
        A message builder which would produce the same text as the given message.
      • copy

        public MessageBuilder copy​(Message message)
        Fill the builder's values with a message.
        Parameters:
        message - The message to copy.
        Returns:
        The current instance in order to chain call methods.
      • setTts

        public MessageBuilder setTts​(boolean tts)
        Sets if the message should be text to speech.
        Parameters:
        tts - Whether the message should be text to speech or not.
        Returns:
        The current instance in order to chain call methods.
      • addSticker

        public MessageBuilder addSticker​(long stickerId)
        Adds a sticker to the message. This will only work if the sticker is from the same server as the message will be sent on, or if it is a default sticker.
        Parameters:
        stickerId - The ID of the sticker to add.
        Returns:
        The current instance in order to chain call methods.
      • addSticker

        public MessageBuilder addSticker​(Sticker sticker)
        Adds a sticker to the message. This will only work if the sticker is from the same server as the message will be sent on, or if it is a default sticker.
        Parameters:
        sticker - The sticker to add.
        Returns:
        The current instance in order to chain call methods.
      • addStickers

        public MessageBuilder addStickers​(long... stickerIds)
        Adds stickers to the message. You can add up to 3 different stickers per message. Adding the same sticker twice or more will just add the sticker once. This will only work if the stickers are from the same server as the message will be sent on, or if they are default stickers.
        Parameters:
        stickerIds - The IDs of the stickers to add.
        Returns:
        The current instance in order to chain call methods.
      • addStickers

        public MessageBuilder addStickers​(java.util.Collection<Sticker> stickers)
        Adds stickers to the message. You can add up to 3 different stickers per message. Adding the same sticker twice or more will just add the sticker once. This will only work if the stickers are from the same server as the message will be sent on, or if they are default stickers.
        Parameters:
        stickers - The IDs of the stickers to add.
        Returns:
        The current instance in order to chain call methods.
      • replyTo

        public MessageBuilder replyTo​(Message message)
        Sets the message to reply to.
        Parameters:
        message - The message to reply to.
        Returns:
        The current instance in order to chain call methods.
      • replyTo

        public MessageBuilder replyTo​(long messageId)
        Sets the message to reply to.
        Parameters:
        messageId - The id of the message to reply to.
        Returns:
        The current instance in order to chain call methods.
      • replyTo

        public MessageBuilder replyTo​(Message message,
                                      boolean assertReferenceExists)
        Sets the message to reply to.
        Parameters:
        message - The message to reply to.
        assertReferenceExists - Used to tell discord if you want to check if the message exists.
        Returns:
        The current instance in order to chain call methods.
      • replyTo

        public MessageBuilder replyTo​(long messageId,
                                      boolean assertReferenceExists)
        Sets the message to reply to.
        Parameters:
        messageId - The id of the message to reply to.
        assertReferenceExists - Used to tell discord if you want to check if the message exists.
        Returns:
        The current instance in order to chain call methods.
      • send

        public java.util.concurrent.CompletableFuture<Message> send​(User user)
        Sends the message.
        Parameters:
        user - The user to which the message should be sent.
        Returns:
        The sent message.
      • send

        public java.util.concurrent.CompletableFuture<Message> send​(TextChannel channel)
        Sends the message.
        Parameters:
        channel - The channel in which the message should be sent.
        Returns:
        The sent message.
      • send

        public java.util.concurrent.CompletableFuture<Message> send​(IncomingWebhook webhook)
        Sends the message.
        Parameters:
        webhook - The webhook from which the message should be sent.
        Returns:
        The sent message.
      • send

        public java.util.concurrent.CompletableFuture<Message> send​(Messageable messageable)
        Sends the message.
        Parameters:
        messageable - The receiver of the message.
        Returns:
        The sent message.
      • sendWithWebhook

        public java.util.concurrent.CompletableFuture<Message> sendWithWebhook​(DiscordApi api,
                                                                               long webhookId,
                                                                               java.lang.String webhookToken)
        Sends the message.
        Parameters:
        api - The api instance needed to send and return the message.
        webhookId - The id of the webhook from which the message should be sent.
        webhookToken - The token of the webhook from which the message should be sent.
        Returns:
        The sent message.
      • sendWithWebhook

        public java.util.concurrent.CompletableFuture<Message> sendWithWebhook​(DiscordApi api,
                                                                               java.lang.String webhookId,
                                                                               java.lang.String webhookToken)
        Sends the message.
        Parameters:
        api - The api instance needed to send and return the message.
        webhookId - The id of the webhook from which the message should be sent.
        webhookToken - The token of the webhook from which the message should be sent.
        Returns:
        The sent message.
      • sendWithWebhook

        public java.util.concurrent.CompletableFuture<Message> sendWithWebhook​(DiscordApi api,
                                                                               java.lang.String webhookUrl)
                                                                        throws java.lang.IllegalArgumentException
        Sends the message.
        Parameters:
        api - The api instance needed to send the message.
        webhookUrl - The url of the webhook from which the message should be sent.
        Returns:
        The sent message.
        Throws:
        java.lang.IllegalArgumentException - If the link isn't valid.
      • addComponents

        public T addComponents​(HighLevelComponent... components)
        Add multiple high level components to the message.
        Parameters:
        components - The high level components.
        Returns:
        The current instance in order to chain call methods.
      • addActionRow

        public T addActionRow​(LowLevelComponent... components)
        Add multiple low level components, wrapped in an ActionRow, to the message.
        Parameters:
        components - The low level components.
        Returns:
        The current instance in order to chain call methods.
      • appendCode

        public T appendCode​(java.lang.String language,
                            java.lang.String code)
        Appends code to the message.
        Parameters:
        language - The language, e.g. "java".
        code - The code.
        Returns:
        The current instance in order to chain call methods.
      • appendTimestamp

        public T appendTimestamp​(long epochSeconds)
        Appends a timestamp to the message with the default timestamp style TimestampStyle.SHORT_DATE_TIME.
        Parameters:
        epochSeconds - The epoch time in seconds.
        Returns:
        The current instance in order to chain call methods.
      • appendTimestamp

        public T appendTimestamp​(java.time.Instant instant)
        Appends a timestamp to the message with the default timestamp style TimestampStyle.SHORT_DATE_TIME.
        Parameters:
        instant - The instant for the displaying timestamp.
        Returns:
        The current instance in order to chain call methods.
      • appendTimestamp

        public T appendTimestamp​(long epochSeconds,
                                 TimestampStyle timestampStyle)
        Appends a timestamp to the message.
        Parameters:
        epochSeconds - The epoch time in seconds.
        timestampStyle - The displayed timestamp style.
        Returns:
        The current instance in order to chain call methods.
      • appendTimestamp

        public T appendTimestamp​(java.time.Instant instant,
                                 TimestampStyle timestampStyle)
        Appends a timestamp to the message.
        Parameters:
        instant - The instant for the displaying timestamp.
        timestampStyle - The displayed timestamp style.
        Returns:
        The current instance in order to chain call methods.
      • append

        public T append​(java.lang.String message,
                        MessageDecoration... decorations)
        Appends a sting with or without decoration to the message.
        Parameters:
        message - The string to append.
        decorations - The decorations of the string.
        Returns:
        The current instance in order to chain call methods.
      • append

        public T append​(Mentionable entity)
        Appends a mentionable entity (usually a user or channel) to the message.
        Parameters:
        entity - The entity to mention.
        Returns:
        The current instance in order to chain call methods.
      • append

        public T append​(java.lang.Object object)
        Appends the string representation of the object (calling String.valueOf(Object) method) to the message.
        Parameters:
        object - The object to append.
        Returns:
        The current instance in order to chain call methods.
        See Also:
        StringBuilder.append(Object)
      • appendNewLine

        public T appendNewLine()
        Appends a new line to the message.
        Returns:
        The current instance in order to chain call methods.
      • setContent

        public T setContent​(java.lang.String content)
        Sets the content of the message. This method overwrites all previous content changes (using append(String, MessageDecoration...) for example).
        Parameters:
        content - The new content of the message.
        Returns:
        The current instance in order to chain call methods.
      • removeContent

        public T removeContent()
        Removes the content of the message. This method overwrites all previous content changes (using append(String, MessageDecoration...) for example).
        Returns:
        The current instance in order to chain call methods.
      • setEmbed

        public T setEmbed​(EmbedBuilder embed)
        Sets the embed of the message (overrides all existing embeds).
        Parameters:
        embed - The embed to set.
        Returns:
        The current instance in order to chain call methods.
      • setEmbeds

        public T setEmbeds​(EmbedBuilder... embeds)
        Sets multiple embeds of the message (overrides all existing embeds).
        Parameters:
        embeds - The embed to set.
        Returns:
        The current instance in order to chain call methods.
      • setEmbeds

        public T setEmbeds​(java.util.List<EmbedBuilder> embeds)
        Sets multiple embeds of the message (overrides all existing embeds).
        Parameters:
        embeds - The embed to set.
        Returns:
        The current instance in order to chain call methods.
      • addEmbed

        public T addEmbed​(EmbedBuilder embed)
        Adds an embed to the message.
        Parameters:
        embed - The embed to add.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(java.awt.image.BufferedImage image,
                               java.lang.String fileName)
        Adds an attachment to the message.
        Parameters:
        image - The image to add as an attachment.
        fileName - The file name of the image.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(java.awt.image.BufferedImage image,
                               java.lang.String fileName,
                               java.lang.String description)
        Adds an attachment to the message.
        Parameters:
        image - The image to add as an attachment.
        fileName - The file name of the image.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(java.io.File file)
        Adds an attachment to the message.
        Parameters:
        file - The file to add as an attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(java.io.File file,
                               java.lang.String description)
        Adds an attachment to the message.
        Parameters:
        file - The file to add as an attachment.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(Icon icon)
        Adds an attachment to the message.
        Parameters:
        icon - The icon to add as an attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(Icon icon,
                               java.lang.String description)
        Adds an attachment to the message.
        Parameters:
        icon - The icon to add as an attachment.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(java.net.URL url)
        Adds an attachment to the message.
        Parameters:
        url - The url of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(java.net.URL url,
                               java.lang.String description)
        Adds an attachment to the message.
        Parameters:
        url - The url of the attachment.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(byte[] bytes,
                               java.lang.String fileName)
        Adds an attachment to the message.
        Parameters:
        bytes - The bytes of the file.
        fileName - The name of the file.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(byte[] bytes,
                               java.lang.String fileName,
                               java.lang.String description)
        Adds an attachment to the message.
        Parameters:
        bytes - The bytes of the file.
        fileName - The name of the file.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(java.io.InputStream stream,
                               java.lang.String fileName)
        Adds an attachment to the message.
        Parameters:
        stream - The stream of the file.
        fileName - The name of the file.
        Returns:
        The current instance in order to chain call methods.
      • addAttachment

        public T addAttachment​(java.io.InputStream stream,
                               java.lang.String fileName,
                               java.lang.String description)
        Adds an attachment to the message.
        Parameters:
        stream - The stream of the file.
        fileName - The name of the file.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(java.awt.image.BufferedImage image,
                                        java.lang.String fileName)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        image - The image to add as an attachment.
        fileName - The file name of the image.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(java.awt.image.BufferedImage image,
                                        java.lang.String fileName,
                                        java.lang.String description)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        image - The image to add as an attachment.
        fileName - The file name of the image.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(java.io.File file)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        file - The file to add as an attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(java.io.File file,
                                        java.lang.String description)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        file - The file to add as an attachment.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(Icon icon)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        icon - The icon to add as an attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(Icon icon,
                                        java.lang.String description)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        icon - The icon to add as an attachment.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(java.net.URL url)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        url - The url of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(java.net.URL url,
                                        java.lang.String description)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        url - The url of the attachment.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(byte[] bytes,
                                        java.lang.String fileName)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        bytes - The bytes of the file.
        fileName - The name of the file.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(byte[] bytes,
                                        java.lang.String fileName,
                                        java.lang.String description)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        bytes - The bytes of the file.
        fileName - The name of the file.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(java.io.InputStream stream,
                                        java.lang.String fileName)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        stream - The stream of the file.
        fileName - The name of the file.
        Returns:
        The current instance in order to chain call methods.
      • addAttachmentAsSpoiler

        public T addAttachmentAsSpoiler​(java.io.InputStream stream,
                                        java.lang.String fileName,
                                        java.lang.String description)
        Adds an attachment to the message and marks it as spoiler.
        Parameters:
        stream - The stream of the file.
        fileName - The name of the file.
        description - The description of the attachment.
        Returns:
        The current instance in order to chain call methods.
      • setAllowedMentions

        public T setAllowedMentions​(AllowedMentions allowedMentions)
        Controls who will be mentioned if mentions exist in the message.
        Parameters:
        allowedMentions - The mention object.
        Returns:
        The current instance in order to chain call methods.
      • removeAllComponents

        public T removeAllComponents()
        Remove all high-level components from the message.
        Returns:
        The current instance in order to chain call methods.
      • addEmbeds

        public T addEmbeds​(EmbedBuilder... embeds)
        Adds the embeds to the message.
        Parameters:
        embeds - The embeds to add.
        Returns:
        The current instance in order to chain call methods.
      • addEmbeds

        public T addEmbeds​(java.util.List<EmbedBuilder> embeds)
        Adds the embeds to the message.
        Parameters:
        embeds - The embeds to add.
        Returns:
        The current instance in order to chain call methods.
      • removeEmbed

        public T removeEmbed​(EmbedBuilder embed)
        Removes the embed from the message.
        Parameters:
        embed - The embed to remove.
        Returns:
        The current instance in order to chain call methods.
      • removeEmbeds

        public T removeEmbeds​(EmbedBuilder... embeds)
        Removes the embeds from the message.
        Parameters:
        embeds - The embeds to remove.
        Returns:
        The current instance in order to chain call methods.
      • removeAllEmbeds

        public T removeAllEmbeds()
        Removes all embeds from the message.
        Returns:
        The current instance in order to chain call methods.
      • setNonce

        public T setNonce​(java.lang.String nonce)
        Sets the nonce of the message.
        Parameters:
        nonce - The nonce to set.
        Returns:
        The current instance in order to chain call methods.
      • getStringBuilder

        public java.lang.StringBuilder getStringBuilder()
        Gets the StringBuilder which is used to build the message.
        Returns:
        The StringBuilder which is used to build the message.