Skip to content

Commit

Permalink
SeekCmd, background queue, play command updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cmorley191 committed Mar 26, 2023
1 parent ee9b7ec commit 2c6acff
Show file tree
Hide file tree
Showing 19 changed files with 868 additions and 284 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ private static void startBot()
new RemoveCmd(bot),
new SearchCmd(bot),
new SCSearchCmd(bot),
new SeekCmd(bot),
new ShuffleCmd(bot),
new SkipCmd(bot),

new ForceRemoveCmd(bot),
new ForceskipCmd(bot),
new MoveTrackCmd(bot),
new PauseCmd(bot),
new PlaynextCmd(bot),
new RepeatCmd(bot),
new SkiptoCmd(bot),
new StopCmd(bot),
Expand Down
96 changes: 75 additions & 21 deletions src/main/java/com/jagrosh/jmusicbot/audio/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
public final static String STOP_EMOJI = "\u23F9"; // ⏹

private final FairQueue<QueuedTrack> queue = new FairQueue<>();
private final FairQueue<QueuedTrack> backgroundQueue = new FairQueue<>();
private AudioTrack latestActiveBackgroundTrack = null;
private Long latestActiveBackgroundTrackPosition = 0L;
private final List<AudioTrack> defaultQueue = new LinkedList<>();
private final Set<String> votes = new HashSet<>();

Expand All @@ -67,42 +70,67 @@ protected AudioHandler(PlayerManager manager, Guild guild, AudioPlayer player)
this.guildId = guild.getIdLong();
}

public int addTrackToFront(QueuedTrack qtrack)
public int addTrack(QueuedTrack qtrack, boolean background, Integer position)
{
if(audioPlayer.getPlayingTrack()==null)
boolean playingBackground = audioPlayer.getPlayingTrack()==latestActiveBackgroundTrack;
boolean playNow = position != null && position < 0;
if(audioPlayer.getPlayingTrack()==null || (playingBackground && (!background || playNow)) || (!background && playNow))
{
audioPlayer.playTrack(qtrack.getTrack());
AudioTrack track = qtrack.getTrack();
if (background) latestActiveBackgroundTrack = track;
else if (latestActiveBackgroundTrack != null) latestActiveBackgroundTrackPosition = latestActiveBackgroundTrack.getPosition();
audioPlayer.playTrack(track);
return -1;
}
else
else if (background)
{
queue.addAt(0, qtrack);
return 0;
if (position == null) return backgroundQueue.add(qtrack);
else {
if (position < 0) position = 0;
return backgroundQueue.addAt(position, qtrack);
}
}
else
{
if (position == null) return queue.add(qtrack);
return queue.addAt(position, qtrack);
}
}

public int addTrack(QueuedTrack qtrack)
public int addTrack(QueuedTrack qtrack, boolean background)
{
if(audioPlayer.getPlayingTrack()==null)
{
audioPlayer.playTrack(qtrack.getTrack());
return -1;
return addTrack(qtrack, background, null);
}

public void skipCurrentTrack() {
if (audioPlayer.getPlayingTrack() == latestActiveBackgroundTrack) {
latestActiveBackgroundTrack = null;
}
else
return queue.add(qtrack);
audioPlayer.stopTrack();
}

public FairQueue<QueuedTrack> getQueue()
{
return queue;
}

public FairQueue<QueuedTrack> getBackgroundQueue()
{
return backgroundQueue;
}

public boolean playingFromBackgroundQueue()
{
return audioPlayer.getPlayingTrack() != null && latestActiveBackgroundTrack != null && audioPlayer.getPlayingTrack() == latestActiveBackgroundTrack;
}

public void stopAndClear()
{
queue.clear();
backgroundQueue.clear();
latestActiveBackgroundTrack = null;
defaultQueue.clear();
audioPlayer.stopTrack();
//current = null;
}

public boolean isMusicPlaying(JDA jda)
Expand All @@ -128,6 +156,25 @@ public RequestMetadata getRequestMetadata()
return rm == null ? RequestMetadata.EMPTY : rm;
}

public boolean playFromBackground()
{
if (latestActiveBackgroundTrack != null) {
latestActiveBackgroundTrack = latestActiveBackgroundTrack.makeClone();
latestActiveBackgroundTrack.setPosition(latestActiveBackgroundTrackPosition);
audioPlayer.playTrack(latestActiveBackgroundTrack);
return true;
}

if (!backgroundQueue.isEmpty())
{
latestActiveBackgroundTrack = backgroundQueue.remove(0).getTrack();
audioPlayer.playTrack(latestActiveBackgroundTrack);
return true;
}

return false;
}

