Skip to content

Commit

Permalink
Unify hybrid play command into a single slash version
Browse files Browse the repository at this point in the history
  • Loading branch information
casko1 committed May 18, 2024
1 parent 0f33425 commit 8dbae62
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 87 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.0
1.4.1
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies {
}

group = 'com.casko1'
version = '1.4.0'
version = '1.4.1'
description = 'wheelbarrow-bot'
java.sourceCompatibility = JavaVersion.VERSION_16

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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<Command.Choice> choice = new ArrayList<>();
List<YouTubeTrack> 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) {
Expand Down Expand Up @@ -141,70 +171,4 @@ private List<YouTubeTrack> 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<Command.Choice> choice = new ArrayList<>();
List<YouTubeTrack> 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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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." +
Expand Down

0 comments on commit 8dbae62

Please sign in to comment.