Compare commits

..

No commits in common. "ed610ebdaec3398e5df75f60e95359945b69c28b" and "358b4e11c8555287faf9e75cccfbf559a37abd28" have entirely different histories.

2 changed files with 220 additions and 213 deletions

View File

@ -22,7 +22,9 @@ import javax.security.auth.login.LoginException;
import javax.sound.sampled.*; import javax.sound.sampled.*;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.audio.AudioReceiveHandler;
import net.dv8tion.jda.api.audio.AudioSendHandler; import net.dv8tion.jda.api.audio.AudioSendHandler;
import net.dv8tion.jda.api.audio.CombinedAudio;
import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
@ -38,34 +40,34 @@ import net.dv8tion.jda.api.utils.cache.CacheFlag;
public class Main extends ListenerAdapter public class Main extends ListenerAdapter
{ {
/** /**
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) throws LoginException public static void main(String[] args) throws LoginException
{ {
// We only need 2 gateway intents enabled for this example: // We only need 2 gateway intents enabled for this example:
EnumSet<GatewayIntent> intents = EnumSet.of( EnumSet<GatewayIntent> intents = EnumSet.of(
// We need messages in guilds to accept commands from users // We need messages in guilds to accept commands from users
GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_MESSAGES,
// We need voice states to connect to the voice channel // We need voice states to connect to the voice channel
GatewayIntent.GUILD_VOICE_STATES GatewayIntent.GUILD_VOICE_STATES
); );
JDABuilder builder = JDABuilder.createDefault(args[0], intents); JDABuilder builder = JDABuilder.createDefault(args[0], intents);
// Disable parts of the cache // Disable parts of the cache
builder.disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE); builder.disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE);
// Enable the bulk delete event // Enable the bulk delete event
builder.setBulkDeleteSplittingEnabled(false); builder.setBulkDeleteSplittingEnabled(false);
// Disable compression (not recommended) // Disable compression (not recommended)
builder.setCompression(Compression.NONE); builder.setCompression(Compression.NONE);
// Set activity (like "playing Something") // Set activity (like "playing Something")
builder.setActivity(Activity.playing("music!")); builder.setActivity(Activity.playing("music!"));
builder.addEventListeners(new Main()); builder.addEventListeners(new Main());
JDA jda = builder.build(); JDA jda = builder.build();
} }
@Override @Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) public void onGuildMessageReceived(GuildMessageReceivedEvent event)
@ -83,20 +85,23 @@ public class Main extends ListenerAdapter
{ {
String arg = content.substring("!join ".length()); String arg = content.substring("!join ".length());
onJoinCommand(event, guild, arg); onJoinCommand(event, guild, arg);
} else if (content.equals("!join"))
onJoinCommand(event);
else if (content.startsWith("!play "))
{
String arg = content.substring("!join ".length());
onPlayCommand(event, guild, arg);
} }
else if (content.equals("!join"))
{
onJoinCommand(event);
}
else if (content.startsWith("!play "))
{
String arg = content.substring("!join ".length());
onPlayCommand(event, guild, arg);
}
} }
/** /**
* Handle command without arguments. * Handle command without arguments.
* *
* @param event * @param event
* The event for this command * The event for this command
*/ */
private void onJoinCommand(GuildMessageReceivedEvent event) private void onJoinCommand(GuildMessageReceivedEvent event)
{ {
@ -108,27 +113,31 @@ public class Main extends ListenerAdapter
{ {
connectTo(channel); // Join the channel of the user connectTo(channel); // Join the channel of the user
onConnecting(channel, event.getChannel()); // Tell the user about our success onConnecting(channel, event.getChannel()); // Tell the user about our success
} else }
else
{
onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure
}
} }
/** /**
* Handle command with arguments. * Handle command with arguments.
* *
* @param event * @param event
* The event for this command * The event for this command
* @param guild * @param guild
* The guild where its happening * The guild where its happening
* @param arg * @param arg
* The input argument * The input argument
*/ */
private void onJoinCommand(GuildMessageReceivedEvent event, Guild guild, String arg) private void onJoinCommand(GuildMessageReceivedEvent event, Guild guild, String arg)
{ {
boolean isNumber = arg.matches("\\d+"); // This is a regular expression that ensures the input consists of digits boolean isNumber = arg.matches("\\d+"); // This is a regular expression that ensures the input consists of digits
VoiceChannel channel = null; VoiceChannel channel = null;
if (isNumber) // The input is an id? if (isNumber) // The input is an id?
{
channel = guild.getVoiceChannelById(arg); channel = guild.getVoiceChannelById(arg);
}
if (channel == null) // Then the input must be a name? if (channel == null) // Then the input must be a name?
{ {
List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg, true); List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg, true);
@ -146,47 +155,48 @@ public class Main extends ListenerAdapter
onConnecting(channel, textChannel); // Let the user know, we were successful! onConnecting(channel, textChannel); // Let the user know, we were successful!
} }
private void onPlayCommand(GuildMessageReceivedEvent event, Guild guild, String arg) private void onPlayCommand(GuildMessageReceivedEvent event, Guild guild, String arg)
{ {
try try
{ {
event.getChannel().sendMessage("Downloading ...").queue(); event.getChannel().sendMessage("Downloading ...").queue();
String destination = downloadSong(arg); String destination = downloadSong(arg);
musicHandler.addSong(new File(destination)); musicHandler.addSong(new File(destination));
event.getChannel().sendMessage("Downloaded and added to queue!").queue(); event.getChannel().sendMessage("Downloaded and added to queue!").queue();
} catch (IOException | InterruptedException | RuntimeException ex) } catch (IOException | InterruptedException | RuntimeException ex)
{ {
event.getChannel().sendMessage("Failed to download! Reason: " + ex.getMessage()).queue(); event.getChannel().sendMessage("Failed to download! Reason: " + ex.getMessage()).queue();
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} }
} }
private String downloadSong(String arg) throws IOException, RuntimeException, InterruptedException private String downloadSong(String arg) throws IOException, RuntimeException, InterruptedException
{ {
String cmd = "/usr/bin/youtube-dl -x --audio-format=wav " + arg; String cmd = "/usr/bin/youtube-dl -x --audio-format=wav "+arg;
System.out.println("Running command: " + cmd); System.out.println("Running command: " + cmd);
// Process exec = Runtime.getRuntime().exec().split(" ")); // Process exec = Runtime.getRuntime().exec().split(" "));
Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).start(); Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).start();
exec.waitFor(10000, TimeUnit.MILLISECONDS); exec.waitFor(10000, TimeUnit.MILLISECONDS);
InputStream in = exec.getInputStream(); InputStream in = exec.getInputStream();
String output = new String(in.readAllBytes(), Charset.defaultCharset()); String output = new String(in.readAllBytes(), Charset.defaultCharset());
System.out.println(output); System.out.println(output);
if (exec.exitValue() != 0) if (exec.exitValue() != 0)
throw new RuntimeException("youtube-dl failed with error " + exec.exitValue()); throw new RuntimeException("youtube-dl failed with error " + exec.exitValue());
Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output); Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output);
matcher.find(); matcher.find();
String destination = matcher.group(1); String destination = matcher.group(1);
return destination; return destination;
} }
/** /**
* Inform user about successful connection. * Inform user about successful connection.
* *
* @param channel * @param channel
* The voice channel we connected to * The voice channel we connected to
* @param textChannel * @param textChannel
* The text channel to send the message in * The text channel to send the message in
*/ */
private void onConnecting(VoiceChannel channel, TextChannel textChannel) private void onConnecting(VoiceChannel channel, TextChannel textChannel)
{ {
@ -197,10 +207,9 @@ public class Main extends ListenerAdapter
* The channel to connect to is not known to us. * The channel to connect to is not known to us.
* *
* @param channel * @param channel
* The message channel (text channel abstraction) to send failure * The message channel (text channel abstraction) to send failure information to
* information to
* @param comment * @param comment
* The information of this channel * The information of this channel
*/ */
private void onUnknownChannel(MessageChannel channel, String comment) private void onUnknownChannel(MessageChannel channel, String comment)
{ {
@ -211,18 +220,19 @@ public class Main extends ListenerAdapter
* Connect to requested channel and start echo handler * Connect to requested channel and start echo handler
* *
* @param channel * @param channel
* The channel to connect to * The channel to connect to
*/ */
private void connectTo(VoiceChannel channel) private void connectTo(VoiceChannel channel)
{ {
Guild guild = channel.getGuild(); Guild guild = channel.getGuild();
// Get an audio manager for this guild, this will be created upon first use for each guild // Get an audio manager for this guild, this will be created upon first use for each guild
AudioManager audioManager = guild.getAudioManager(); AudioManager audioManager = guild.getAudioManager();
musicHandler = new MusicHandler(); musicHandler = new MusicHandler();
// Create our Send/Receive handler for the audio connection // Create our Send/Receive handler for the audio connection
// EchoHandler handler = new EchoHandler(); // EchoHandler handler = new EchoHandler();
// The order of the following instructions does not matter! // The order of the following instructions does not matter!
// Set the sending handler to our echo system // Set the sending handler to our echo system
audioManager.setSendingHandler(musicHandler); audioManager.setSendingHandler(musicHandler);
// Set the receiving handler to the same echo system, otherwise we can't echo anything // Set the receiving handler to the same echo system, otherwise we can't echo anything
@ -230,6 +240,143 @@ public class Main extends ListenerAdapter
// Connect to the voice channel // Connect to the voice channel
audioManager.openAudioConnection(channel); audioManager.openAudioConnection(channel);
} }
private MusicHandler musicHandler; private MusicHandler musicHandler;
public static class MusicHandler implements AudioSendHandler, Closeable
{
private final Queue<File> songQueue = new ConcurrentLinkedQueue<>();
private File currentSong;
private AudioInputStream din = null;
private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
private int byteCount;
public MusicHandler()
{
// load whatever songs we have in WAV format
// File folder = new File("/home/jimj316/Music/");
// List<File> wavs = Arrays.asList(folder.listFiles((file, string) ->
// {
// return string.endsWith(".wav");
// }));
// Collections.shuffle(wavs);
// songQueue.addAll(wavs);
// nextSong();
}
public void addSong(File file)
{
System.out.println("Song added to queue: " + file.getAbsolutePath());
songQueue.add(file);
if (!canProvide())
nextSong();
}
private boolean nextSong()
{
AudioInputStream in = null;
try
{
if (din != null)
{
din.close();
din = null;
}
if (currentSong != null)
{
currentSong.delete();
currentSong = null;
}
currentSong = songQueue.poll();
if (currentSong == null)
return false;
System.out.println("Playing song " + currentSong.getAbsolutePath());
in = AudioSystem.getAudioInputStream(currentSong);
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
din = AudioSystem.getAudioInputStream(decodedFormat, in);
byteCount = 3840;
while (queue.size() < 500)
{
if (!readData())
break;
}
System.out.println("Queue filled to " + queue.size());
return true;
} catch (UnsupportedAudioFileException | IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
}
return false;
}
@Override
public boolean canProvide()
{
// If we have something in our buffer we can provide it to the send system
return !queue.isEmpty();
}
@Override
public ByteBuffer provide20MsAudio()
{
// use what we have in our buffer to send audio as PCM
while (queue.size() < 500)
{
if (!readData())
{
if (!nextSong())
break;
}
}
byte[] data = queue.poll();
return data == null ? null : ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
}
private boolean readData()
{
if (din == null)
return false;
try
{
// if (din.available() == 0)
// return false;
int bytesToRead = byteCount;
if (din.available() > 0 && din.available() < bytesToRead)
bytesToRead = din.available();
byte[] bytes = new byte[bytesToRead];
// byte[] bytes = din.readNBytes(bytesToRead);
int read = din.read(bytes);
if (read < 0)
return false;
queue.add(bytes);
} catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return true;
}
@Override
public boolean isOpus()
{
return false; //To change body of generated methods, choose Tools | Templates.
}
@Override
public void close() throws IOException
{
din.close();
}
}
} }

View File

@ -1,140 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package moe.nekojimi.chords;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import net.dv8tion.jda.api.audio.AudioSendHandler;
/**
*
* @author jimj316
*/
public class MusicHandler implements AudioSendHandler, Closeable
{
private final Queue<File> songQueue = new ConcurrentLinkedQueue<>();
private File currentSong;
private AudioInputStream din = null;
private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
private int byteCount;
public MusicHandler()
{
}
public void addSong(File file)
{
System.out.println("Song added to queue: " + file.getAbsolutePath());
songQueue.add(file);
if (!canProvide())
nextSong();
}
private boolean nextSong()
{
AudioInputStream in = null;
try
{
if (din != null)
{
din.close();
din = null;
}
if (currentSong != null)
{
currentSong.delete();
currentSong = null;
}
currentSong = songQueue.poll();
if (currentSong == null)
return false;
System.out.println("Playing song " + currentSong.getAbsolutePath());
in = AudioSystem.getAudioInputStream(currentSong);
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
din = AudioSystem.getAudioInputStream(decodedFormat, in);
byteCount = 3840;
while (queue.size() < 500)
if (!readData())
break;
System.out.println("Queue filled to " + queue.size());
return true;
} catch (UnsupportedAudioFileException | IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
}
return false;
}
@Override
public boolean canProvide()
{
// If we have something in our buffer we can provide it to the send system
return !queue.isEmpty();
}
@Override
public ByteBuffer provide20MsAudio()
{
// use what we have in our buffer to send audio as PCM
while (queue.size() < 500)
if (!readData())
if (!nextSong())
break;
byte[] data = queue.poll();
return data == null ? null : ByteBuffer.wrap(data); // Wrap this in a java.nio.ByteBuffer
}
private boolean readData()
{
if (din == null)
return false;
try
{
// if (din.available() == 0)
// return false;
int bytesToRead = byteCount;
if (din.available() > 0 && din.available() < bytesToRead)
bytesToRead = din.available();
byte[] bytes = new byte[bytesToRead];
// byte[] bytes = din.readNBytes(bytesToRead);
int read = din.read(bytes);
if (read < 0)
return false;
queue.add(bytes);
} catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return true;
}
@Override
public boolean isOpus()
{
return false; //To change body of generated methods, choose Tools | Templates.
}
@Override
public void close() throws IOException
{
din.close();
}
}