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

JDA

> 编程语言
开源

Java 封装的流行聊天和 VOIP 服务: Discord https://discord.com

4.7K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Java 封装的流行聊天和 VOIP 服务: Discord https://discord.com

JDA (Java Discord API)

This open source library is intended for implementing bots on Discord using the real-time gateway and REST API. It provides event based functionality to implement bots of any kind, allowing for effective and scalable applications.

Overview

The core concepts of JDA have been developed to make building scalable apps easy:

  1. Event System
    Providing simplified events from the gateway API, to respond to any platform events in real-time without much hassle.
  2. Rest Actions
    Easy to use and scalable implementation of REST API functionality, letting you choose between callbacks with combinators, futures, and blocking. The library also handles rate-limits imposed by Discord automatically, while still offering ways to replace the default implementation.
  3. Customizable Cache
    Trading memory usage for better performance where necessary, with sane default presets to choose from and customize.

You can learn more by visiting our wiki or referencing our Javadocs.

Installation

This library is available on maven central. The latest version is always shown in the GitHub Release.

The minimum java version supported by JDA is Java SE 8. JDA also uses JSR 305 to support solid interoperability with Kotlin out of the box.

[!NOTE] To use JDA for audio connections, you must also add a dependency that implements the DAVE Protocol. See Making a Music Bot for details.

Gradle

…

Maven

<dependency>
    <groupId>net.dv8tion</groupId>
    <artifactId>JDA</artifactId>
    <version>$version</version> 
    
    <exclusions>
        
        
    </exclusions>
</dependency>

Creating a Bot

To use this library, you have to create an Application in the Discord Application Dashboard and grab your bot token. You can find a step-by-step guide for this in our wiki page Creating a Discord Bot.

‍♂️ Getting Started

We provide a number of examples to introduce you to JDA. You can also take a look at our official Wiki, Documentation, and FAQ.

Every bot implemented by JDA starts out using the JDABuilder or DefaultShardManagerBuilder. Both builders provide a set of default presets for cache usage and events it wants to receive:

  • createDefault - Enables cache for users who are active in voice channels and all cache flags
  • createLight - Disables all user cache and cache flags
  • create - Enables member chunking, caches all users, and enables all cache flags

We recommend reading the guide on caching and intents to get a feel for configuring your bot properly. Here are some possible use-cases:

Example: Message Logging

[!NOTE] The following example makes use of the privileged intent GatewayIntent.MESSAGE_CONTENT, which must be explicitly enabled in your application dashboard. You can find out more about intents in our wiki guide.

Simply logging messages to the console. Making use of JDABuilder, the intended entry point for smaller bots that don't intend to grow to thousands of guilds.

Starting your bot and attaching an event listener, using the right intents:

public static void main(String[] args) {
  JDABuilder.createLight(token, EnumSet.of(GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT))
      .addEventListeners(new MessageReceiveListener())
      .build();
}

Your event listener could look like this:

public class MessageReceiveListener extends ListenerAdapter {
  @Override
  public void onMessageReceived(MessageReceivedEvent event) {
    System.out.printf("[%s] %#s: %s\n",
      event.getChannel(),
      event.getAuthor(),
      event.getMessage().getContentDisplay());
  }
}

You can find a more thorough example with the MessageLoggerExample class.

Example: Slash Command Bot

This is a bot that makes use of interactions to respond to user commands. Unlike the message logging bot, this bot can work without any enabled intents, since interactions are always available.

…

An event listener that responds to commands could look like this:

…

You can find a more thorough example with the SlashBotExample class.

RestAction

In this library, the RestAction interface is used as a request builder for all API endpoints. This interface represents a lazy request builder, as shown in this simple example:

channel.sendMessage("Hello Friend!")
  .addFiles(FileUpload.fromData(greetImage)) // Chain builder methods to configure the request
  .queue() // Send the request asynchronously

[!IMPORTANT] The final call to queue() sends the request. You can also send the request synchronously or using futures, check out our extended guide in the RestAction Wiki.

The RestAction interface also supports a number of operators to avoid callback hell:

  • map
    Convert the result of the RestAction to a different value
  • flatMap
    Chain another RestAction on the result
  • delay
    Delay the element of the previous step

As well as combinators like:

  • and
    Require another RestAction to complete successfully, running in parallel
  • allOf
    Accumulate a list of many actions into one (see also mapToResult)
  • zip
    Similar to and, but combines the results into a list

And configurators like:

  • timeout and deadline
    Configure how long the action is allowed to be in queue, cancelling if it takes too long
  • setCheck
    Running some checks right before the request is sent, this can be helpful when it is in queue for a while
  • reason
    The audit log reason for an action

Example:

public RestAction<Void> selfDestruct(MessageChannel channel, String content) {
    return channel.sendMessage("The following message will destroy itself in 1 minute!")
        .addComponents(ActionRow.of(Button.danger("delete", "Delete now"))) // further amend message before sending
        .delay(10, SECONDS, scheduler) // after sending, wait 10 seconds
        .flatMap((it) -> it.editMessage(content)) // then edit the message
        .delay(1, MINUTES, scheduler) // wait another minute
        .flatMap(Message::delete); // then delete
}

This could then be used in code:

selfDestruct(channel, "Hello friend, this is my secret message").queue();

Extensions

jda-ktx

Created and maintained by MinnDevelopment.
Provides Kotlin extensions for RestAction and events that provide a more idiomatic Kotlin experience.

fun main() {
    val jda = light(BOT_TOKEN)
    
    jda.onCommand("ping") { event ->
        val time = measureTime {
            event.reply("Pong!").await() // suspending
        }.inWholeMilliseconds

        event.hook.editOriginal("Pong: $time ms").queue()
    }
}

There are a number of examples available in the README.

Lavaplayer

Created by sedmelluq and now maintained by the lavalink community
Lavaplayer is the most popular library used by Music Bots created in Java. It is highly compatible with JDA and Discord4J and allows playing audio from YouTube, Soundcloud, Twitch, Bandcamp and more providers.
The library can easily be expanded to more services by implementing your own AudioSourceManager and registering it. We recommend to also use udpqueue in addition to lavaplayer, to avoid stuttering issues caused by GC pauses.

It is recommended to read the Usage section of Lavaplayer to understand a proper implementation.
Sedmelluq provided a demo in his repository which presents an example implementation for JDA: https://github.com/lavalink-devs/lavaplayer/tree/master/demo-jda

udpqueue (an extension of jda-nas)

Created and maintain

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Javaapiapi-wrapperbotbot-api

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

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言