Skip to content

4) Making a Music Bot

Florian Spieß edited this page Jul 31, 2022 · 19 revisions

This wiki page is migrating to jda.wiki/using-jda/making-a-music-bot


What do I need to get started?

  1. Setup your project
  2. Setup 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

  1. Retrieve the AudioManager
    AudioManager audioManager = guild.getAudioManager();
  2. Create a new AudioSendHandler instance for your implementation. (Note: For LavaPlayer read here)
  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
    }

    // Note that we are using GuildMessageReceivedEvent to only include messages from a Guild!
    @Override
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) 
    {
        // 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);
    }
}

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

Using LavaPlayer

  1. Setup LavaPlayer guide
  2. Implement an AudioSendHandler -> Example
  3. Connect to a voice channel how?
  4. Register your AudioSendHandler how?
  5. Use the LavaPlayer resources How To Use LavaPlayer

More example implementations can be found in existing bots like: