Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move track command #218

Merged
merged 3 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PR feedback pass
  • Loading branch information
Telluur committed Apr 8, 2019
commit eb1fb776a5ac6806cd8452b0e308e7e6a3b5855e
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 @@ -114,11 +114,11 @@ public static void main(String[] args)
new SkipCmd(bot),

new ForceskipCmd(bot),
new MoveTrackCmd(bot),
new PauseCmd(bot),
new PlaynextCmd(bot, config.getLoading()),
new RepeatCmd(bot),
new SkiptoCmd(bot),
new MoveTrackCmd(bot),
new StopCmd(bot),
new VolumeCmd(bot),

Expand Down
57 changes: 25 additions & 32 deletions src/main/java/com/jagrosh/jmusicbot/commands/dj/MoveTrackCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@
import com.jagrosh.jmusicbot.commands.DJCommand;
import com.jagrosh.jmusicbot.queue.FairQueue;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Command that provides users the ability to move a track in the playlist.
*/
public class MoveTrackCmd extends DJCommand
{

private static final Pattern PATTERN = Pattern.compile("^(\\d+)\\s+(\\d+)$");

public MoveTrackCmd(Bot bot)
{
super(bot);
this.name = "movetrack";
this.help = "Move a track in the current playlist to a different position";
this.help = "move a track in the current queue to a different position";
this.arguments = "<from> <to>";
this.aliases = new String[]{"move"};
this.bePlaying = true;
Expand All @@ -35,58 +30,56 @@ public void doCommand(CommandEvent event)
int from;
int to;

String[] parts = event.getArgs().split("\\s+", 2);
if(parts.length < 2)
{
event.replyError("Please include two valid indexes.'");
Telluur marked this conversation as resolved.
Show resolved Hide resolved
return;
}

try
{
// Validate the args
String args = event.getArgs().trim();
Matcher matcher = PATTERN.matcher(args);
if (!matcher.matches())
{
event.replyError("That ain't right. Usage: movetrack <from> <to>");
return;
}

from = Integer.parseInt(matcher.group(1));
to = Integer.parseInt(matcher.group(2));
from = Integer.parseInt(parts[0]);
to = Integer.parseInt(parts[1]);
}
catch (NumberFormatException e)
{
// Should already be caught by the regex but ok..
event.replyError("That ain't a number: " + e.getMessage());
event.replyError("Please provide two valid indexes.");
return;
}

if (from == to)
{
event.replySuccess("Wow! That was easy. Great job using this command. You're doing a great job.");
event.replyError("Can't move a track to the same position.");
return;
}

// Validate that these are both positions available
// Validate that from and to are available
AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
FairQueue<QueuedTrack> queue = handler.getQueue();
if (!isAvailablePosition(event, queue, from) || !isAvailablePosition(event, queue, to))
if (isUnavailablePosition(queue, from))
{
String reply = String.format("`%d` is not a valid position in the queue!", from);
event.replyError(reply);
return;
}
if (isUnavailablePosition(queue, to))
{
String reply = String.format("`%d` is not a valid position in the queue!", to);
event.replyError(reply);
return;
}

// Move the track
QueuedTrack track = queue.moveItem(from - 1, to - 1);
String trackTitle = track.getTrack().getInfo().title;
String reply = String.format("Moved track '%s' from position %d to %d.", trackTitle, from, to);
String reply = String.format("Moved **%s** from position `%d` to `%d.`", trackTitle, from, to);
Telluur marked this conversation as resolved.
Show resolved Hide resolved
event.replySuccess(reply);
}

private boolean isAvailablePosition(CommandEvent event, FairQueue<QueuedTrack> queue, int position)
private static boolean isUnavailablePosition(FairQueue<QueuedTrack> queue, int position)
{
if (position < 1 || position > queue.size())
{
event.replyError(position + " ain't a valid position.");
return false;
}
else
{
return true;
}
return (position < 1 || position > queue.size());
}
}