This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 208
Playlist command #599
Open
kthezelais
wants to merge
2
commits into
freyacodes:dev
Choose a base branch
from
TiltdBastion:playlistCommand
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Playlist command #599
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
367 changes: 367 additions & 0 deletions
367
FredBoat/src/main/java/fredboat/command/music/control/PlayListCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,367 @@ | ||
package fredboat.command.music.control; | ||
|
||
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; | ||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack; | ||
import fredboat.audio.player.GuildPlayer; | ||
import fredboat.audio.queue.AudioTrackContext; | ||
import fredboat.command.info.HelpCommand; | ||
import fredboat.commandmeta.abs.CommandContext; | ||
import fredboat.commandmeta.abs.ICommandRestricted; | ||
import fredboat.commandmeta.abs.IMusicCommand; | ||
import fredboat.commandmeta.abs.JCommand; | ||
import fredboat.definitions.PermissionLevel; | ||
import fredboat.definitions.SearchProvider; | ||
import fredboat.messaging.internal.Context; | ||
import fredboat.util.MessageBuilder; | ||
import fredboat.util.TextUtils; | ||
import javax.annotation.Nonnull; | ||
import fredboat.util.rest.TrackSearcher; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static fredboat.main.LauncherKt.getBotController; | ||
import static fredboat.util.MessageBuilderKt.localMessageBuilder; | ||
|
||
public class PlayListCommand extends JCommand implements IMusicCommand, ICommandRestricted { | ||
|
||
private List<AudioTrackContext> playlist; | ||
private List<SearchProvider> searchProviders; | ||
private TrackSearcher trackSearcher; | ||
private List<AudioTrack> selectable; | ||
private boolean inWaitCopy; | ||
private boolean inWaitRemove; | ||
|
||
public PlayListCommand(List<SearchProvider> searchProviders, TrackSearcher trackSearcher, String name, String aliases) { | ||
super(name, aliases); | ||
this.searchProviders = searchProviders; | ||
this.trackSearcher = trackSearcher; | ||
this.selectable = null; | ||
this.inWaitCopy = false; | ||
this.inWaitRemove = false; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public PermissionLevel getMinimumPerms() { | ||
return PermissionLevel.DJ; | ||
} | ||
|
||
@Override | ||
public void onInvoke(@Nonnull CommandContext context) { | ||
if (!context.hasArguments()) { | ||
HelpCommand.sendFormattedCommandHelp(context); | ||
return; | ||
} | ||
|
||
GuildPlayer player = getBotController().getPlayerRegistry().getOrCreate(context.getGuild()); | ||
String userInput = context.getArgs()[0]; | ||
|
||
// Menu selection used when a previous copy request | ||
// has been called, and the playlist is not empty. | ||
if (this.inWaitCopy) { | ||
switch (userInput) { | ||
case "1": | ||
// replace the playlist content by the queue. | ||
this.copy(context, player); | ||
break; | ||
|
||
case "2": | ||
// Don't do anything. | ||
context.reply(context.i18n("playlistNoModification")); | ||
break; | ||
|
||
default: | ||
context.reply(context.i18n("playlistSelectionMenu")); | ||
break; | ||
|
||
} | ||
this.inWaitCopy = false; | ||
return; | ||
} | ||
|
||
if (this.inWaitRemove) { | ||
this.remove(context, player, userInput); | ||
this.inWaitRemove = false; | ||
return; | ||
} | ||
|
||
// All the playlist options. | ||
switch (userInput) { | ||
case "copy": | ||
if (this.playlist != null) { | ||
if (!this.playlist.isEmpty()) { | ||
context.reply(context.i18n("playlistReplaceContentQuestion")); | ||
this.inWaitCopy = true; | ||
return; | ||
} | ||
} | ||
|
||
this.copy(context, player); | ||
break; | ||
|
||
case "remove": | ||
if (this.playlist == null || this.playlist.isEmpty()) { | ||
context.reply(context.i18n("playlistIsEmpty")); | ||
return; | ||
} else if (context.getArgs().length == 1) { | ||
context.reply(context.i18nFormat("playlistRemoveMenuSelection", this.playlist.size())); | ||
this.list(context, player); | ||
return; | ||
} | ||
|
||
this.remove(context, player, userInput); | ||
break; | ||
|
||
case "list": | ||
this.list(context, player); | ||
break; | ||
|
||
case "add": | ||
this.add(context, player); | ||
break; | ||
|
||
case "run": | ||
this.run(context, player); | ||
break; | ||
|
||
default: | ||
HelpCommand.sendFormattedCommandHelp(context); | ||
return; | ||
} | ||
|
||
} | ||
|
||
// Copy the queue into the playlist. If the playlist is not empty, enter the | ||
// command ;;playlist [1-2] (1: overwrite the playlist, 2: do nothing). | ||
private void copy(@Nonnull CommandContext context, GuildPlayer player) { | ||
if (this.inWaitCopy) { | ||
this.playlist = player.getTracksInRange(0, player.getTrackCount()); | ||
context.reply(context.i18n("playlistReplaceContent")); | ||
} else if (player == null || player.isQueueEmpty()) { | ||
this.playlist = null; | ||
context.reply(context.i18n("playlistCopyEmptyQueue")); | ||
} else { | ||
this.playlist = player.getTracksInRange(0, player.getTrackCount()); | ||
context.reply(context.i18n("playlistCreated")); | ||
} | ||
} | ||
|
||
// Display title's list of the playlist. | ||
private void list(@Nonnull CommandContext context, GuildPlayer player) { | ||
if (this.playlist == null || this.playlist.isEmpty()) { | ||
context.reply(context.i18n("playlistListIsEmpty")); | ||
return; | ||
} | ||
|
||
MessageBuilder mb = localMessageBuilder(); | ||
|
||
int i = 0; | ||
|
||
for (AudioTrackContext atc : this.playlist) { | ||
String status = " "; | ||
|
||
mb.code(TextUtils.escapeAndDefuse("[" + (i + 1) + "]")); | ||
mb.append(status); | ||
mb.append(" " + TextUtils.escapeAndDefuse(atc.getEffectiveTitle()) + " (" + TextUtils.formatTime(atc.getEffectiveDuration()) + ")"); | ||
mb.append("\n"); | ||
|
||
i++; | ||
} | ||
|
||
context.reply(mb.build()); | ||
} | ||
|
||
// Add a title from a search. Then enter the command ;;playlist [1-5] | ||
// to choose a title from the search list. | ||
private void add(@Nonnull CommandContext context, GuildPlayer player) { | ||
|
||
if(context.getArgs().length == 1) { | ||
context.reply(context.i18n("playlistBadEntry")); | ||
this.selectable = null; | ||
return; | ||
} | ||
|
||
if(this.selectable == null || this.selectable.isEmpty()) { | ||
if (context.getArgs()[1].matches("[-1-9]*") && context.getArgs().length == 2) { | ||
context.reply(context.i18n("playlistSearchFirst")); | ||
return; | ||
} | ||
|
||
searchForVideos(context); | ||
return; | ||
} | ||
|
||
if(context.getArgs()[1].matches("[1-5]")) { | ||
context.reply(context.i18nFormat("playlistTrackSelection", context.getArgs()[1], this.selectable.get(Integer.parseInt(context.getArgs()[1]) - 1).getInfo().title)); | ||
AudioTrackContext newTrack = new AudioTrackContext(this.selectable.get(Integer.parseInt(context.getArgs()[1]) - 1), context.getMember(), false); | ||
|
||
if (this.playlist == null) { | ||
this.playlist = new ArrayList<>(); | ||
} | ||
|
||
this.playlist.add(newTrack); | ||
|
||
this.selectable = null; | ||
return; | ||
|
||
} | ||
|
||
if (context.getArgs()[1].matches("[-1-9]*") && context.getArgs().length == 2) { | ||
context.reply(context.i18n("playlistSelectionNumber")); | ||
this.selectable = null; | ||
return; | ||
} | ||
|
||
if(this.selectable != null && context.getArgs()[1].matches("[1-9A-Za-z ]*")) { | ||
context.reply(context.i18n("playlistSelectionNumber")); | ||
this.selectable = null; | ||
return; | ||
} | ||
|
||
context.reply(context.i18n("playlistOperationError")); | ||
|
||
} | ||
|
||
// Internal Playlist function that is used by the add function to do a research | ||
// form the Youtube API. It's inspired by the PlayCommand methods. | ||
private void searchForVideos(@Nonnull CommandContext context) { | ||
String query = context.getRawArgs().substring(4).replace(TrackSearcher.PUNCTUATION_REGEX, ""); | ||
AudioPlaylist list; | ||
|
||
try { | ||
list = this.trackSearcher.searchForTracks(query, this.searchProviders); | ||
|
||
} catch (TrackSearcher.SearchingException e) { | ||
context.reply(context.i18n("playYoutubeSearchError")); | ||
return; | ||
} | ||
|
||
if (list == null || list.getTracks().isEmpty()) { | ||
context.reply(context.i18n("playlistNoSearchResult")); | ||
|
||
} else { | ||
//Get at most 5 tracks | ||
this.selectable = list.getTracks().subList(0, Math.min(TrackSearcher.MAX_RESULTS, list.getTracks().size())); | ||
|
||
MessageBuilder mb = localMessageBuilder(); | ||
int i = 0; | ||
|
||
for (AudioTrack atc : this.selectable) { | ||
String status = " "; | ||
|
||
mb.code(TextUtils.escapeAndDefuse((i + 1) + ":")); | ||
mb.append(status); | ||
mb.append(" " + TextUtils.escapeAndDefuse(atc.getInfo().title) + " (" + TextUtils.formatTime(atc.getDuration()) + ")"); | ||
mb.append("\n"); | ||
|
||
i++; | ||
} | ||
|
||
context.reply(mb.build()); | ||
} | ||
} | ||
|
||
// Delete one or more titles from the playlist. Use all option to remove | ||
// all the titles into the playlist, or the title number (find it on the | ||
// list) to remove only one. | ||
private void remove(@Nonnull CommandContext context, GuildPlayer player, String userInput) { | ||
int rank; | ||
|
||
if (context.getArgs().length > 1) { | ||
String action = context.getArgs()[1]; | ||
|
||
switch (action) { | ||
case "all": | ||
this.playlist.clear(); | ||
context.reply(context.i18n("playlistEmptied")); | ||
break; | ||
|
||
default: | ||
if (context.getArgs()[1].matches("[0-9]*")) { | ||
rank = Integer.parseInt(context.getArgs()[1]); | ||
} else { | ||
context.reply(context.i18nFormat("playlistRemoveTrackAttempt", this.playlist.size())); | ||
return; | ||
} | ||
String title = this.playlist.get(rank - 1).getEffectiveTitle(); | ||
this.playlist.remove(rank - 1); | ||
context.reply(context.i18nFormat("playlistTrackRemove", title)); | ||
this.inWaitRemove = false; | ||
break; | ||
} | ||
this.inWaitRemove = false; | ||
return; | ||
} | ||
|
||
context.reply(context.i18nFormat("playlistRemoveMenuSelection", this.playlist.size())); | ||
list(context, player); | ||
this.inWaitRemove = true; | ||
|
||
if(userInput.matches("[0-9]*")) { | ||
rank = Integer.parseInt(userInput); | ||
} else { | ||
context.reply(context.i18nFormat("playlistRemoveTrackAttempt", this.playlist.size())); | ||
return; | ||
} | ||
|
||
if (rank > this.playlist.size() | rank <= 0) { | ||
context.reply(context.i18nFormat("playlistRemoveTrackAttempt", this.playlist.size())); | ||
return; | ||
} | ||
|
||
String title = this.playlist.get(rank - 1).getEffectiveTitle(); | ||
this.playlist.remove(rank - 1); | ||
context.reply(context.i18nFormat("playlistTrackRemove", title)); | ||
} | ||
|
||
// Add all the titles from the playlist to the queue | ||
private void run(@Nonnull CommandContext context, GuildPlayer player) { | ||
if (this.playlist != null) { | ||
if (this.playlist.isEmpty()) { | ||
context.reply(context.i18n("playlistIsEmpty")); | ||
return; | ||
} | ||
|
||
if(context.getArgs().length > 1) { | ||
|
||
if(context.getArgs()[1].equals("random")) { | ||
List<AudioTrackContext> randomPlaylist = new ArrayList<>(); | ||
List<Integer> playlistRanks = new ArrayList<>(); | ||
int randomNumber; | ||
|
||
for (int i = 0; i < this.playlist.size(); i++) { | ||
playlistRanks.add(i); | ||
} | ||
|
||
for (int i = 0; i < this.playlist.size(); i++) { | ||
randomNumber = (int)(Math.random() * (playlistRanks.size())); | ||
randomPlaylist.add(this.playlist.get(randomNumber)); | ||
|
||
} | ||
|
||
player.loadAll(randomPlaylist); | ||
context.reply(context.i18n("playlistAddToQueue")); | ||
player.joinChannel(context.getMember()); | ||
player.setPause(false); | ||
|
||
return; | ||
} | ||
context.reply(context.i18n("playlistBadEntry")); | ||
return; | ||
} | ||
|
||
player.loadAll(this.playlist); | ||
context.reply(context.i18n("playlistAddToQueue")); | ||
player.joinChannel(context.getMember()); | ||
player.setPause(false); | ||
return; | ||
} | ||
|
||
context.reply(context.i18n("playlistIsEmpty")); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String help(@Nonnull Context context) { return context.i18n("helpPlayListCommand"); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fundamentally doesn't work. You're sharing the same playlist between everyone. It also doesn't persist between restarts.
Doing something like this this the right way would require working with the database, and even then I would do things differently.