public boolean playFromDefault()
{
if(!defaultQueue.isEmpty())
Expand All @@ -142,7 +189,7 @@ public boolean playFromDefault()
Playlist pl = manager.getBot().getPlaylistLoader().getPlaylist(settings.getDefaultPlaylist());
if(pl==null || pl.getItems().isEmpty())
return false;
pl.loadTracks(manager, (at) ->
pl.loadTracks(manager, (at, i) ->
{
if(audioPlayer.getPlayingTrack()==null)
audioPlayer.playTrack(at);
Expand All @@ -165,15 +212,22 @@ public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason
if(endReason==AudioTrackEndReason.FINISHED && repeatMode != RepeatMode.OFF)
{
QueuedTrack clone = new QueuedTrack(track.makeClone(), track.getUserData(RequestMetadata.class));
FairQueue<QueuedTrack> trackQueue = (track == latestActiveBackgroundTrack) ? backgroundQueue : queue;
if(repeatMode == RepeatMode.ALL)
queue.add(clone);
trackQueue.add(clone);
else
queue.addAt(0, clone);
trackQueue.addAt(0, clone);
}

if (track == latestActiveBackgroundTrack && endReason != AudioTrackEndReason.REPLACED) {
// when background track is replaced by a queue track, we keep it around. Otherwise, this track is over.
latestActiveBackgroundTrack = null;
// note that REPLACED is *also* used when the track is skipped, but skipTrack() makes sure we don't keep it around in that case.
}

if(queue.isEmpty())
if(queue.isEmpty() && endReason!=AudioTrackEndReason.REPLACED)
{
if(!playFromDefault())
if(!playFromBackground() && !playFromDefault())
{
manager.getBot().getNowplayingHandler().onTrackUpdate(guildId, null, this);
if(!manager.getBot().getConfig().getStay())
Expand All @@ -183,7 +237,7 @@ public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason
player.setPaused(false);
}
}
else
else if (endReason.mayStartNext)
{
QueuedTrack qt = queue.pull();
player.playTrack(qt.getTrack());
Expand All @@ -206,7 +260,7 @@ public Message getNowPlaying(JDA jda)
Guild guild = guild(jda);
AudioTrack track = audioPlayer.getPlayingTrack();
MessageBuilder mb = new MessageBuilder();
mb.append(FormatUtil.filter(manager.getBot().getConfig().getSuccess()+" **Now Playing in "+guild.getSelfMember().getVoiceState().getChannel().getAsMention()+"...**"));
mb.append(FormatUtil.filter(manager.getBot().getConfig().getSuccess()+" **Now Playing from "+(playingFromBackgroundQueue() ? "Background Queue" : "Queue")+" in "+guild.getSelfMember().getVoiceState().getChannel().getAsMention()+"...**"));
EmbedBuilder eb = new EmbedBuilder();
eb.setColor(guild.getSelfMember().getColor());
RequestMetadata rm = getRequestMetadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ForceRemoveCmd(Bot bot)
{
super(bot);
this.name = "forceremove";
this.help = "removes all entries by a user from the queue";
this.help = "removes all entries by a user from the queue and the background queue";
this.arguments = "<user>";
this.aliases = bot.getConfig().getAliases(this.name);
this.beListening = false;
Expand All @@ -56,9 +56,9 @@ public void doCommand(CommandEvent event)
}

AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
if (handler.getQueue().isEmpty())
if (handler.getQueue().isEmpty() && handler.getBackgroundQueue().isEmpty())
{
event.reply(bot.getError(event)+"There is nothing in the queue!");
event.reply(bot.getError(event)+"There is nothing in the queues!");
return;
}

Expand Down Expand Up @@ -107,13 +107,14 @@ else if(found.size()>1)
private void removeAllEntries(User target, CommandEvent event)
{
int count = ((AudioHandler) event.getGuild().getAudioManager().getSendingHandler()).getQueue().removeAll(target.getIdLong());
if (count == 0)
int backgroundCount = ((AudioHandler) event.getGuild().getAudioManager().getSendingHandler()).getBackgroundQueue().removeAll(target.getIdLong());
if (count == 0 && backgroundCount == 0)
{
event.reply(bot.getWarning(event)+"**"+target.getName()+"** doesn't have any songs in the queue!");
event.reply(bot.getWarning(event)+"**"+target.getName()+"** doesn't have any songs in the queues!");
}
else
{
event.reply(bot.getSuccess(event)+"Successfully removed `"+count+"` entries from **"+target.getName()+"**#"+target.getDiscriminator()+".");
event.reply(bot.getSuccess(event)+"Successfully removed `"+(count+backgroundCount)+"` entries from **"+target.getName()+"**#"+target.getDiscriminator()+".");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public void doCommand(CommandEvent event)
RequestMetadata rm = handler.getRequestMetadata();
event.reply(bot.getSuccess(event)+" Skipped **"+handler.getPlayer().getPlayingTrack().getInfo().title
+"** "+(rm.getOwner() == 0L ? "(autoplay)" : "(requested by **" + rm.user.username + "**)"));
handler.getPlayer().stopTrack();
handler.skipCurrentTrack();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public MoveTrackCmd(Bot bot)
{
super(bot);
this.name = "movetrack";
this.help = "move a track in the current queue to a different position";
this.help = "move a track in the currently playing queue to a different position";
this.arguments = "<from> <to>";
this.aliases = bot.getConfig().getAliases(this.name);
this.bePlaying = true;
Expand Down Expand Up @@ -57,24 +57,25 @@ public void doCommand(CommandEvent event)

// Validate that from and to are available
AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
FairQueue<QueuedTrack> queue = handler.getQueue();
FairQueue<QueuedTrack> queue = (handler.playingFromBackgroundQueue()) ? handler.getBackgroundQueue() : handler.getQueue();
String queueName = (handler.playingFromBackgroundQueue()) ? "the background queue" : "the queue";
if (isUnavailablePosition(queue, from))
{
String reply = String.format("`%d` is not a valid position in the queue!", from);
String reply = String.format("`%d` is not a valid position in "+queueName+"!", from);
event.reply(bot.getError(event)+reply);
return;
}
if (isUnavailablePosition(queue, to))
{
String reply = String.format("`%d` is not a valid position in the queue!", to);
String reply = String.format("`%d` is not a valid position in "+queueName+"!", to);
event.reply(bot.getError(event)+reply);
return;
}

// Move the track
QueuedTrack track = queue.moveItem(from - 1, to - 1);
String trackTitle = track.getTrack().getInfo().title;
String reply = String.format("Moved **%s** from position `%d` to `%d`.", trackTitle, from, to);
String reply = String.format("Moved **%s** from position `%d` to `%d` in "+queueName+".", trackTitle, from, to);
event.reply(bot.getSuccess(event)+reply);
}

Expand Down
Loading

0 comments on commit 2c6acff

Please sign in to comment.