{"id":13410348,"url":"https://github.com/MinnDevelopment/discord-webhooks","last_synced_at":"2025-03-14T15:32:30.952Z","repository":{"id":37496320,"uuid":"155432679","full_name":"MinnDevelopment/discord-webhooks","owner":"MinnDevelopment","description":"Provides easy to use bindings for the Discord Webhook API","archived":false,"fork":false,"pushed_at":"2024-02-28T18:32:00.000Z","size":407,"stargazers_count":180,"open_issues_count":8,"forks_count":32,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-16T11:54:58.232Z","etag":null,"topics":["discord","gradle","hacktoberfest","java","webhooks"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MinnDevelopment.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-10-30T18:00:02.000Z","updated_at":"2024-10-08T21:16:48.000Z","dependencies_parsed_at":"2024-02-27T10:47:12.026Z","dependency_job_id":null,"html_url":"https://github.com/MinnDevelopment/discord-webhooks","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MinnDevelopment%2Fdiscord-webhooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MinnDevelopment%2Fdiscord-webhooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MinnDevelopment%2Fdiscord-webhooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MinnDevelopment%2Fdiscord-webhooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MinnDevelopment","download_url":"https://codeload.github.com/MinnDevelopment/discord-webhooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221482000,"owners_count":16829979,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["discord","gradle","hacktoberfest","java","webhooks"],"created_at":"2024-07-30T20:01:06.360Z","updated_at":"2024-10-26T01:30:53.273Z","avatar_url":"https://github.com/MinnDevelopment.png","language":"Java","readme":"[version]: https://img.shields.io/maven-central/v/club.minnced/discord-webhooks\n[download]: https://mvnrepository.com/artifact/club.minnced/discord-webhooks/latest\n[license]: https://img.shields.io/badge/License-Apache%202.0-lightgrey.svg\n[license-file]: https://github.com/MinnDevelopment/discord-webhooks/blob/master/LICENSE\n\n[WebhookClient#setErrorHandler]: https://minndevelopment.github.io/discord-webhooks/club/minnced/discord/webhook/WebhookClient.html#setErrorHandler(club.minnced.discord.webhook.util.WebhookErrorHandler)\n[WebhookClient#setDefaultErrorHandler]: https://minndevelopment.github.io/discord-webhooks/club/minnced/discord/webhook/WebhookClient.html#setDefaultErrorHandler(club.minnced.discord.webhook.util.WebhookErrorHandler)\n\n[ ![version] ][download]\n[ ![license] ][license-file]\n\n# Discord-Webhooks\n\nOriginally part of JDA, this library provides easy to use bindings for the\nDiscord Webhook API.\n\n# Introduction\n\nHere we will give a small overview of the proper usage and applicability of the resources provided by this library.\n\nDocumentation is available via the GitHub pages on this repository: [Javadoc](https://minndevelopment.github.io/discord-webhooks/overview-tree.html)\n\n## Limitations\n\nWebhooks on discord are only capable of sending messages, nothing more. For anything else you either have to use OAuth2 or a bot account. This library does not provide any functionality for creating or modifying webhooks.\n\n## Getting Started\n\nThe first thing to do is to create either a `WebhookClient` or a `WebhookCluster`. The `WebhookClient` provides functionality to send messages to one webhook based on either a webhook URL or the ID and token of a webhook. It implements automatic rate-limit handling and can be configured to use a shared thread-pool.\n\n### Creating a WebhookClient\n\n```java\n// Using the builder\nWebhookClientBuilder builder = new WebhookClientBuilder(url); // or id, token\nbuilder.setThreadFactory((job) -\u003e {\n    Thread thread = new Thread(job);\n    thread.setName(\"Hello\");\n    thread.setDaemon(true);\n    return thread;\n});\nbuilder.setWait(true);\nWebhookClient client = builder.build();\n```\n\n```java\n// Using the factory methods\nWebhookClient client = WebhookClient.withUrl(url); // or withId(id, token)\n```\n\n### Creating a WebhookCluster\n\n```java\n// Create and initialize the cluster\nWebhookCluster cluster = new WebhookCluster(5); // create an initial 5 slots (dynamic like lists)\ncluster.setDefaultHttpClient(new OkHttpClient());\ncluster.setDefaultDaemon(true);\n\n// Create a webhook client\ncluster.buildWebhook(id, token);\n\n// Add an existing webhook client\ncluster.addWebhook(client);\n```\n\n## Sending Messages\n\nSending messages happens in a background thread (configured through the pool/factory) and thus is async by default. To access the message you have to enable the `wait` mechanic (enabled by default). With this you can use the callbacks provided by `CompletableFuture\u003cReadonlyMessage\u003e`.\n\n```java\n// Send and forget\nclient.send(\"Hello World\");\n\n// Send and log (using embed)\nWebhookEmbed embed = new WebhookEmbedBuilder()\n        .setColor(0xFF00EE)\n        .setDescription(\"Hello World\")\n        .build();\n\nclient.send(embed)\n      .thenAccept((message) -\u003e System.out.printf(\"Message with embed has been sent [%s]%n\", message.getId()));\n\n// Change appearance of webhook message\nWebhookMessage message = new WebhookMessageBuilder()\n        .setUsername(\"Minn\") // use this username\n        .setAvatarUrl(avatarUrl) // use this avatar\n        .setContent(\"Hello World\")\n        .build();\nclient.send(message);\n```\n\n## Threads\n\nYou can use the webhook clients provided by this library to send messages in threads. There are two ways to accomplish this.\n\nSet a thread id in the client builder to send all messages in that client to the thread:\n\n```java\nWebhookClient client = new WebhookClientBuilder(url)\n        .setThreadId(threadId)\n        .build();\n\nclient.send(\"Hello\"); // appears in the thread\n```\n\nUse `onThread` to create a client with a thread id and all other settings inherited:\n\n```java\ntry (WebhookClient client = WebhookClient.withUrl(url)) {\n    WebhookClient thread = client.onThread(123L);\n    thread.send(\"Hello\"); // appears only in the thread with id 123\n    client.send(\"Friend\"); // appears in the channel instead\n} // calls client.close() which automatically also closes all onThread clients as well.\n```\n\nAll `WebhookClient` instances created with `onThread` will share the same thread pool used by the original client. This means that shutting down or closing any of the clients will also close all other clients associated with that underlying thread pool.\n\n```java\nWebhookClient thread = null;\ntry (WebhookClient client = WebhookClient.withUrl(url)) {\n    thread = client.onThread(id);\n} // closes client\nthread.send(\"Hello\"); // \u003c- throws rejected execution due to pool being shutdown by client.close() above ^\n\nWebhookClient client = WebhookClient.withUrl(url);\ntry (WebhookClient thread = client.onThread(id)) {\n    thread.send(\"...\");\n} // closes thread\nclient.send(\"Hello\");  // \u003c- throws rejected execution due to pool being shutdown by thread.close() above ^\n```\n\n### Shutdown\n\nSince the clients use threads for sending messages you should close the client to end the threads. This can be ignored if a shared thread-pool is used between multiple clients but that pool has to be shutdown by the user accordingly.\n\n```java\ntry (WebhookClient client = WebhookClient.withUrl(url)) {\n    client.send(\"Hello World\");\n} // client.close() automated\n\nwebhookCluster.close(); // closes each client and can be used again\n```\n\n## Error Handling\n\nBy default, this library will log every exception encountered when sending a message using the SLF4J logger implementation.\nThis can be configured using [WebhookClient#setErrorHandler] to custom behavior per client or [WebhookClient#setDefaultErrorHandler] for all clients.\n\n### Example\n\n```java\nWebhookClient.setDefaultErrorHandler((client, message, throwable) -\u003e {\n    System.err.printf(\"[%s] %s%n\", client.getId(), message);\n    if (throwable != null)\n        throwable.printStackTrace();\n    // Shutdown the webhook client when you get 404 response (may also trigger for client#edit calls, be careful)\n    if (throwable instanceof HttpException ex \u0026\u0026 ex.getCode() == 404) {\n        client.close();\n    }\n});\n```\n\n## External Libraries\n\nThis library also supports sending webhook messages with integration from other libraries such as\n\n- [JDA](/DV8FromTheWorld/JDA) (version 5.0.0-beta.12) with [JDAWebhookClient](https://github.com/MinnDevelopment/discord-webhooks/blob/master/src/main/java/club/minnced/discord/webhook/external/JDAWebhookClient.java)\n- [Discord4J](/Discord4J/Discord4J) (version 3.2.5) with [D4JWebhookClient](https://github.com/MinnDevelopment/discord-webhooks/blob/master/src/main/java/club/minnced/discord/webhook/external/D4JWebhookClient.java)\n- [Javacord](/Javacord/Javacord) (version 3.8.0) with [JavacordWebhookClient](https://github.com/MinnDevelopment/discord-webhooks/blob/master/src/main/java/club/minnced/discord/webhook/external/JavacordWebhookClient.java)\n\n### Example JDA\n\n```java\npublic void sendWebhook(Webhook webhook) {\n    MessageCreateData message = new MessageCreateBuilder().setContent(\"Hello World\").build();\n    try (JDAWebhookClient client = JDAWebhookClient.from(webhook)) { // create a client instance from the JDA webhook\n        client.send(message); // send a JDA message instance\n    }\n}\n```\n\n### Example Discord4J\n\n```java\npublic void sendWebhook(Webhook webhook) {\n    try (D4JWebhookClient client = D4JWebhookClient.from(webhook)) {\n        client.send(MessageCreateSpec.create()\n            .withContent(\"Hello World\")\n            .addFile(\"cat.png\", new FileInputStream(\"cat.png\"))\n        );\n    }\n}\n```\n\n# Download\n\n[ ![version] ][download]\n\nNote: Replace `%VERSION%` below with the desired version.\n\n## Gradle\n\n```gradle\nrepositories {\n    mavenCentral()\n}\n```\n\n```gradle\ndependencies {\n    implementation(\"club.minnced:discord-webhooks:%VERSION%\")\n}\n```\n\n## Maven\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eclub.minnced\u003c/groupId\u003e\n    \u003cartifactId\u003ediscord-webhooks\u003c/artifactId\u003e\n    \u003cversion\u003e%VERSION%\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Compile Yourself\n\n1. Clone repository\n1. Run `gradlew shadowJar`\n1. Use jar suffixed with `-all.jar` in `build/libs`\n\n\n# Example\n\n```java\nclass MyAppender extends AppenderBase\u003cLoggingEvent\u003e {\n    private final WebhookClient client;\n\n    @Override\n    protected void append(LoggingEvent eventObject) {\n        if (client == null)\n            return;\n        WebhookEmbedBuilder builder = new WebhookEmbedBuilder();\n        builder.setDescription(eventObject.getFormattedMessage());\n        int color = -1;\n        switch (eventObject.getLevel().toInt()) {\n            case ERROR_INT:\n                color = 0xFF0000;\n                break;\n            case INFO_INT:\n                color = 0xF8F8FF;\n                break;\n        }\n        if (color \u003e 0)\n            builder.setColor(color);\n        builder.setTimestamp(Instant.ofEpochMilli(eventObject.getTimeStamp()));\n        client.send(builder.build());\n    }\n}\n```\n\n\u003e This is an example implementation of an Appender for logback-classic\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Java"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMinnDevelopment%2Fdiscord-webhooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMinnDevelopment%2Fdiscord-webhooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMinnDevelopment%2Fdiscord-webhooks/lists"}