百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

discord-webhooks

> 包管理
开源

为 Discord Webhook API 提供易于使用的绑定

195 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

为 Discord Webhook API 提供易于使用的绑定

version license

Discord-Webhooks

Originally part of JDA, this library provides easy to use bindings for the Discord Webhook API.

Introduction

Here we will give a small overview of the proper usage and applicability of the resources provided by this library.

Documentation is available via the GitHub pages on this repository: Javadoc

Limitations

Webhooks 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.

Getting Started

The 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.

Creating a WebhookClient

java
// Using the builder
WebhookClientBuilder builder = new WebhookClientBuilder(url); // or id, token
builder.setThreadFactory((job) -> {
    Thread thread = new Thread(job);
    thread.setName("Hello");
    thread.setDaemon(true);
    return thread;
});
builder.setWait(true);
WebhookClient client = builder.build();
java
// Using the factory methods
WebhookClient client = WebhookClient.withUrl(url); // or withId(id, token)

Creating a WebhookCluster

java
// Create and initialize the cluster
WebhookCluster cluster = new WebhookCluster(5); // create an initial 5 slots (dynamic like lists)
cluster.setDefaultHttpClient(new OkHttpClient());
cluster.setDefaultDaemon(true);

// Create a webhook client
cluster.buildWebhook(id, token);

// Add an existing webhook client
cluster.addWebhook(client);

Sending Messages

Sending 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.

…

Threads

You can use the webhook clients provided by this library to send messages in threads. There are two ways to accomplish this.

Set a thread id in the client builder to send all messages in that client to the thread:

java
WebhookClient client = new WebhookClientBuilder(url)
        .setThreadId(threadId)
        .build();

client.send("Hello"); // appears in the thread

Use onThread to create a client with a thread id and all other settings inherited:

java
try (WebhookClient client = WebhookClient.withUrl(url)) {
    WebhookClient thread = client.onThread(123L);
    thread.send("Hello"); // appears only in the thread with id 123
    client.send("Friend"); // appears in the channel instead
} // calls client.close() which automatically also closes all onThread clients as well.

All 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.

java
WebhookClient thread = null;
try (WebhookClient client = WebhookClient.withUrl(url)) {
    thread = client.onThread(id);
} // closes client
thread.send("Hello"); //  {
    System.err.printf("[%s] %s%n", client.getId(), message);
    if (throwable != null)
        throwable.printStackTrace();
    // Shutdown the webhook client when you get 404 response (may also trigger for client#edit calls, be careful)
    if (throwable instanceof HttpException ex && ex.getCode() == 404) {
        client.close();
    }
});

External Libraries

This library also supports sending webhook messages with integration from other libraries such as

  • JDA (version 5.0.0-beta.12) with JDAWebhookClient
  • Discord4J (version 3.2.5) with D4JWebhookClient
  • Javacord (version 3.8.0) with JavacordWebhookClient

Example JDA

java
public void sendWebhook(Webhook webhook) {
    MessageCreateData message = new MessageCreateBuilder().setContent("Hello World").build();
    try (JDAWebhookClient client = JDAWebhookClient.from(webhook)) { // create a client instance from the JDA webhook
        client.send(message); // send a JDA message instance
    }
}

Example Discord4J

java
public void sendWebhook(Webhook webhook) {
    try (D4JWebhookClient client = D4JWebhookClient.from(webhook)) {
        client.send(MessageCreateSpec.create()
            .withContent("Hello World")
            .addFile("cat.png", new FileInputStream("cat.png"))
        );
    }
}

Download

version

Note: Replace %VERSION% below with the desired version.

Gradle

gradle
repositories {
    mavenCentral()
}
gradle
dependencies {
    implementation("club.minnced:discord-webhooks:%VERSION%")
}

Maven

xml

    club.minnced
    discord-webhooks
    %VERSION%

Compile Yourself

  1. Clone repository
  2. Run gradlew shadowJar
  3. Use jar suffixed with -all.jar in build/libs

Example

…

This is an example implementation of an Appender for logback-classic

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javadiscordgradlehacktoberfestjava

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类包管理
定价开源

> 相关工具

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image