Skip to content

What do I need to get started?

  1. Set up your project:

  2. Set up JDA

  3. Once you have your project you will need an additional dependency for your AudioSendHandler
    • If you don't want to implement it yourself, use LavaPlayer

Connecting to a VoiceChannel

  1. Getting a VoiceChannel (guild references an instance of Guild)
  2. Retrieve the AudioManager
    AudioManager audioManager = guild.getAudioManager();
  3. Open an audio connection audioManager.openAudioConnection()
    audioManager.openAudioConnection(myChannel);

Note

It may be important to do certain permission checks before trying to open an audio connection! It may result in a PermissionException throw otherwise!

Sending Audio to an Open Audio Connection

Note

For LavaPlayer read here

  1. Retrieve the AudioManager
    AudioManager audioManager = guild.getAudioManager();
  2. Create a new AudioSendHandler instance for your implementation.
  3. Register your AudioSendHandler: audioManager.setSendingHandler(myAudioSendHandler)
    You may only use one AudioSendHandler per Guild and not use the same instance on another Guild!
    Doing that will result in speedup due to multiple send threads pulling from the same instance!

A Working Example

public class MusicBot extends ListenerAdapter 
{
    public static void main(String[] args)
    throws IllegalArgumentException, LoginException, RateLimitedException
    {
        JDABuilder.createDefault(args[0]) // Use token provided as JVM argument
            .addEventListeners(new MusicBot()) // Register new MusicBot instance as EventListener
            .build(); // Build JDA - connect to discord
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event) 
    {
        // Make sure we only respond to events that occur in a guild
        if (!event.isFromGuild()) return;
        // This makes sure we only execute our code when someone sends a message with "!play"
        if (!event.getMessage().getContentRaw().startsWith("!play")) return;
        // Now we want to exclude messages from bots since we want to avoid command loops in chat!
        // this will include own messages as well for bot accounts
        // if this is not a bot make sure to check if this message is sent by yourself!
        if (event.getAuthor().isBot()) return;
        Guild guild = event.getGuild();
        // This will get the first voice channel with the name "music"
        // matching by voiceChannel.getName().equalsIgnoreCase("music")
        VoiceChannel channel = guild.getVoiceChannelsByName("music", true).get(0);
        AudioManager manager = guild.getAudioManager();

        // MySendHandler should be your AudioSendHandler implementation
        manager.setSendingHandler(new MySendHandler());
        // Here we finally connect to the target voice channel 
        // and it will automatically start pulling the audio from the MySendHandler instance
        manager.openAudioConnection(channel);
    }
}

Important

This example expects you to have your own AudioSendHandler implementation.
It is crucial you only use one AudioSendHandler per Guild!

Using LavaPlayer

  1. Set up LavaPlayer
  2. Implement an AudioSendHandler
  3. Connect to a voice channel
  4. Register your AudioSendHandler
  5. Use the LavaPlayer resources: How To Use LavaPlayer

More example implementations can be found in existing bots like: