diff --git a/VERSION b/VERSION index e21e727..13175fd 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.4.0 \ No newline at end of file +1.4.1 \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4ab1150..17ad525 100644 --- a/build.gradle +++ b/build.gradle @@ -62,7 +62,7 @@ dependencies { } group = 'com.casko1' -version = '1.4.0' +version = '1.4.1' description = 'wheelbarrow-bot' java.sourceCompatibility = JavaVersion.VERSION_16 diff --git a/src/main/java/com/casko1/wheelbarrow/bot/commands/events/PlaySlashCommandEvent.java b/src/main/java/com/casko1/wheelbarrow/bot/commands/events/PlaySlashCommandEvent.java index df4fcbf..5bf801e 100644 --- a/src/main/java/com/casko1/wheelbarrow/bot/commands/events/PlaySlashCommandEvent.java +++ b/src/main/java/com/casko1/wheelbarrow/bot/commands/events/PlaySlashCommandEvent.java @@ -3,45 +3,49 @@ import com.casko1.wheelbarrow.bot.commands.interfaces.PlayEvent; import com.casko1.wheelbarrow.bot.utils.ArgumentsUtil; import com.jagrosh.jdautilities.command.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; public class PlaySlashCommandEvent extends CommonSlashCommandEvent implements PlayEvent { - private final boolean isQuery; - public PlaySlashCommandEvent(SlashCommandEvent event, boolean isQuery) { + private String args; + private final boolean isUrl; + + public PlaySlashCommandEvent(SlashCommandEvent event) { super(event); - this.isQuery = isQuery; + this.args = initializeArgs(event); + this.isUrl = ArgumentsUtil.isUrl(args); + } + + private String initializeArgs(SlashCommandEvent event) { + OptionMapping argsOption = event.getOption("url-or-search"); + return argsOption == null ? "" : argsOption.getAsString(); } @Override public String getArgs() { - return event.hasOption("url") ? event.getOption("url").getAsString() : event.getOption("query").getAsString(); + return args; } @Override public boolean getShuffle() { - return event.hasOption("shuffle") && event.getOption("shuffle").getAsBoolean(); + OptionMapping shuffleOption = event.getOption("shuffle"); + return shuffleOption != null && shuffleOption.getAsBoolean(); } @Override public boolean isUrl() { - return true; + return isUrl; } @Override public void setUrl(String url) { + args = url; } @Override public boolean verifyCommandArguments() { - boolean isUrl = ArgumentsUtil.isUrl(getArgs()); - - if (!isUrl) { - if (!isQuery) { - reply("You must provide an URL when using this command"); - } else { - reply("You must select an option from the list"); - } - + if (args.length() == 0) { + reply("URL/Search cannot be empty"); return false; } else { return true; diff --git a/src/main/java/com/casko1/wheelbarrow/bot/commands/hybrid/music/PlayHybridCommand.java b/src/main/java/com/casko1/wheelbarrow/bot/commands/hybrid/music/PlayHybridCommand.java index edbea9b..3ec2656 100644 --- a/src/main/java/com/casko1/wheelbarrow/bot/commands/hybrid/music/PlayHybridCommand.java +++ b/src/main/java/com/casko1/wheelbarrow/bot/commands/hybrid/music/PlayHybridCommand.java @@ -38,7 +38,14 @@ public class PlayHybridCommand extends SlashCommand { public PlayHybridCommand() { this.name = "play"; this.help = "Plays a song or playlist from specified url or query."; - this.children = new SlashCommand[]{new Url(), new Search()}; + this.options = Arrays.asList( + new OptionData( + OptionType.STRING, "url-or-search", "URL or name of the song/playlist, optionally select an option from the list", true, true + ), + new OptionData( + OptionType.BOOLEAN, "shuffle", "Shuffle the playlist", false + ) + ); this.guildOnly = false; this.searchClient = new YouTubeClient(); } @@ -50,11 +57,34 @@ protected void execute(CommandEvent event) { @Override protected void execute(SlashCommandEvent event) { + event.deferReply().queue(); + PlayHybridCommand.this.executeCommand(new PlaySlashCommandEvent(event)); } + @Override public void onAutoComplete(CommandAutoCompleteInteractionEvent event) { super.onAutoComplete(event); - this.children[1].onAutoComplete(event); + String query = event.getOption("url-or-search").getAsString(); + if (query.length() <= 3 || ArgumentsUtil.isUrl(query)) { + event.replyChoices(Collections.emptyList()).queue(); + return; + } + + List choice = new ArrayList<>(); + List results = getYouTubeTracks(query); + + if (results.size() > 0) { + for (int i = 0; i < Math.min(results.size(), 10); i++) { + YouTubeTrack track = results.get(i); + String title = track.getTitle(); + String clampedTitle = title.substring(0, Math.min(100, title.length())); + choice.add(new Command.Choice(clampedTitle, track.getUrl())); + } + + event.replyChoices(choice).queue(); + } else { + event.replyChoices(Collections.emptyList()).queue(); + } } public void executeCommand(PlayEvent event) { @@ -141,70 +171,4 @@ private List getYouTubeTracks(String query) { return Collections.emptyList(); } } - - private class Url extends SlashCommand { - - public Url() { - this.name = "url"; - this.options = Arrays.asList( - new OptionData( - OptionType.STRING, "url", "URL of the song/playlist", true - ), - new OptionData( - OptionType.BOOLEAN, "shuffle", "Shuffle the playlist", false - ) - ); - } - - @Override - protected void execute(SlashCommandEvent event) { - event.deferReply().queue(); - PlayHybridCommand.this.executeCommand(new PlaySlashCommandEvent(event, false)); - } - } - - private class Search extends SlashCommand { - - public Search() { - this.name = "search"; - this.options = Arrays.asList( - new OptionData( - OptionType.STRING, "query", "Name of the song/playlist", true, true - ), - new OptionData( - OptionType.BOOLEAN, "shuffle", "Shuffle the playlist", false - ) - ); - } - - @Override - protected void execute(SlashCommandEvent event) { - event.deferReply().queue(); - PlayHybridCommand.this.executeCommand(new PlaySlashCommandEvent(event, true)); - } - - @Override - public void onAutoComplete(CommandAutoCompleteInteractionEvent event) { - super.onAutoComplete(event); - String query = event.getOption("query").getAsString(); - if (query.length() <= 3) { - event.replyChoices(Collections.emptyList()).queue(); - return; - } - - List choice = new ArrayList<>(); - List results = getYouTubeTracks(query); - - if (results.size() > 0) { - for (int i = 0; i < Math.min(results.size(), 10); i++) { - YouTubeTrack track = results.get(i); - choice.add(new Command.Choice(track.getTitle(), track.getUrl())); - } - - event.replyChoices(choice).queue(); - } else { - event.replyChoices(Collections.emptyList()).queue(); - } - } - } } diff --git a/src/main/java/com/casko1/wheelbarrow/bot/commands/menu/music/SongDetectContextMenu.java b/src/main/java/com/casko1/wheelbarrow/bot/commands/menu/music/SongDetectContextMenu.java index 2d43a3b..7b66dee 100644 --- a/src/main/java/com/casko1/wheelbarrow/bot/commands/menu/music/SongDetectContextMenu.java +++ b/src/main/java/com/casko1/wheelbarrow/bot/commands/menu/music/SongDetectContextMenu.java @@ -40,8 +40,6 @@ protected void execute(MessageContextMenuEvent event) { return; } String type = ArgumentsUtil.getUrlContentType(url); - logger.info(type); - logger.info(url); if (!ArgumentsUtil.isValidVideoType(type)) { event.getHook().editOriginal("Unsupported video format or the URL cannot be read from." +