Class WebhookMessageBuilder



  • public class WebhookMessageBuilder
    extends Object
    This class can help you to create webhook messages.
    • Constructor Detail

      • WebhookMessageBuilder

        public WebhookMessageBuilder​()
    • Method Detail

      • fromMessage

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

        public WebhookMessageBuilder appendCode​(String language,
                                                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.
      • append

        public WebhookMessageBuilder append​(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 WebhookMessageBuilder 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.
      • appendNewLine

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

        public WebhookMessageBuilder setContent​(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.
      • addEmbed

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

        public WebhookMessageBuilder 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.
      • removeEmbed

        public WebhookMessageBuilder 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 WebhookMessageBuilder 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 WebhookMessageBuilder removeAllEmbeds​()
        Removes all embeds from the message.
        Returns:
        The current instance in order to chain call methods.
      • setTts

        public WebhookMessageBuilder 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.
      • addFile

        public WebhookMessageBuilder addFile​(File file)
        Adds a file to the message.
        Parameters:
        file - The file to add as an attachment.
        Returns:
        The current instance in order to chain call methods.
        See Also:
        addAttachment(File)
      • addFile

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

        public WebhookMessageBuilder addFile​(URL url)
        Adds a file to the message and marks it as a spoiler.
        Parameters:
        url - The url of the attachment.
        Returns:
        The current instance in order to chain call methods.
        See Also:
        addAttachment(URL)
      • addFile

        public WebhookMessageBuilder addFile​(byte[] bytes,
                                             String fileName)
        Adds a file 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.
        See Also:
        addAttachment(byte[], String)
      • addFileAsSpoiler

        public WebhookMessageBuilder addFileAsSpoiler​(File file)
        Adds a file 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.
        See Also:
        addAttachmentAsSpoiler(File)
      • addFileAsSpoiler

        public WebhookMessageBuilder addFileAsSpoiler​(Icon icon)
        Adds a file 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.
        See Also:
        addAttachmentAsSpoiler(Icon)
      • addFileAsSpoiler

        public WebhookMessageBuilder addFileAsSpoiler​(URL url)
        Adds a file to the message and marks it as a spoiler.
        Parameters:
        url - The url of the attachment.
        Returns:
        The current instance in order to chain call methods.
        See Also:
        addAttachment(URL)
      • addFileAsSpoiler

        public WebhookMessageBuilder addFileAsSpoiler​(byte[] bytes,
                                                      String fileName)
        Adds a file 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.
        See Also:
        addAttachmentAsSpoiler(byte[], String)
      • addAttachment

        public WebhookMessageBuilder addAttachment​(BufferedImage image,
                                                   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 WebhookMessageBuilder addAttachment​(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 WebhookMessageBuilder 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 WebhookMessageBuilder addAttachment​(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 WebhookMessageBuilder addAttachment​(byte[] bytes,
                                                   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 WebhookMessageBuilder addAttachment​(InputStream stream,
                                                   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.
      • addAttachmentAsSpoiler

        public WebhookMessageBuilder addAttachmentAsSpoiler​(BufferedImage image,
                                                            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 WebhookMessageBuilder addAttachmentAsSpoiler​(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 WebhookMessageBuilder 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 WebhookMessageBuilder addAttachmentAsSpoiler​(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 WebhookMessageBuilder addAttachmentAsSpoiler​(byte[] bytes,
                                                            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 WebhookMessageBuilder addAttachmentAsSpoiler​(InputStream stream,
                                                            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.
      • setAllowedMentions

        public WebhookMessageBuilder 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.
      • setDisplayName

        public WebhookMessageBuilder setDisplayName​(String displayName)
        Sets the display name of the webhook.
        Parameters:
        displayName - The display name of the webhook.
        Returns:
        The current instance in order to chain call methods.
      • setDisplayAvatar

        public WebhookMessageBuilder setDisplayAvatar​(URL avatarUrl)
        Sets the display avatar of the webhook.
        Parameters:
        avatarUrl - The display avatar of the webhook.
        Returns:
        The current instance in order to chain call methods.
      • setDisplayAvatar

        public WebhookMessageBuilder setDisplayAvatar​(Icon avatar)
        Sets the display avatar of the webhook.
        Parameters:
        avatar - The display avatar of the webhook.
        Returns:
        The current instance in order to chain call methods.
      • setDisplayAuthor

        public WebhookMessageBuilder setDisplayAuthor​(MessageAuthor author)
        Sets the display avatar of the webhook.
        Parameters:
        author - The author to take display name and display avatar of.
        Returns:
        The current instance in order to chain call methods.
      • setDisplayAuthor

        public WebhookMessageBuilder setDisplayAuthor​(User author)
        Sets the display avatar of the webhook.
        Parameters:
        author - The author to take display name and display avatar of.
        Returns:
        The current instance in order to chain call methods.
      • getStringBuilder

        public StringBuilder getStringBuilder​()
        Gets the StringBuilder which is used to build the message.
        Returns:
        The StringBuilder which is used to build the message.
      • send

        public 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 CompletableFuture<Message> send​(DiscordApi api,
                                               long webhookId,
                                               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.
      • send

        public CompletableFuture<Message> send​(DiscordApi api,
                                               String webhookId,
                                               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.
      • sendSilently

        public CompletableFuture<Void> sendSilently​(IncomingWebhook webhook)
        Sends the message without waiting for a response.
        Parameters:
        webhook - The webhook from which the message should be sent.
        Returns:
        A CompletableFuture indicating whether or not sending the request to discord was successful.
      • sendSilently

        public CompletableFuture<Void> sendSilently​(DiscordApi api,
                                                    long webhookId,
                                                    String webhookToken)
        Sends the message without waiting for a response.
        Parameters:
        api - The api instance needed to send 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:
        A CompletableFuture indicating whether or not sending the request to discord was successful.
      • sendSilently

        public CompletableFuture<Void> sendSilently​(DiscordApi api,
                                                    String webhookId,
                                                    String webhookToken)
        Sends the message without waiting for a response.
        Parameters:
        api - The api instance needed to send 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:
        A CompletableFuture indicating whether or not sending the request to discord was successful.
      • sendSilently

        public CompletableFuture<Void> sendSilently​(DiscordApi api,
                                                    String webhookUrl)
                                             throws IllegalArgumentException
        Sends the message without waiting for a response.
        Parameters:
        api - The api instance needed to send the message.
        webhookUrl - The url of the webhook from which the message should be sent.
        Returns:
        A CompletableFuture indicating whether or not sending the request to discord was successful.
        Throws:
        IllegalArgumentException - If the link isn't valid.