Many bug fixes, small features added. #3

Merged
andrewlalis merged 4 commits from development into master 2017-06-25 09:53:11 +00:00
19 changed files with 165 additions and 50 deletions
Showing only changes of commit 7bf9c9ab3e - Show all commits

View File

@ -29,11 +29,20 @@ queue all
Because the play command is defined as `play [URL]`, and the queue command is defined as `queue [all]`. Because the play command is defined as `play [URL]`, and the queue command is defined as `queue [all]`.
### General
* `info` - Displays the most common commands, and some basic information about the bot.
* `help` - Sends a private message to whoever issues this command. The message contains an in-depth list of all commands and their proper usage.
* `setprefix <PREFIX>` - Sets the prefix for all commands. Be careful, as some values will cause irreversible damage, if for example, a prefix conflicts with another bot's prefix.
### Music ### Music
* `play [URL]` - Starts playback from the queue, or if a URL is defined, then it will attempt to play that song, or add it to the queue, depending on if a song is already playing. If a song is already playing, you should receive an estimate of when your song should begin playing. * `play [URL]` - Starts playback from the queue, or if a URL is defined, then it will attempt to play that song, or add it to the queue, depending on if a song is already playing. If a song is already playing, you should receive an estimate of when your song should begin playing.
* `stop` - If music is playing, this will stop it.
* `skip` - If a song is playing, the bot will skip it and play the next song in the queue. * `skip` - If a song is playing, the bot will skip it and play the next song in the queue.
* `queue [all|clear]` - Lists up to the first 10 items on the queue, if no argument is given. * `queue [all|clear]` - Lists up to the first 10 items on the queue, if no argument is given.

View File

@ -24,6 +24,7 @@ public class Commands {
static { static {
//Music commands. //Music commands.
commands.add(new PlayCommand()); commands.add(new PlayCommand());
commands.add(new StopCommand());
commands.add(new QueueCommand()); commands.add(new QueueCommand());
commands.add(new SkipCommand()); commands.add(new SkipCommand());
commands.add(new RepeatCommand()); commands.add(new RepeatCommand());
@ -51,4 +52,18 @@ public class Commands {
log.log(BotLog.TYPE.ERROR, context.getGuild(), "Invalid command: "+command+" issued by "+context.getUser().getName()); log.log(BotLog.TYPE.ERROR, context.getGuild(), "Invalid command: "+command+" issued by "+context.getUser().getName());
} }
/**
* Attempts to get a command object, given the name of a command.
* @param command The name of the command to get.
* @return Either a command, or null.
*/
public Command get(String command){
for (Command cmd : commands){
if (cmd.getName().equals(command)){
return cmd;
}
}
return null;
}
} }

View File

