Package com.marcpg.libpg.web.discord
Class Webhook
java.lang.Object
com.marcpg.libpg.web.discord.Webhook
Can be used to interact with Discord webhooks and send messages and embeds to them.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull String
escapeJson
(@NotNull String unescaped) Formats aString
to be compatible with JSON, by escaping backslashes, normal slashes and quotation marks.getUrl()
Gets the URL that's posted to.int
Posts aMessage
to the webhook, which can contain text, embeds and attachments.int
Posts an emptyMessage
with only embeds to the webhook.int
Posts a plaintextMessage
to the webhook.int
Posts an emptyMessage
with only embeds to the webhook.int
Posts a raw JSON to the webhook.void
Change the URL that's posted to.
-
Constructor Details
-
Webhook
Creates a new webhook to post messages to.- Parameters:
webhookUrl
- The Discord webhook URL to post all messages to.
-
-
Method Details
-
postRaw
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
Posts aMessage
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
Posts a plaintextMessage
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
Posts an emptyMessage
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
Posts an emptyMessage
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
Gets the URL that's posted to.- Returns:
- The Discord webhook URL that's posted to.
-
setUrl
Change the URL that's posted to.
Generally not recommended and has no intended use case.- Parameters:
url
- The new Discord webhook URL
-
escapeJson
Formats aString
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.
-