forked from jagrosh/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jagrosh#218 from SlapGaming/pullreqs
Move track command
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
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
85 changes: 85 additions & 0 deletions
85
src/main/java/com/jagrosh/jmusicbot/commands/dj/MoveTrackCmd.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,85 @@ | ||
package com.jagrosh.jmusicbot.commands.dj; | ||
|
||
|
||
import com.jagrosh.jdautilities.command.CommandEvent; | ||
import com.jagrosh.jmusicbot.Bot; | ||
import com.jagrosh.jmusicbot.audio.AudioHandler; | ||
import com.jagrosh.jmusicbot.audio.QueuedTrack; | ||
import com.jagrosh.jmusicbot.commands.DJCommand; | ||
import com.jagrosh.jmusicbot.queue.FairQueue; | ||
|
||
/** | ||
* Command that provides users the ability to move a track in the playlist. | ||
*/ | ||
public class MoveTrackCmd extends DJCommand | ||
{ | ||
|
||
public MoveTrackCmd(Bot bot) | ||
{ | ||
super(bot); | ||
this.name = "movetrack"; | ||
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; | ||
} | ||
|
||
@Override | ||
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."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
// Validate the args | ||
from = Integer.parseInt(parts[0]); | ||
to = Integer.parseInt(parts[1]); | ||
} | ||
catch (NumberFormatException e) | ||
{ | ||
event.replyError("Please provide two valid indexes."); | ||
return; | ||
} | ||
|
||
if (from == to) | ||
{ | ||
event.replyError("Can't move a track to the same position."); | ||
return; | ||
} | ||
|
||
// Validate that from and to are available | ||
AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler(); | ||
FairQueue<QueuedTrack> queue = handler.getQueue(); | ||
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 **%s** from position `%d` to `%d`.", trackTitle, from, to); | ||
event.replySuccess(reply); | ||
} | ||
|
||
private static boolean isUnavailablePosition(FairQueue<QueuedTrack> queue, int position) | ||
{ | ||
return (position < 1 || position > queue.size()); | ||
} | ||
} |
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