@ -1,6 +1,8 @@
package handiebot.command.commands; package handiebot.command.commands;
import handiebot.command.CommandContext; import handiebot.command.CommandContext;
import handiebot.command.Commands;
import handiebot.command.types.Command;
import handiebot.command.types.ContextCommand; import handiebot.command.types.ContextCommand;
import sx.blah.discord.handle.obj.IPrivateChannel; import sx.blah.discord.handle.obj.IPrivateChannel;
import sx.blah.discord.util.EmbedBuilder; import sx.blah.discord.util.EmbedBuilder;
@ -14,7 +16,9 @@ import java.awt.*;
public class HelpCommand extends ContextCommand { public class HelpCommand extends ContextCommand {
//TODO: Finish the help class. //TODO: Finish the help class.
public HelpCommand() { public HelpCommand() {
super("help"); super("help",
"",
"Displays a list of commands and what they do.");
} }
@Override @Override
@ -28,7 +32,21 @@ public class HelpCommand extends ContextCommand {
builder.withColor(new Color(255, 0, 0)); builder.withColor(new Color(255, 0, 0));
builder.withDescription("I'm a discord bot that can manage music, as well as some other important functions which will be implemented later on. Some commands are shown below."); builder.withDescription("I'm a discord bot that can manage music, as well as some other important functions which will be implemented later on. Some commands are shown below.");
builder.appendField("Commands:", "play, skip, help", false);
//Music Commands:
StringBuilder sb = new StringBuilder();
for (Command cmd : Commands.commands){
sb.append('`');
if (cmd instanceof ContextCommand){
sb.append(((ContextCommand)cmd).getUsage(context.getGuild()));
} else {
sb.append(cmd.getUsage());
}
sb.append("`\n").append(cmd.getDescription()).append('\n');
}
builder.appendField("Commands:", sb.toString(), false);
pm.sendMessage(builder.build()); pm.sendMessage(builder.build());
} }

View File

@ -1,7 +1,8 @@
package handiebot.command.commands; package handiebot.command.commands;
import handiebot.command.CommandContext; import handiebot.command.CommandContext;
import handiebot.command.CommandHandler; import handiebot.command.commands.music.PlayCommand;
import handiebot.command.commands.music.QueueCommand;
import handiebot.command.types.ContextCommand; import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage; import handiebot.utils.DisappearingMessage;
import sx.blah.discord.util.EmbedBuilder; import sx.blah.discord.util.EmbedBuilder;
@ -15,7 +16,9 @@ import java.awt.*;
public class InfoCommand extends ContextCommand { public class InfoCommand extends ContextCommand {
public InfoCommand() { public InfoCommand() {
super("info"); super("info",
"",
"Displays some common commands and information about the bot.");
} }
@Override @Override
@ -23,8 +26,9 @@ public class InfoCommand extends ContextCommand {
EmbedBuilder builder = new EmbedBuilder(); EmbedBuilder builder = new EmbedBuilder();
builder.withColor(new Color(255, 0, 0)); builder.withColor(new Color(255, 0, 0));
builder.withDescription("HandieBot is a Discord bot created by Andrew Lalis. It can play music, manage playlists, and provide other assistance to users. Some useful commands are shown below."); builder.withDescription("HandieBot is a Discord bot created by Andrew Lalis. It can play music, manage playlists, and provide other assistance to users. Some useful commands are shown below.");
builder.appendField("`"+ CommandHandler.PREFIXES.get(context.getGuild())+"help`", "Receive a message with a detailed list of all commands and how to use them.", false); builder.appendField("`"+new HelpCommand().getUsage(context.getGuild())+"`", "Receive a message with a detailed list of all commands and how to use them.", false);
builder.appendField("`"+CommandHandler.PREFIXES.get(context.getGuild())+"setprefix`", "Changed the prefix used at the beginning of each command.", false); builder.appendField("`"+new PlayCommand().getUsage(context.getGuild())+"`", "Play a song, or add it to the queue if one is already playing. A URL can be a YouTube or SoundCloud link.", false);
builder.appendField("`"+new QueueCommand().getUsage(context.getGuild())+"`", "Show a list of songs that will soon be played.", false);
DisappearingMessage.deleteMessageAfter(10000, context.getChannel().sendMessage(builder.build())); DisappearingMessage.deleteMessageAfter(10000, context.getChannel().sendMessage(builder.build()));
} }
} }

View File

@ -15,7 +15,9 @@ import static handiebot.HandieBot.log;
public class SetPrefixCommand extends ContextCommand { public class SetPrefixCommand extends ContextCommand {
public SetPrefixCommand() { public SetPrefixCommand() {
super("setprefix"); super("setprefix",
"<PREFIX>",
"Sets the prefix for commands.");
} }
@Override @Override

View File

@ -13,7 +13,9 @@ import handiebot.utils.DisappearingMessage;
public class PlayCommand extends ContextCommand { public class PlayCommand extends ContextCommand {
public PlayCommand() { public PlayCommand() {
super("play"); super("play",
"[URL]",
"Plays a song, or adds it to the queue.");
} }
@Override @Override

View File

@ -23,7 +23,17 @@ import static handiebot.HandieBot.log;
public class PlaylistCommand extends ContextCommand { public class PlaylistCommand extends ContextCommand {
public PlaylistCommand(){ public PlaylistCommand(){
super("playlist"); super("playlist",
"<create|delete|show|add|remove|rename|move|play> [PLAYLIST]",
"Do something with a playlist.\n" +
"\tcreate - Creates a playlist.\n" +
"\tdelete - Deletes a playlist.\n" +
"\tshow [PLAYLIST] - If a playlist given, show that, otherwise show a list of playlists.\n" +
"\tadd <PLAYLIST> <URL> [URL]... - Adds one or more songs to a playlist.\n" +
"\tremove <PLAYLIST> <SONGINDEX> - Removes a song from a playlist.\n" +
"\trename <PLAYLIST> <NEWNAME> - Renames a playlist.\n" +
"\tmove <PLAYLIST> <OLDINDEX> <NEWINDEX> - Moves a song from one index to another.\n" +
"\tplay <PLAYLIST> - Queues all songs from a playlist.");
} }
@Override @Override
@ -69,7 +79,7 @@ public class PlaylistCommand extends ContextCommand {
* @param channel The channel to show the error message in. * @param channel The channel to show the error message in.
*/ */
private void incorrectMainArg(IChannel channel){ private void incorrectMainArg(IChannel channel){
new DisappearingMessage(channel, "Please use one of the following actions: \n`<create|delete|show|play|add|remove|rename>`", 5000); new DisappearingMessage(channel, "To use the playlist command: \n"+this.getUsage(channel.getGuild()), 5000);
} }
/** /**

View File

@ -3,7 +3,6 @@ package handiebot.command.commands.music;
import handiebot.HandieBot; import handiebot.HandieBot;
import handiebot.command.CommandContext; import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand; import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage;
/** /**
* @author Andrew Lalis * @author Andrew Lalis
@ -11,7 +10,11 @@ import handiebot.utils.DisappearingMessage;
*/ */
public class QueueCommand extends ContextCommand { public class QueueCommand extends ContextCommand {
public QueueCommand() { public QueueCommand() {
super("queue"); super("queue",
"[all|clear]",
"Shows the first 10 songs in the queue.\n" +
"\tall - Shows all songs.\n" +
"\tclear - Clears the queue and stops playing.");
} }
@Override @Override
@ -20,8 +23,7 @@ public class QueueCommand extends ContextCommand {
if (context.getArgs()[0].equals("all")){ if (context.getArgs()[0].equals("all")){
HandieBot.musicPlayer.showQueueList(context.getGuild(), true); HandieBot.musicPlayer.showQueueList(context.getGuild(), true);
} else if (context.getArgs()[0].equals("clear")){ } else if (context.getArgs()[0].equals("clear")){
HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.clearQueue(); HandieBot.musicPlayer.clearQueue(context.getGuild());
new DisappearingMessage(context.getChannel(), "Cleared the queue.", 5000);
} }
} else { } else {
HandieBot.musicPlayer.showQueueList(context.getGuild(), false); HandieBot.musicPlayer.showQueueList(context.getGuild(), false);

View File

@ -3,10 +3,6 @@ package handiebot.command.commands.music;
import handiebot.HandieBot; import handiebot.HandieBot;
import handiebot.command.CommandContext; import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand; import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import static handiebot.HandieBot.log;
/** /**
* @author Andrew Lalis * @author Andrew Lalis
@ -15,7 +11,9 @@ import static handiebot.HandieBot.log;
public class RepeatCommand extends ContextCommand { public class RepeatCommand extends ContextCommand {
public RepeatCommand(){ public RepeatCommand(){
super("repeat"); super("repeat",
"[true|false]",
"Sets repeating.");
} }
@Override @Override
@ -26,7 +24,5 @@ public class RepeatCommand extends ContextCommand {
} else { } else {
HandieBot.musicPlayer.toggleRepeat(context.getGuild()); HandieBot.musicPlayer.toggleRepeat(context.getGuild());
} }
log.log(BotLog.TYPE.MUSIC, context.getGuild(), "Set repeat to "+HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.isRepeating());
new DisappearingMessage(context.getChannel(), "Set repeat to "+HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.isRepeating(), 3000);
} }
} }

View File

@ -3,10 +3,6 @@ package handiebot.command.commands.music;
import handiebot.HandieBot; import handiebot.HandieBot;
import handiebot.command.CommandContext; import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand; import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import static handiebot.HandieBot.log;
/** /**
* @author Andrew Lalis * @author Andrew Lalis
@ -15,7 +11,9 @@ import static handiebot.HandieBot.log;
public class ShuffleCommand extends ContextCommand { public class ShuffleCommand extends ContextCommand {
public ShuffleCommand(){ public ShuffleCommand(){
super("shuffle"); super("shuffle",
"[true|false]",
"Sets shuffling.");
} }
@Override @Override
@ -26,7 +24,5 @@ public class ShuffleCommand extends ContextCommand {
} else { } else {
HandieBot.musicPlayer.toggleShuffle(context.getGuild()); HandieBot.musicPlayer.toggleShuffle(context.getGuild());
} }
log.log(BotLog.TYPE.MUSIC, context.getGuild(), "Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.isShuffling()));
new DisappearingMessage(context.getChannel(), "Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.isShuffling()), 3000);
} }
} }

View File

@ -11,7 +11,9 @@ import handiebot.command.types.ContextCommand;
public class SkipCommand extends ContextCommand { public class SkipCommand extends ContextCommand {
public SkipCommand() { public SkipCommand() {
super("skip"); super("skip",
"",
"Skips the current song.");
} }
@Override @Override

View File

@ -0,0 +1,23 @@
package handiebot.command.commands.music;
import handiebot.HandieBot;
import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand;
/**
* @author Andrew Lalis
* Command to stop playback of music on a server.
*/
public class StopCommand extends ContextCommand {
public StopCommand(){
super("stop",
"",
"Stops playing music.");
}
@Override
public void execute(CommandContext context) {
HandieBot.musicPlayer.stop(context.getGuild());
}
}

View File

@ -7,13 +7,25 @@ package handiebot.command.types;
public abstract class Command { public abstract class Command {
private String name; private String name;
private String usage;
private String description;
public Command(String name){ public Command(String name, String usage, String description){
this.name = name; this.name = name;
this.usage = usage;
this.description = description;
} }
public String getName(){ public String getName(){
return this.name; return this.name;
}; };
public String getUsage() {
return this.name+" "+this.usage;
};
public String getDescription() {
return this.description;
}
} }

View File

@ -1,6 +1,8 @@
package handiebot.command.types; package handiebot.command.types;
import handiebot.command.CommandContext; import handiebot.command.CommandContext;
import handiebot.command.CommandHandler;
import sx.blah.discord.handle.obj.IGuild;
/** /**
* @author Andrew Lalis * @author Andrew Lalis
@ -8,10 +10,19 @@ import handiebot.command.CommandContext;
*/ */
public abstract class ContextCommand extends Command { public abstract class ContextCommand extends Command {
public ContextCommand(String s) { public ContextCommand(String name, String usage, String description) {
super(s); super(name, usage, description);
} }
public abstract void execute(CommandContext context); public abstract void execute(CommandContext context);
/**
* Gets the usage of a command, including the guild, so that the prefix is known.
* @param guild The guild the command should be used on.
* @return A string representing the usage for this command.
*/
public String getUsage(IGuild guild){
return CommandHandler.PREFIXES.get(guild)+this.getUsage();
}
} }

View File

@ -6,8 +6,8 @@ package handiebot.command.types;
*/ */
public abstract class StaticCommand extends Command { public abstract class StaticCommand extends Command {
public StaticCommand(String s) { public StaticCommand(String name, String usage, String description) {
super(s); super(name, usage, description);
} }
public abstract void execute(); public abstract void execute();

View File

@ -3,6 +3,7 @@ package handiebot.lavaplayer;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers; import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import handiebot.HandieBot;
import handiebot.command.CommandHandler; import handiebot.command.CommandHandler;
import handiebot.lavaplayer.playlist.UnloadedTrack; import handiebot.lavaplayer.playlist.UnloadedTrack;
import handiebot.utils.DisappearingMessage; import handiebot.utils.DisappearingMessage;
@ -25,6 +26,7 @@ import static handiebot.HandieBot.log;
* @author Andrew Lalis * @author Andrew Lalis
* This class is a container for all the music related functions, and contains methods for easy playback and queue * This class is a container for all the music related functions, and contains methods for easy playback and queue
* management. * management.
* The goal is to abstract all functions to this layer, rather than have the bot interact directly with any schedulers.
*/ */
public class MusicPlayer { public class MusicPlayer {
@ -113,8 +115,7 @@ public class MusicPlayer {
* @param guild The guild to repeat for. * @param guild The guild to repeat for.
*/ */
public void toggleRepeat(IGuild guild){ public void toggleRepeat(IGuild guild){
GuildMusicManager musicManager = this.getMusicManager(guild); setRepeat(guild, !getMusicManager(guild).scheduler.isRepeating());
musicManager.scheduler.setRepeat(!musicManager.scheduler.isRepeating());
} }
/** /**
@ -124,6 +125,8 @@ public class MusicPlayer {
*/ */
public void setRepeat(IGuild guild, boolean value){ public void setRepeat(IGuild guild, boolean value){
getMusicManager(guild).scheduler.setRepeat(value); getMusicManager(guild).scheduler.setRepeat(value);
log.log(BotLog.TYPE.MUSIC, guild, "Set repeat to "+getMusicManager(guild).scheduler.isRepeating());
new DisappearingMessage(getChatChannel(guild), "Set repeat to "+getMusicManager(guild).scheduler.isRepeating(), 3000);
} }
/** /**
@ -131,8 +134,7 @@ public class MusicPlayer {
* @param guild The guild to toggle shuffling for. * @param guild The guild to toggle shuffling for.
*/ */
public void toggleShuffle(IGuild guild){ public void toggleShuffle(IGuild guild){
GuildMusicManager musicManager = this.getMusicManager(guild); setShuffle(guild, !getMusicManager(guild).scheduler.isShuffling());
musicManager.scheduler.setShuffle(!musicManager.scheduler.isShuffling());
} }
/** /**
@ -142,6 +144,8 @@ public class MusicPlayer {
*/ */
public void setShuffle(IGuild guild, boolean value){ public void setShuffle(IGuild guild, boolean value){
getMusicManager(guild).scheduler.setShuffle(value); getMusicManager(guild).scheduler.setShuffle(value);
log.log(BotLog.TYPE.MUSIC, guild, "Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(guild).scheduler.isShuffling()));
new DisappearingMessage(getChatChannel(guild), "Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(guild).scheduler.isShuffling()), 3000);
} }
/** /**
@ -217,6 +221,11 @@ public class MusicPlayer {
getMusicManager(guild).scheduler.nextTrack(); getMusicManager(guild).scheduler.nextTrack();
} }
public void clearQueue(IGuild guild){
getMusicManager(guild).scheduler.clearQueue();
new DisappearingMessage(getChatChannel(guild), "Cleared the queue.", 5000);
}
/** /**
* Skips the current track. * Skips the current track.
*/ */
@ -228,18 +237,20 @@ public class MusicPlayer {
/** /**
* Stops playback and disconnects from the voice channel, to cease music actions. * Stops playback and disconnects from the voice channel, to cease music actions.
* @param guild The guild to quit from. * @param guild The guild to stop from.
*/ */
public void quit(IGuild guild){ public void stop(IGuild guild){
getMusicManager(guild).scheduler.quit(); getMusicManager(guild).scheduler.stop();
new DisappearingMessage(getChatChannel(guild), "Stopped playing music.", 5000);
log.log(BotLog.TYPE.MUSIC, guild, "Stopped playing music.");
} }
/** /**
* Performs the same functions as quit, but with every guild. * Performs the same functions as stop, but with every guild.
*/ */
public void quitAll(){ public void quitAll(){
this.musicManagers.forEach((guild, musicManager) -> { this.musicManagers.forEach((guild, musicManager) -> {
musicManager.scheduler.quit(); musicManager.scheduler.stop();
}); });
this.playerManager.shutdown(); this.playerManager.shutdown();
} }

View File

@ -61,7 +61,7 @@ public class TrackScheduler extends AudioEventAdapter {
* Clears the queue. * Clears the queue.
*/ */
public void clearQueue(){ public void clearQueue(){
this.quit(); this.stop();
this.activePlaylist.clear(); this.activePlaylist.clear();
} }
@ -149,20 +149,22 @@ public class TrackScheduler extends AudioEventAdapter {
} }
player.startTrack(track, false); player.startTrack(track, false);
} else { } else {
this.quit(); this.stop();
} }
} }
/** /**
* If the user wishes to quit, stop the currently played track. * If the user wishes to stop, stop the currently played track.
*/ */
public void quit(){ public void stop(){
IVoiceChannel voiceChannel = HandieBot.musicPlayer.getVoiceChannel(this.guild); IVoiceChannel voiceChannel = HandieBot.musicPlayer.getVoiceChannel(this.guild);
if (voiceChannel.isConnected()){ if (voiceChannel.isConnected()){
voiceChannel.leave(); voiceChannel.leave();
} }
if (this.player.getPlayingTrack() != null) {
this.player.stopTrack(); this.player.stopTrack();
} }
}
@Override @Override
public void onTrackStart(AudioPlayer player, AudioTrack track) { public void onTrackStart(AudioPlayer player, AudioTrack track) {

View File

@ -41,7 +41,7 @@ public class CommandLineListener implements KeyListener {
* @param args The list of arguments for the command. * @param args The list of arguments for the command.
*/ */
private void executeCommand(String command, String[] args){ private void executeCommand(String command, String[] args){
if (command.equals("quit")){ if (command.equals("stop")){
new QuitAction().actionPerformed(null); new QuitAction().actionPerformed(null);
} }
} }

View File

@ -24,7 +24,7 @@ public class QuitAction implements ActionListener {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (guild != null){ if (guild != null){
HandieBot.musicPlayer.getChatChannel(this.guild).sendMessage("Quiting HandieBot"); HandieBot.musicPlayer.getChatChannel(this.guild).sendMessage("Quiting HandieBot");
HandieBot.musicPlayer.quit(this.guild); HandieBot.musicPlayer.stop(this.guild);
} else { } else {
HandieBot.quit(); HandieBot.quit();
} }