Class Webhook

java.lang.Object
com.marcpg.libpg.web.discord.Webhook

public final class Webhook extends Object
Can be used to interact with Discord webhooks and send messages and embeds to them.
Since:
0.0.4
See Also:
  • Constructor Details

    • Webhook

      public Webhook(URL webhookUrl)
      Creates a new webhook to post messages to.
      Parameters:
      webhookUrl - The Discord webhook URL to post all messages to.
  • Method Details

    • postRaw

      public int postRaw(String messageJson) throws IOException
      Posts a raw JSON to the webhook. Should not be used, as it's unreliable. Use the post() methods instead.
      Parameters:
      messageJson - The message to send as a JSON.
      Returns:
      The http response code.
      Throws:
      IOException - if there was an error while posting the message.
    • post

      public int post(@NotNull @NotNull Message message) throws IOException
      Posts a Message to the webhook, which can contain text, embeds and attachments.
      Parameters:
      message - The message to send.
      Returns:
      The http response code.
      Throws:
      IOException - if there was an error while posting the message.
    • post

      public int post(String content) throws IOException
      Posts a plaintext Message to the webhook.
      Parameters:
      content - The plaintext to send.
      Returns:
      The http response code.
      Throws:
      IOException - if there was an error while posting the message.
    • post

      public int post(Embed... embeds) throws IOException
      Posts an empty Message with only embeds to the webhook.
      Parameters:
      embeds - All embeds to be posted.
      Returns:
      The http response code.
      Throws:
      IOException - if there was an error while posting the message.
    • post

      public int post(List<Embed> embeds) throws IOException
      Posts an empty Message with only embeds to the webhook.
      Parameters:
      embeds - A list of all embeds to be posted.
      Returns:
      The http response code.
      Throws:
      IOException - if there was an error while posting the message.
    • getUrl

      public URL getUrl()
      Gets the URL that's posted to.
      Returns:
      The Discord webhook URL that's posted to.
    • setUrl

      public void setUrl(URL url)
      Change the URL that's posted to.
      Generally not recommended and has no intended use case.
      Parameters:
      url - The new Discord webhook URL
    • escapeJson

      @NotNull public static @NotNull String escapeJson(@NotNull @NotNull String unescaped)
      Formats a String to be compatible with JSON, by escaping backslashes, normal slashes and quotation marks. Will run into issues if the input already has these characters escaped.
      Parameters:
      unescaped - The string or json string without any escaped characters.
      Returns:
      A fully escaped and JSON-compatible string.