|
|
|
@ -12,6 +12,8 @@ import java.util.*; |
|
|
|
|
import java.util.concurrent.CompletableFuture; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
import javax.security.auth.login.LoginException; |
|
|
|
|
import moe.nekojimi.chords.commands.Command; |
|
|
|
|
import moe.nekojimi.chords.commands.JoinCommand; |
|
|
|
|
import moe.nekojimi.musicsearcher.Query; |
|
|
|
|
import moe.nekojimi.musicsearcher.Result; |
|
|
|
|
import moe.nekojimi.musicsearcher.providers.MetaSearcher; |
|
|
|
@ -41,6 +43,8 @@ public class Main extends ListenerAdapter |
|
|
|
|
private final Searcher searcher; |
|
|
|
|
private JDA jda; |
|
|
|
|
|
|
|
|
|
private final Map<String, Command> commands = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
private VoiceChannel currentVoiceChannel = null; |
|
|
|
|
|
|
|
|
|
private List<Result> lastSearchResults; |
|
|
|
@ -91,12 +95,27 @@ public class Main extends ListenerAdapter |
|
|
|
|
if (song.getLocation() != null) |
|
|
|
|
channel.sendMessage(/*bracketNo + */"Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue(); |
|
|
|
|
else |
|
|
|
|
channel.sendMessage(/*bracketNo + */"Now downloading " + song + " for " + song.getRequestedBy() + " ...").queue(); |
|
|
|
|
{ |
|
|
|
|
Format format = song.getBestFormat(); |
|
|
|
|
String formatDetails = ""; |
|
|
|
|
if (format != null) |
|
|
|
|
{ |
|
|
|
|
formatDetails = " (" + format.getBitrate() / 1000 + "k, " + String.format("%.2f", format.getSize() / (1024.0 * 1024.0)) + "MiB)"; |
|
|
|
|
} |
|
|
|
|
channel.sendMessage(/*bracketNo + */"Now downloading " + song + formatDetails + " for " + song.getRequestedBy() + " ...").queue(); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
channel.sendMessage(/*bracketNo + */"Failed to download " + song + " for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue(); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml")); |
|
|
|
|
|
|
|
|
|
addCommand(new JoinCommand(this)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addCommand(Command command) |
|
|
|
|
{ |
|
|
|
|
commands.put(command.getKeyword(), command); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setJda(JDA jda) |
|
|
|
@ -122,17 +141,30 @@ public class Main extends ListenerAdapter |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
String[] split = content.split("\\s+", 2); |
|
|
|
|
String cmd = split[0].toLowerCase(); |
|
|
|
|
|
|
|
|
|
if (!cmd.startsWith("!")) |
|
|
|
|
return; // doesn't start with prefix char
|
|
|
|
|
|
|
|
|
|
cmd = cmd.substring(1); // strip prefix char
|
|
|
|
|
|
|
|
|
|
String arg = ""; |
|
|
|
|
if (content.contains(" ")) |
|
|
|
|
arg = content.split(" ", 2)[1]; |
|
|
|
|
if (split.length > 1) |
|
|
|
|
arg = split[1]; |
|
|
|
|
|
|
|
|
|
if (commands.containsKey(cmd)) |
|
|
|
|
{ |
|
|
|
|
Command command = commands.get(cmd); |
|
|
|
|
command.call(event, List.of(arg)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (content.startsWith("!join ")) |
|
|
|
|
onJoinCommand(event, guild, arg); |
|
|
|
|
// else if (content.startsWith("!join "))
|
|
|
|
|
// onJoinCommand(event, guild, arg);
|
|
|
|
|
else if (content.equals("!leave")) |
|
|
|
|
onLeaveCommand(event); |
|
|
|
|
else if (content.equals("!join")) |
|
|
|
|
onJoinCommand(event); |
|
|
|
|
// else if (content.equals("!join"))
|
|
|
|
|
// onJoinCommand(event);
|
|
|
|
|
else if (content.startsWith("!play ")) |
|
|
|
|
onPlayCommand(event, guild, arg); |
|
|
|
|
else if (content.startsWith("!queue")) |
|
|
|
@ -157,20 +189,20 @@ public class Main extends ListenerAdapter |
|
|
|
|
* @param event |
|
|
|
|
* The event for this command |
|
|
|
|
*/ |
|
|
|
|
private void onJoinCommand(GuildMessageReceivedEvent event) |
|
|
|
|
{ |
|
|
|
|
// Note: None of these can be null due to our configuration with the JDABuilder!
|
|
|
|
|
Member member = event.getMember(); // Member is the context of the user for the specific guild, containing voice state and roles
|
|
|
|
|
GuildVoiceState voiceState = member.getVoiceState(); // Check the current voice state of the user
|
|
|
|
|
VoiceChannel channel = voiceState.getChannel(); // Use the channel the user is currently connected to
|
|
|
|
|
// if (channel != null)
|
|
|
|
|
// private void onJoinCommand(GuildMessageReceivedEvent event)
|
|
|
|
|
// {
|
|
|
|
|
// connectTo(channel); // Join the channel of the user
|
|
|
|
|
// onConnecting(channel, event.getChannel()); // Tell the user about our success
|
|
|
|
|
// } else
|
|
|
|
|
// onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure
|
|
|
|
|
onJoinCommand(event, member.getGuild(), channel.getName()); |
|
|
|
|
} |
|
|
|
|
// // Note: None of these can be null due to our configuration with the JDABuilder!
|
|
|
|
|
// Member member = event.getMember(); // Member is the context of the user for the specific guild, containing voice state and roles
|
|
|
|
|
// GuildVoiceState voiceState = member.getVoiceState(); // Check the current voice state of the user
|
|
|
|
|
// VoiceChannel channel = voiceState.getChannel(); // Use the channel the user is currently connected to
|
|
|
|
|
//// if (channel != null)
|
|
|
|
|
//// {
|
|
|
|
|
//// connectTo(channel); // Join the channel of the user
|
|
|
|
|
//// onConnecting(channel, event.getChannel()); // Tell the user about our success
|
|
|
|
|
//// } else
|
|
|
|
|
//// onUnknownChannel(event.getChannel(), "your voice channel"); // Tell the user about our failure
|
|
|
|
|
// onJoinCommand(event, member.getGuild(), channel.getName());
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Handle command with arguments. |
|
|
|
@ -182,35 +214,34 @@ public class Main extends ListenerAdapter |
|
|
|
|
* @param arg |
|
|
|
|
* The input argument |
|
|
|
|
*/ |
|
|
|
|
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
|
|
|
|
|
VoiceChannel channel = null; |
|
|
|
|
if (isNumber) // The input is an id?
|
|
|
|
|
channel = guild.getVoiceChannelById(arg); |
|
|
|
|
if (channel == null) // Then the input must be a name?
|
|
|
|
|
{ |
|
|
|
|
List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg, true); |
|
|
|
|
if (!channels.isEmpty()) // Make sure we found at least one exact match
|
|
|
|
|
channel = channels.get(0); // We found a channel! This cannot be null.
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TextChannel textChannel = event.getChannel(); |
|
|
|
|
if (channel == null) // I have no idea what you want mr user
|
|
|
|
|
{ |
|
|
|
|
onUnknownChannel(textChannel, arg); // Let the user know about our failure
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
connectTo(channel); // We found a channel to connect to!
|
|
|
|
|
onConnecting(channel, textChannel); // Let the user know, we were successful!
|
|
|
|
|
} |
|
|
|
|
// 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
|
|
|
|
|
// VoiceChannel channel = null;
|
|
|
|
|
// if (isNumber) // The input is an id?
|
|
|
|
|
// channel = guild.getVoiceChannelById(arg);
|
|
|
|
|
// if (channel == null) // Then the input must be a name?
|
|
|
|
|
// {
|
|
|
|
|
// List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg, true);
|
|
|
|
|
// if (!channels.isEmpty()) // Make sure we found at least one exact match
|
|
|
|
|
// channel = channels.get(0); // We found a channel! This cannot be null.
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// TextChannel textChannel = event.getChannel();
|
|
|
|
|
// if (channel == null) // I have no idea what you want mr user
|
|
|
|
|
// {
|
|
|
|
|
// onUnknownChannel(textChannel, arg); // Let the user know about our failure
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// connectTo(channel); // We found a channel to connect to!
|
|
|
|
|
// onConnecting(channel, textChannel); // Let the user know, we were successful!
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
private void onLeaveCommand(GuildMessageReceivedEvent event) |
|
|
|
|
{ |
|
|
|
|
if (currentVoiceChannel != null) |
|
|
|
|
{ |
|
|
|
|
disconnect(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -404,43 +435,19 @@ public class Main extends ListenerAdapter |
|
|
|
|
+ "!remove <Index> - Remove the song at position <Index> from the queue.\n" |
|
|
|
|
+ "!skip - Skip the current song and play the next one.\n" |
|
|
|
|
+ "!restart - Try playing the current song again in case it goes wrong.\n"; |
|
|
|
|
// for (String key: commands.keySet())
|
|
|
|
|
// {
|
|
|
|
|
// help += "!" + key + ":"
|
|
|
|
|
// }
|
|
|
|
|
event.getChannel().sendMessage(help).queue(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Inform user about successful connection. |
|
|
|
|
* |
|
|
|
|
* @param channel |
|
|
|
|
* The voice channel we connected to |
|
|
|
|
* @param textChannel |
|
|
|
|
* The text channel to send the message in |
|
|
|
|
*/ |
|
|
|
|
private void onConnecting(VoiceChannel channel, TextChannel textChannel) |
|
|
|
|
{ |
|
|
|
|
textChannel.sendMessage("Connecting to " + channel.getName()).queue(); // never forget to queue()!
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The channel to connect to is not known to us. |
|
|
|
|
* |
|
|
|
|
* @param channel |
|
|
|
|
* The message channel (text channel abstraction) to send failure |
|
|
|
|
* information to |
|
|
|
|
* @param comment |
|
|
|
|
* The information of this channel |
|
|
|
|
*/ |
|
|
|
|
private void onUnknownChannel(MessageChannel channel, String comment) |
|
|
|
|
{ |
|
|
|
|
channel.sendMessage("Unable to connect to ``" + comment + "``, no such channel!").queue(); // never forget to queue()!
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Connect to requested channel and start echo handler |
|
|
|
|
* |
|
|
|
|
* @param channel |
|
|
|
|
* The channel to connect to |
|
|
|
|
*/ |
|
|
|
|
private void connectTo(VoiceChannel channel) |
|
|
|
|
public void connectTo(VoiceChannel channel) |
|
|
|
|
{ |
|
|
|
|
Guild guild = channel.getGuild(); |
|
|
|
|
// Get an audio manager for this guild, this will be created upon first use for each guild
|
|
|
|
@ -460,7 +467,7 @@ public class Main extends ListenerAdapter |
|
|
|
|
currentVoiceChannel = channel; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void disconnect() |
|
|
|
|
public void disconnect() |
|
|
|
|
{ |
|
|
|
|
if (currentVoiceChannel != null) |
|
|
|
|
{ |
|
|
|
@ -473,4 +480,39 @@ public class Main extends ListenerAdapter |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public MusicHandler getMusicHandler() |
|
|
|
|
{ |
|
|
|
|
return musicHandler; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Downloader getDownloader() |
|
|
|
|
{ |
|
|
|
|
return downloader; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Searcher getSearcher() |
|
|
|
|
{ |
|
|
|
|
return searcher; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public JDA getJda() |
|
|
|
|
{ |
|
|
|
|
return jda; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public VoiceChannel getCurrentVoiceChannel() |
|
|
|
|
{ |
|
|
|
|
return currentVoiceChannel; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<Result> getLastSearchResults() |
|
|
|
|
{ |
|
|
|
|
return lastSearchResults; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public int getTrackNumber() |
|
|
|
|
{ |
|
|
|
|
return trackNumber; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|