What do I need to get started?¶
-
Set up your project:
- 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¶
- Getting a VoiceChannel (
guild
references an instance ofGuild
)- By the channel id:
guild.getVoiceChannelById(CHANNEL_ID)
VoiceChannel myChannel = guild.getVoiceChannelById(CHANNEL_ID);
- By the channel name:
guild.getVoiceChannelsByName(CHANNEL_NAME, true)
VoiceChannel myChannel = guild.getVoiceChannelsByName(CHANNEL_NAME, true).get(0);
- By the voice state of a member
member.getVoiceState().getChannel()
VoiceChannel myChannel = member.getVoiceState().getChannel();
- By the channel id:
- Retrieve the
AudioManager
AudioManager audioManager = guild.getAudioManager();
- 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
- Retrieve the
AudioManager
AudioManager audioManager = guild.getAudioManager();
- Create a new AudioSendHandler instance for your implementation.
- 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¶
- Set up LavaPlayer
- Implement an AudioSendHandler
- Connect to a voice channel
- Register your AudioSendHandler
- Use the LavaPlayer resources: How To Use LavaPlayer
More example implementations can be found in existing bots like:¶
- AudioEchoExample
- Clarity by @jagrosh
- FredBoat by @freyacodes