Skip to content

Instantly share code, notes, and snippets.

@DV8FromTheWorld
Last active December 24, 2018 08:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DV8FromTheWorld/fbcefb22935c23f64826b343c9ea3d9b to your computer and use it in GitHub Desktop.
Save DV8FromTheWorld/fbcefb22935c23f64826b343c9ea3d9b to your computer and use it in GitHub Desktop.
Simple Audio bridge written with JDA's new audio receiving system.
/*
* Copyright 2016 Austin Keener
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import net.dv8tion.jda.audio.AudioReceiveHandler;
import net.dv8tion.jda.audio.AudioSendHandler;
import net.dv8tion.jda.audio.CombinedAudio;
import net.dv8tion.jda.audio.UserAudio;
import net.dv8tion.jda.entities.User;
import java.util.concurrent.ConcurrentLinkedQueue;
public class AudioBridge implements AudioReceiveHandler, AudioSendHandler
{
double volume = 1.0;
ConcurrentLinkedQueue<byte[]> bridgeQueue = new ConcurrentLinkedQueue<>();
@Override
public boolean canReceiveCombined()
{
return true;
}
@Override
public boolean canReceiveUser()
{
return false;
}
@Override
public void handleCombinedAudio(CombinedAudio combinedAudio)
{
bridgeQueue.add(combinedAudio.getAudioData(volume));
}
@Override
public void handleUserAudio(UserAudio userAudio)
{
}
@Override
public void handleUserTalking(User user, boolean talking)
{
}
@Override
public boolean canProvide()
{
return !bridgeQueue.isEmpty();
}
@Override
public byte[] provide20MsAudio()
{
return bridgeQueue.poll();
}
}
/*
* Copyright 2016 Austin Keener
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import net.dv8tion.jda.JDA;
import net.dv8tion.jda.JDABuilder;
import net.dv8tion.jda.entities.Guild;
import net.dv8tion.jda.entities.VoiceChannel;
import net.dv8tion.jda.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.hooks.ListenerAdapter;
import net.dv8tion.jda.managers.AudioManager;
import javax.security.auth.login.LoginException;
public class BridgeBotExample extends ListenerAdapter
{
AudioManager currentTo = null;
AudioManager currentFrom = null;
AudioBridge bridge = new AudioBridge();
public static void main(String[] args) throws LoginException, InterruptedException
{
JDA api = new JDABuilder()
.setBotToken("BOT_LOGIN_TOKEN_HERE")
.addListener(new BridgeBotExample())
.buildBlocking();
}
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event)
{
//DV8FromTheWorld is the only one who can control this bot. Change/comment if you want to control!
if (!event.getAuthor().getId().equals("107562988810027008"))
return;
String msg = event.getMessage().getContent();
//Expects command format: .bridge from NAME_OF_CHANNEL
if (msg.startsWith(".bridge from "))
{
String chanName = msg.substring(".bridge from ".length());
VoiceChannel chan = getChannelWithName(event.getGuild(), chanName);
if (chan == null)
{
event.getChannel().sendMessage("There isn't a VoiceChannel in this Guild with the name: '" + chanName + "'");
return;
}
if (currentFrom != null)
{
currentFrom.setSendingHandler(null);
currentFrom.closeAudioConnection();
}
currentFrom = event.getGuild().getAudioManager();
currentFrom.openAudioConnection(chan);
currentFrom.setReceivingHandler(bridge);
}
//Expects command format: .bridge to NAME_OF_CHANNEL
if (msg.startsWith(".bridge to "))
{
String chanName = msg.substring(".bridge to ".length());
VoiceChannel chan = getChannelWithName(event.getGuild(), chanName);
if (chan == null)
{
event.getChannel().sendMessage("There isn't a VoiceChannel in this Guild with the name: '" + chanName + "'");
return;
}
if (currentTo != null)
{
currentTo.setSendingHandler(null);
currentTo.closeAudioConnection();
}
currentTo = event.getGuild().getAudioManager();
currentTo.openAudioConnection(chan);
currentTo.setSendingHandler(bridge);
}
}
public VoiceChannel getChannelWithName(Guild guild, String chanName)
{
//Scans through the VoiceChannels in this Guild, looking for one with a case-insensitive matching name.
VoiceChannel channel = guild.getVoiceChannels().stream().filter(
vChan -> vChan.getName().equalsIgnoreCase(chanName))
.findFirst().orElse(null); //If there isn't a matching name, return null.
return channel;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment