From 7bf9c9ab3e17478ae7924ad942f760c48826683d Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sun, 25 Jun 2017 02:06:34 +0200 Subject: [PATCH] Added better command descriptions. Help does not work. --- README.md | 9 ++++++ src/main/java/handiebot/command/Commands.java | 15 ++++++++++ .../command/commands/HelpCommand.java | 22 ++++++++++++-- .../command/commands/InfoCommand.java | 12 +++++--- .../command/commands/SetPrefixCommand.java | 4 ++- .../command/commands/music/PlayCommand.java | 4 ++- .../commands/music/PlaylistCommand.java | 14 +++++++-- .../command/commands/music/QueueCommand.java | 10 ++++--- .../command/commands/music/RepeatCommand.java | 10 ++----- .../commands/music/ShuffleCommand.java | 10 ++----- .../command/commands/music/SkipCommand.java | 4 ++- .../command/commands/music/StopCommand.java | 23 +++++++++++++++ .../java/handiebot/command/types/Command.java | 14 ++++++++- .../command/types/ContextCommand.java | 15 ++++++++-- .../command/types/StaticCommand.java | 4 +-- .../handiebot/lavaplayer/MusicPlayer.java | 29 +++++++++++++------ .../handiebot/lavaplayer/TrackScheduler.java | 12 ++++---- .../handiebot/view/CommandLineListener.java | 2 +- .../handiebot/view/actions/QuitAction.java | 2 +- 19 files changed, 165 insertions(+), 50 deletions(-) create mode 100644 src/main/java/handiebot/command/commands/music/StopCommand.java diff --git a/README.md b/README.md index 0f5f1b8..706e82c 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,20 @@ 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 ` - 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 * `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. * `queue [all|clear]` - Lists up to the first 10 items on the queue, if no argument is given. diff --git a/src/main/java/handiebot/command/Commands.java b/src/main/java/handiebot/command/Commands.java index 2f87ae2..f3cdc73 100644 --- a/src/main/java/handiebot/command/Commands.java +++ b/src/main/java/handiebot/command/Commands.java @@ -24,6 +24,7 @@ public class Commands { static { //Music commands. commands.add(new PlayCommand()); + commands.add(new StopCommand()); commands.add(new QueueCommand()); commands.add(new SkipCommand()); 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()); } + /** + * 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; + } + } diff --git a/src/main/java/handiebot/command/commands/HelpCommand.java b/src/main/java/handiebot/command/commands/HelpCommand.java index 4050398..a4d2e7d 100644 --- a/src/main/java/handiebot/command/commands/HelpCommand.java +++ b/src/main/java/handiebot/command/commands/HelpCommand.java @@ -1,6 +1,8 @@ package handiebot.command.commands; import handiebot.command.CommandContext; +import handiebot.command.Commands; +import handiebot.command.types.Command; import handiebot.command.types.ContextCommand; import sx.blah.discord.handle.obj.IPrivateChannel; import sx.blah.discord.util.EmbedBuilder; @@ -14,7 +16,9 @@ import java.awt.*; public class HelpCommand extends ContextCommand { //TODO: Finish the help class. public HelpCommand() { - super("help"); + super("help", + "", + "Displays a list of commands and what they do."); } @Override @@ -28,7 +32,21 @@ public class HelpCommand extends ContextCommand { 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.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()); } diff --git a/src/main/java/handiebot/command/commands/InfoCommand.java b/src/main/java/handiebot/command/commands/InfoCommand.java index 229814d..6d10564 100644 --- a/src/main/java/handiebot/command/commands/InfoCommand.java +++ b/src/main/java/handiebot/command/commands/InfoCommand.java @@ -1,7 +1,8 @@ package handiebot.command.commands; 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.utils.DisappearingMessage; import sx.blah.discord.util.EmbedBuilder; @@ -15,7 +16,9 @@ import java.awt.*; public class InfoCommand extends ContextCommand { public InfoCommand() { - super("info"); + super("info", + "", + "Displays some common commands and information about the bot."); } @Override @@ -23,8 +26,9 @@ public class InfoCommand extends ContextCommand { EmbedBuilder builder = new EmbedBuilder(); 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.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("`"+CommandHandler.PREFIXES.get(context.getGuild())+"setprefix`", "Changed the prefix used at the beginning of each command.", 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("`"+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())); } } diff --git a/src/main/java/handiebot/command/commands/SetPrefixCommand.java b/src/main/java/handiebot/command/commands/SetPrefixCommand.java index a68e1e0..e5b3012 100644 --- a/src/main/java/handiebot/command/commands/SetPrefixCommand.java +++ b/src/main/java/handiebot/command/commands/SetPrefixCommand.java @@ -15,7 +15,9 @@ import static handiebot.HandieBot.log; public class SetPrefixCommand extends ContextCommand { public SetPrefixCommand() { - super("setprefix"); + super("setprefix", + "", + "Sets the prefix for commands."); } @Override diff --git a/src/main/java/handiebot/command/commands/music/PlayCommand.java b/src/main/java/handiebot/command/commands/music/PlayCommand.java index 1d961ec..567468f 100644 --- a/src/main/java/handiebot/command/commands/music/PlayCommand.java +++ b/src/main/java/handiebot/command/commands/music/PlayCommand.java @@ -13,7 +13,9 @@ import handiebot.utils.DisappearingMessage; public class PlayCommand extends ContextCommand { public PlayCommand() { - super("play"); + super("play", + "[URL]", + "Plays a song, or adds it to the queue."); } @Override diff --git a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java index 0b82649..7e620d7 100644 --- a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java +++ b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java @@ -23,7 +23,17 @@ import static handiebot.HandieBot.log; public class PlaylistCommand extends ContextCommand { public PlaylistCommand(){ - super("playlist"); + super("playlist", + " [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 [URL]... - Adds one or more songs to a playlist.\n" + + "\tremove - Removes a song from a playlist.\n" + + "\trename - Renames a playlist.\n" + + "\tmove - Moves a song from one index to another.\n" + + "\tplay - Queues all songs from a playlist."); } @Override @@ -69,7 +79,7 @@ public class PlaylistCommand extends ContextCommand { * @param channel The channel to show the error message in. */ private void incorrectMainArg(IChannel channel){ - new DisappearingMessage(channel, "Please use one of the following actions: \n``", 5000); + new DisappearingMessage(channel, "To use the playlist command: \n"+this.getUsage(channel.getGuild()), 5000); } /** diff --git a/src/main/java/handiebot/command/commands/music/QueueCommand.java b/src/main/java/handiebot/command/commands/music/QueueCommand.java index d5ab4b9..60d864e 100644 --- a/src/main/java/handiebot/command/commands/music/QueueCommand.java +++ b/src/main/java/handiebot/command/commands/music/QueueCommand.java @@ -3,7 +3,6 @@ package handiebot.command.commands.music; import handiebot.HandieBot; import handiebot.command.CommandContext; import handiebot.command.types.ContextCommand; -import handiebot.utils.DisappearingMessage; /** * @author Andrew Lalis @@ -11,7 +10,11 @@ import handiebot.utils.DisappearingMessage; */ public class QueueCommand extends ContextCommand { 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 @@ -20,8 +23,7 @@ public class QueueCommand extends ContextCommand { if (context.getArgs()[0].equals("all")){ HandieBot.musicPlayer.showQueueList(context.getGuild(), true); } else if (context.getArgs()[0].equals("clear")){ - HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.clearQueue(); - new DisappearingMessage(context.getChannel(), "Cleared the queue.", 5000); + HandieBot.musicPlayer.clearQueue(context.getGuild()); } } else { HandieBot.musicPlayer.showQueueList(context.getGuild(), false); diff --git a/src/main/java/handiebot/command/commands/music/RepeatCommand.java b/src/main/java/handiebot/command/commands/music/RepeatCommand.java index 25e0793..aa0b735 100644 --- a/src/main/java/handiebot/command/commands/music/RepeatCommand.java +++ b/src/main/java/handiebot/command/commands/music/RepeatCommand.java @@ -3,10 +3,6 @@ package handiebot.command.commands.music; import handiebot.HandieBot; import handiebot.command.CommandContext; import handiebot.command.types.ContextCommand; -import handiebot.utils.DisappearingMessage; -import handiebot.view.BotLog; - -import static handiebot.HandieBot.log; /** * @author Andrew Lalis @@ -15,7 +11,9 @@ import static handiebot.HandieBot.log; public class RepeatCommand extends ContextCommand { public RepeatCommand(){ - super("repeat"); + super("repeat", + "[true|false]", + "Sets repeating."); } @Override @@ -26,7 +24,5 @@ public class RepeatCommand extends ContextCommand { } else { 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); } } diff --git a/src/main/java/handiebot/command/commands/music/ShuffleCommand.java b/src/main/java/handiebot/command/commands/music/ShuffleCommand.java index c4f5ebf..1618909 100644 --- a/src/main/java/handiebot/command/commands/music/ShuffleCommand.java +++ b/src/main/java/handiebot/command/commands/music/ShuffleCommand.java @@ -3,10 +3,6 @@ package handiebot.command.commands.music; import handiebot.HandieBot; import handiebot.command.CommandContext; import handiebot.command.types.ContextCommand; -import handiebot.utils.DisappearingMessage; -import handiebot.view.BotLog; - -import static handiebot.HandieBot.log; /** * @author Andrew Lalis @@ -15,7 +11,9 @@ import static handiebot.HandieBot.log; public class ShuffleCommand extends ContextCommand { public ShuffleCommand(){ - super("shuffle"); + super("shuffle", + "[true|false]", + "Sets shuffling."); } @Override @@ -26,7 +24,5 @@ public class ShuffleCommand extends ContextCommand { } else { 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); } } diff --git a/src/main/java/handiebot/command/commands/music/SkipCommand.java b/src/main/java/handiebot/command/commands/music/SkipCommand.java index 69ed235..8129444 100644 --- a/src/main/java/handiebot/command/commands/music/SkipCommand.java +++ b/src/main/java/handiebot/command/commands/music/SkipCommand.java @@ -11,7 +11,9 @@ import handiebot.command.types.ContextCommand; public class SkipCommand extends ContextCommand { public SkipCommand() { - super("skip"); + super("skip", + "", + "Skips the current song."); } @Override diff --git a/src/main/java/handiebot/command/commands/music/StopCommand.java b/src/main/java/handiebot/command/commands/music/StopCommand.java new file mode 100644 index 0000000..6ab231d --- /dev/null +++ b/src/main/java/handiebot/command/commands/music/StopCommand.java @@ -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()); + } +} diff --git a/src/main/java/handiebot/command/types/Command.java b/src/main/java/handiebot/command/types/Command.java index 7f6ca26..4d69991 100644 --- a/src/main/java/handiebot/command/types/Command.java +++ b/src/main/java/handiebot/command/types/Command.java @@ -7,13 +7,25 @@ package handiebot.command.types; public abstract class Command { 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.usage = usage; + this.description = description; } public String getName(){ return this.name; }; + public String getUsage() { + return this.name+" "+this.usage; + }; + + public String getDescription() { + return this.description; + } + } diff --git a/src/main/java/handiebot/command/types/ContextCommand.java b/src/main/java/handiebot/command/types/ContextCommand.java index 6ac4e91..e059bab 100644 --- a/src/main/java/handiebot/command/types/ContextCommand.java +++ b/src/main/java/handiebot/command/types/ContextCommand.java @@ -1,6 +1,8 @@ package handiebot.command.types; import handiebot.command.CommandContext; +import handiebot.command.CommandHandler; +import sx.blah.discord.handle.obj.IGuild; /** * @author Andrew Lalis @@ -8,10 +10,19 @@ import handiebot.command.CommandContext; */ public abstract class ContextCommand extends Command { - public ContextCommand(String s) { - super(s); + public ContextCommand(String name, String usage, String description) { + super(name, usage, description); } 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(); + } + } diff --git a/src/main/java/handiebot/command/types/StaticCommand.java b/src/main/java/handiebot/command/types/StaticCommand.java index 60274dc..351e61f 100644 --- a/src/main/java/handiebot/command/types/StaticCommand.java +++ b/src/main/java/handiebot/command/types/StaticCommand.java @@ -6,8 +6,8 @@ package handiebot.command.types; */ public abstract class StaticCommand extends Command { - public StaticCommand(String s) { - super(s); + public StaticCommand(String name, String usage, String description) { + super(name, usage, description); } public abstract void execute(); diff --git a/src/main/java/handiebot/lavaplayer/MusicPlayer.java b/src/main/java/handiebot/lavaplayer/MusicPlayer.java index 95024a4..d944c39 100644 --- a/src/main/java/handiebot/lavaplayer/MusicPlayer.java +++ b/src/main/java/handiebot/lavaplayer/MusicPlayer.java @@ -3,6 +3,7 @@ package handiebot.lavaplayer; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager; import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers; +import handiebot.HandieBot; import handiebot.command.CommandHandler; import handiebot.lavaplayer.playlist.UnloadedTrack; import handiebot.utils.DisappearingMessage; @@ -25,6 +26,7 @@ import static handiebot.HandieBot.log; * @author Andrew Lalis * This class is a container for all the music related functions, and contains methods for easy playback and queue * management. + * The goal is to abstract all functions to this layer, rather than have the bot interact directly with any schedulers. */ public class MusicPlayer { @@ -113,8 +115,7 @@ public class MusicPlayer { * @param guild The guild to repeat for. */ public void toggleRepeat(IGuild guild){ - GuildMusicManager musicManager = this.getMusicManager(guild); - musicManager.scheduler.setRepeat(!musicManager.scheduler.isRepeating()); + setRepeat(guild, !getMusicManager(guild).scheduler.isRepeating()); } /** @@ -124,6 +125,8 @@ public class MusicPlayer { */ public void setRepeat(IGuild guild, boolean 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. */ public void toggleShuffle(IGuild guild){ - GuildMusicManager musicManager = this.getMusicManager(guild); - musicManager.scheduler.setShuffle(!musicManager.scheduler.isShuffling()); + setShuffle(guild, !getMusicManager(guild).scheduler.isShuffling()); } /** @@ -142,6 +144,8 @@ public class MusicPlayer { */ public void setShuffle(IGuild guild, boolean 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(); } + public void clearQueue(IGuild guild){ + getMusicManager(guild).scheduler.clearQueue(); + new DisappearingMessage(getChatChannel(guild), "Cleared the queue.", 5000); + } + /** * Skips the current track. */ @@ -228,18 +237,20 @@ public class MusicPlayer { /** * 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){ - getMusicManager(guild).scheduler.quit(); + public void stop(IGuild guild){ + 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(){ this.musicManagers.forEach((guild, musicManager) -> { - musicManager.scheduler.quit(); + musicManager.scheduler.stop(); }); this.playerManager.shutdown(); } diff --git a/src/main/java/handiebot/lavaplayer/TrackScheduler.java b/src/main/java/handiebot/lavaplayer/TrackScheduler.java index 98cd89c..edf3b9c 100644 --- a/src/main/java/handiebot/lavaplayer/TrackScheduler.java +++ b/src/main/java/handiebot/lavaplayer/TrackScheduler.java @@ -61,7 +61,7 @@ public class TrackScheduler extends AudioEventAdapter { * Clears the queue. */ public void clearQueue(){ - this.quit(); + this.stop(); this.activePlaylist.clear(); } @@ -149,19 +149,21 @@ public class TrackScheduler extends AudioEventAdapter { } player.startTrack(track, false); } 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); if (voiceChannel.isConnected()){ voiceChannel.leave(); } - this.player.stopTrack(); + if (this.player.getPlayingTrack() != null) { + this.player.stopTrack(); + } } @Override diff --git a/src/main/java/handiebot/view/CommandLineListener.java b/src/main/java/handiebot/view/CommandLineListener.java index 0179497..e43502a 100644 --- a/src/main/java/handiebot/view/CommandLineListener.java +++ b/src/main/java/handiebot/view/CommandLineListener.java @@ -41,7 +41,7 @@ public class CommandLineListener implements KeyListener { * @param args The list of arguments for the command. */ private void executeCommand(String command, String[] args){ - if (command.equals("quit")){ + if (command.equals("stop")){ new QuitAction().actionPerformed(null); } } diff --git a/src/main/java/handiebot/view/actions/QuitAction.java b/src/main/java/handiebot/view/actions/QuitAction.java index 23d9089..03f4c74 100644 --- a/src/main/java/handiebot/view/actions/QuitAction.java +++ b/src/main/java/handiebot/view/actions/QuitAction.java @@ -24,7 +24,7 @@ public class QuitAction implements ActionListener { public void actionPerformed(ActionEvent e) { if (guild != null){ HandieBot.musicPlayer.getChatChannel(this.guild).sendMessage("Quiting HandieBot"); - HandieBot.musicPlayer.quit(this.guild); + HandieBot.musicPlayer.stop(this.guild); } else { HandieBot.quit(); }