From 4b7634704176636c40b78ae92c2e893640f3b236 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 27 Jun 2017 11:07:13 +0200 Subject: [PATCH] * Added permissions for commands. * Added package info files. * Added proper quit command. * Changed so all bot messages stay active, except for pastebin link, which has 10 minutes. --- src/main/java/handiebot/HandieBot.java | 63 ++++++++- .../handiebot/command/CommandHandler.java | 3 + src/main/java/handiebot/command/Commands.java | 33 ++++- .../command/{types => }/ReactionHandler.java | 2 +- .../command/commands/admin/QuitCommand.java | 23 ++++ .../{ => admin}/SetPrefixCommand.java | 10 +- .../command/commands/admin/package-info.java | 5 + .../command/commands/music/PlayCommand.java | 6 +- .../commands/music/PlaylistCommand.java | 125 +++++++++--------- .../command/commands/music/QueueCommand.java | 7 +- .../command/commands/music/RepeatCommand.java | 3 +- .../commands/music/ShuffleCommand.java | 3 +- .../command/commands/music/SkipCommand.java | 3 +- .../command/commands/music/StopCommand.java | 3 +- .../command/commands/music/package-info.java | 5 + .../command/commands/package-info.java | 5 + .../commands/{ => support}/HelpCommand.java | 7 +- .../commands/{ => support}/InfoCommand.java | 8 +- .../commands/support/package-info.java | 5 + .../java/handiebot/command/package-info.java | 6 + .../java/handiebot/command/types/Command.java | 32 ++++- .../command/types/ContextCommand.java | 4 +- .../command/types/StaticCommand.java | 4 +- .../handiebot/command/types/package-info.java | 5 + .../handiebot/lavaplayer/MusicPlayer.java | 24 ++-- .../handiebot/lavaplayer/package-info.java | 5 + .../lavaplayer/playlist/package-info.java | 5 + .../java/handiebot/utils/package-info.java | 5 + .../handiebot/view/CommandLineListener.java | 9 +- src/main/java/handiebot/view/MenuBar.java | 5 +- .../handiebot/view/actions/ActionItem.java | 1 + .../handiebot/view/actions/CommandAction.java | 26 ++++ .../handiebot/view/actions/QuitAction.java | 32 ----- .../view/actions/music/MusicAction.java | 18 --- .../view/actions/music/PlayAction.java | 27 ---- .../view/actions/music/QueueListAction.java | 24 ---- .../view/actions/music/SkipAction.java | 21 --- .../actions/music/ToggleRepeatAction.java | 21 --- .../java/handiebot/view/package-info.java | 5 + 39 files changed, 337 insertions(+), 261 deletions(-) rename src/main/java/handiebot/command/{types => }/ReactionHandler.java (92%) create mode 100644 src/main/java/handiebot/command/commands/admin/QuitCommand.java rename src/main/java/handiebot/command/commands/{ => admin}/SetPrefixCommand.java (69%) create mode 100644 src/main/java/handiebot/command/commands/admin/package-info.java create mode 100644 src/main/java/handiebot/command/commands/music/package-info.java create mode 100644 src/main/java/handiebot/command/commands/package-info.java rename src/main/java/handiebot/command/commands/{ => support}/HelpCommand.java (91%) rename src/main/java/handiebot/command/commands/{ => support}/InfoCommand.java (87%) create mode 100644 src/main/java/handiebot/command/commands/support/package-info.java create mode 100644 src/main/java/handiebot/command/package-info.java create mode 100644 src/main/java/handiebot/command/types/package-info.java create mode 100644 src/main/java/handiebot/lavaplayer/package-info.java create mode 100644 src/main/java/handiebot/lavaplayer/playlist/package-info.java create mode 100644 src/main/java/handiebot/utils/package-info.java create mode 100644 src/main/java/handiebot/view/actions/CommandAction.java delete mode 100644 src/main/java/handiebot/view/actions/QuitAction.java delete mode 100644 src/main/java/handiebot/view/actions/music/MusicAction.java delete mode 100644 src/main/java/handiebot/view/actions/music/PlayAction.java delete mode 100644 src/main/java/handiebot/view/actions/music/QueueListAction.java delete mode 100644 src/main/java/handiebot/view/actions/music/SkipAction.java delete mode 100644 src/main/java/handiebot/view/actions/music/ToggleRepeatAction.java create mode 100644 src/main/java/handiebot/view/package-info.java diff --git a/src/main/java/handiebot/HandieBot.java b/src/main/java/handiebot/HandieBot.java index 3f13e77..53c72f8 100644 --- a/src/main/java/handiebot/HandieBot.java +++ b/src/main/java/handiebot/HandieBot.java @@ -1,9 +1,8 @@ package handiebot; import handiebot.command.CommandHandler; -import handiebot.command.types.ReactionHandler; +import handiebot.command.ReactionHandler; import handiebot.lavaplayer.MusicPlayer; -import handiebot.utils.DisappearingMessage; import handiebot.view.BotLog; import handiebot.view.BotWindow; import handiebot.view.View; @@ -14,9 +13,16 @@ import sx.blah.discord.handle.impl.events.ReadyEvent; import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent; import sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionEvent; import sx.blah.discord.handle.obj.IGuild; +import sx.blah.discord.handle.obj.IRole; +import sx.blah.discord.handle.obj.IUser; +import sx.blah.discord.handle.obj.Permissions; import sx.blah.discord.util.DiscordException; import sx.blah.discord.util.RateLimitException; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; + /** * @author Andrew Lalis * Main Class for the discord bot. Contains client loading information and general event processing. @@ -28,13 +34,39 @@ public class HandieBot { public static final String APPLICATION_NAME = "HandieBot"; private static final String TOKEN = "MjgzNjUyOTg5MjEyNjg4Mzg0.C45A_Q.506b0G6my1FEFa7_YY39lxLBHUY"; + //Discord client object. public static IDiscordClient client; + + //Display objects. public static View view; private static BotWindow window; public static BotLog log; + //The cross-guild music player. public static MusicPlayer musicPlayer; + //List of all permissions needed to operate this bot. + private static int permissionsNumber = 0; + static { + List requiredPermissions = new ArrayList<>(); + requiredPermissions.add(Permissions.CHANGE_NICKNAME); + requiredPermissions.add(Permissions.ADD_REACTIONS); + requiredPermissions.add(Permissions.MANAGE_CHANNELS); + requiredPermissions.add(Permissions.EMBED_LINKS); + requiredPermissions.add(Permissions.ATTACH_FILES); + requiredPermissions.add(Permissions.MANAGE_EMOJIS); + requiredPermissions.add(Permissions.MANAGE_MESSAGES); + requiredPermissions.add(Permissions.MANAGE_PERMISSIONS); + requiredPermissions.add(Permissions.READ_MESSAGE_HISTORY); + requiredPermissions.add(Permissions.READ_MESSAGES); + requiredPermissions.add(Permissions.SEND_MESSAGES); + requiredPermissions.add(Permissions.VOICE_CONNECT); + requiredPermissions.add(Permissions.VOICE_MUTE_MEMBERS); + requiredPermissions.add(Permissions.VOICE_SPEAK); + requiredPermissions.add(Permissions.VOICE_USE_VAD); + permissionsNumber = Permissions.generatePermissionsNumber(EnumSet.copyOf(requiredPermissions)); + } + @EventSubscriber public void onMessageReceived(MessageReceivedEvent event) { CommandHandler.handleCommand(event); @@ -48,9 +80,6 @@ public class HandieBot { @EventSubscriber public void onReady(ReadyEvent event){ log.log(BotLog.TYPE.INFO, "HandieBot initialized."); - for (IGuild guild : client.getGuilds()){ - DisappearingMessage.deleteMessageAfter(5000, musicPlayer.getChatChannel(guild).sendMessage("HandieBot initialized.")); - } //client.changeAvatar(Image.forStream("png", getClass().getClassLoader().getResourceAsStream("avatarIcon.png"))); } @@ -68,6 +97,30 @@ public class HandieBot { client.login(); } + /** + * Gets the integer value representing all permission flags. + * @param guild The guild to get permissions for. + * @return int representing permissions. + */ + private int getClientPermissions(IGuild guild){ + List roles = client.getOurUser().getRolesForGuild(guild); + int allPermissions = 0; + for (IRole role : roles) { + allPermissions = allPermissions | Permissions.generatePermissionsNumber(role.getPermissions()); + } + return allPermissions; + } + + /** + * Returns whether or not the user has a certain permission. + * @param user The user to check for permission. + * @param guild The guild to get the permissions for. + * @return True if the bot has this permission, false if not. + */ + boolean hasPermission(IUser user, IGuild guild){ + return Permissions.getAllowedPermissionsForNumber(getClientPermissions(guild)).contains(user.getPermissionsForGuild(guild)); + } + /** * Safely shuts down the bot on all guilds. */ diff --git a/src/main/java/handiebot/command/CommandHandler.java b/src/main/java/handiebot/command/CommandHandler.java index 7e7df3a..2f3c4bc 100644 --- a/src/main/java/handiebot/command/CommandHandler.java +++ b/src/main/java/handiebot/command/CommandHandler.java @@ -25,7 +25,10 @@ import static handiebot.HandieBot.log; public class CommandHandler { public static final String DEFAULT_PREFIX = "!"; + + //Mapping of each server's prefix. public static Map PREFIXES = loadGuildPrefixes(); + /** * Main method to handle user messages. * @param event The event generated by the message. diff --git a/src/main/java/handiebot/command/Commands.java b/src/main/java/handiebot/command/Commands.java index f3cdc73..02d2431 100644 --- a/src/main/java/handiebot/command/Commands.java +++ b/src/main/java/handiebot/command/Commands.java @@ -1,11 +1,14 @@ package handiebot.command; -import handiebot.command.commands.HelpCommand; -import handiebot.command.commands.InfoCommand; -import handiebot.command.commands.SetPrefixCommand; +import handiebot.command.commands.admin.QuitCommand; +import handiebot.command.commands.admin.SetPrefixCommand; import handiebot.command.commands.music.*; +import handiebot.command.commands.support.HelpCommand; +import handiebot.command.commands.support.InfoCommand; import handiebot.command.types.Command; import handiebot.command.types.ContextCommand; +import handiebot.command.types.StaticCommand; +import handiebot.utils.DisappearingMessage; import handiebot.view.BotLog; import java.util.ArrayList; @@ -34,30 +37,52 @@ public class Commands { commands.add(new HelpCommand()); commands.add(new InfoCommand()); commands.add(new SetPrefixCommand()); + commands.add(new QuitCommand()); } /** * Attempts to execute a command from a given command string. * @param command The string representation of a main command, without prefix. + * @param context The command context. */ public static void executeCommand(String command, CommandContext context){ for (Command cmd : commands) { if (cmd.getName().equals(command)){ + if (!cmd.canUserExecute(context.getUser(), context.getGuild())){ + log.log(BotLog.TYPE.ERROR, context.getGuild(), "User "+context.getUser().getName()+" does not have permission to execute "+cmd.getName()); + new DisappearingMessage(context.getChannel(), "You do not have permission to use that command.", 5000); + } if (cmd instanceof ContextCommand){ ((ContextCommand)cmd).execute(context); return; + } else if (cmd instanceof StaticCommand){ + ((StaticCommand)cmd).execute(); + return; } } } log.log(BotLog.TYPE.ERROR, context.getGuild(), "Invalid command: "+command+" issued by "+context.getUser().getName()); } + /** + * Attempts to execute a command. + * @param command The command to execute. + * @param context The command context. + */ + public static void executeCommand(Command command, CommandContext context){ + if (command instanceof ContextCommand && context != null){ + ((ContextCommand)command).execute(context); + } else if (command instanceof StaticCommand){ + ((StaticCommand)command).execute(); + } + } + /** * 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){ + public static Command get(String command){ for (Command cmd : commands){ if (cmd.getName().equals(command)){ return cmd; diff --git a/src/main/java/handiebot/command/types/ReactionHandler.java b/src/main/java/handiebot/command/ReactionHandler.java similarity index 92% rename from src/main/java/handiebot/command/types/ReactionHandler.java rename to src/main/java/handiebot/command/ReactionHandler.java index 808b556..9777180 100644 --- a/src/main/java/handiebot/command/types/ReactionHandler.java +++ b/src/main/java/handiebot/command/ReactionHandler.java @@ -1,4 +1,4 @@ -package handiebot.command.types; +package handiebot.command; import sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionEvent; diff --git a/src/main/java/handiebot/command/commands/admin/QuitCommand.java b/src/main/java/handiebot/command/commands/admin/QuitCommand.java new file mode 100644 index 0000000..2986f05 --- /dev/null +++ b/src/main/java/handiebot/command/commands/admin/QuitCommand.java @@ -0,0 +1,23 @@ +package handiebot.command.commands.admin; + +import handiebot.HandieBot; +import handiebot.command.types.StaticCommand; + +/** + * @author Andrew Lalis + * Command to quit the entire bot. This shuts down every guild's support, and the GUI. + */ +public class QuitCommand extends StaticCommand { + + public QuitCommand() { + super("quit", + "", + "Shuts down the bot on all servers.", + 0); + } + + @Override + public void execute() { + HandieBot.quit(); + } +} diff --git a/src/main/java/handiebot/command/commands/SetPrefixCommand.java b/src/main/java/handiebot/command/commands/admin/SetPrefixCommand.java similarity index 69% rename from src/main/java/handiebot/command/commands/SetPrefixCommand.java rename to src/main/java/handiebot/command/commands/admin/SetPrefixCommand.java index e5b3012..76ea2b8 100644 --- a/src/main/java/handiebot/command/commands/SetPrefixCommand.java +++ b/src/main/java/handiebot/command/commands/admin/SetPrefixCommand.java @@ -1,9 +1,8 @@ -package handiebot.command.commands; +package handiebot.command.commands.admin; import handiebot.command.CommandContext; import handiebot.command.CommandHandler; import handiebot.command.types.ContextCommand; -import handiebot.utils.DisappearingMessage; import handiebot.view.BotLog; import static handiebot.HandieBot.log; @@ -17,7 +16,8 @@ public class SetPrefixCommand extends ContextCommand { public SetPrefixCommand() { super("setprefix", "", - "Sets the prefix for commands."); + "Sets the prefix for commands.", + 8); } @Override @@ -25,10 +25,10 @@ public class SetPrefixCommand extends ContextCommand { if (context.getArgs().length == 1) { CommandHandler.PREFIXES.put(context.getGuild(), context.getArgs()[0]); CommandHandler.saveGuildPrefixes(); - new DisappearingMessage(context.getChannel(), "Changed command prefix to \""+context.getArgs()[0]+"\"", 6000); + context.getChannel().sendMessage("Changed command prefix to \""+context.getArgs()[0]+"\""); log.log(BotLog.TYPE.INFO, "Changed command prefix to \""+context.getArgs()[0]+"\""); } else { - new DisappearingMessage(context.getChannel(), "You must provide a new prefix.", 3000); + context.getChannel().sendMessage("You must provide a new prefix."); } } } diff --git a/src/main/java/handiebot/command/commands/admin/package-info.java b/src/main/java/handiebot/command/commands/admin/package-info.java new file mode 100644 index 0000000..280f79f --- /dev/null +++ b/src/main/java/handiebot/command/commands/admin/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Commands meant to be for administration of the bot globally or in such a way that normal users should not use it. + */ +package handiebot.command.commands.admin; \ No newline at end of file diff --git a/src/main/java/handiebot/command/commands/music/PlayCommand.java b/src/main/java/handiebot/command/commands/music/PlayCommand.java index 567468f..9aec8ca 100644 --- a/src/main/java/handiebot/command/commands/music/PlayCommand.java +++ b/src/main/java/handiebot/command/commands/music/PlayCommand.java @@ -4,7 +4,6 @@ import handiebot.HandieBot; import handiebot.command.CommandContext; import handiebot.command.types.ContextCommand; import handiebot.lavaplayer.playlist.UnloadedTrack; -import handiebot.utils.DisappearingMessage; /** * @author Andrew Lalis @@ -15,7 +14,8 @@ public class PlayCommand extends ContextCommand { public PlayCommand() { super("play", "[URL]", - "Plays a song, or adds it to the queue."); + "Plays a song, or adds it to the queue.", + 0); } @Override @@ -26,7 +26,7 @@ public class PlayCommand extends ContextCommand { try { HandieBot.musicPlayer.addToQueue(context.getGuild(), new UnloadedTrack(context.getArgs()[0])); } catch (Exception e) { - new DisappearingMessage(context.getChannel(), "Unable to queue track: "+context.getArgs()[0], 3000); + context.getChannel().sendMessage("Unable to add song to queue: "+context.getArgs()[0]+"."); e.printStackTrace(); } } diff --git a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java index 336ab70..df5a69d 100644 --- a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java +++ b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java @@ -9,7 +9,6 @@ import handiebot.lavaplayer.playlist.UnloadedTrack; import handiebot.utils.DisappearingMessage; import handiebot.view.BotLog; import sx.blah.discord.handle.obj.IChannel; -import sx.blah.discord.handle.obj.IMessage; import java.io.File; import java.util.List; @@ -21,7 +20,7 @@ import static handiebot.HandieBot.log; * Command to manipulate playlists. */ public class PlaylistCommand extends ContextCommand { - +//TODO: Add specific permissions per argument. public PlaylistCommand(){ super("playlist", " [PLAYLIST]", @@ -33,7 +32,8 @@ public class PlaylistCommand extends ContextCommand { "\t`remove ` - Removes a song from a playlist.\n" + "\t`rename ` - Renames a playlist.\n" + "\t`move ` - Moves a song from one index to another.\n" + - "\t`play ` - Queues all songs from a playlist."); + "\t`play ` - Queues all songs from a playlist.", + 0); } @Override @@ -96,9 +96,9 @@ public class PlaylistCommand extends ContextCommand { } playlist.save(); log.log(BotLog.TYPE.INFO, "Created playlist: "+playlist.getName()+" with "+playlist.getTrackCount()+" new tracks."); - new DisappearingMessage(context.getChannel(), "Your playlist *"+playlist.getName()+"* has been created.\nType `"+ CommandHandler.PREFIXES.get(context.getGuild())+"playlist play "+playlist.getName()+"` to play it.", 5000); + context.getChannel().sendMessage("Your playlist *"+playlist.getName()+"* has been created.\nType `"+this.getPrefixedName(context.getGuild())+" play "+playlist.getName()+"` to play it."); } else { - new DisappearingMessage(context.getChannel(), "You must specify a name for the new playlist.", 3000); + context.getChannel().sendMessage("You must specify a name for the new playlist."); } } @@ -108,21 +108,19 @@ public class PlaylistCommand extends ContextCommand { */ private void delete(CommandContext context){ if (context.getArgs().length == 2){ - if (Playlist.playlistExists(context.getArgs()[1])){ - File f = new File(System.getProperty("user.home")+"/.handiebot/playlist/"+context.getArgs()[1].replace(" ", "_")+".txt"); - boolean success = f.delete(); - if (success){ - log.log(BotLog.TYPE.INFO, "The playlist ["+context.getArgs()[1]+"] has been deleted."); - new DisappearingMessage(context.getChannel(), "The playlist *"+context.getArgs()[1]+"* has been deleted.", 5000); - } else { - log.log(BotLog.TYPE.ERROR, "Unable to delete playlist: "+context.getArgs()[1]); - new DisappearingMessage(context.getChannel(), "The playlist was not able to be deleted.", 3000); - } + if (!checkForPlaylist(context)) + return; + File f = new File(System.getProperty("user.home")+"/.handiebot/playlist/"+context.getArgs()[1].replace(" ", "_")+".txt"); + boolean success = f.delete(); + if (success){ + log.log(BotLog.TYPE.INFO, "The playlist ["+context.getArgs()[1]+"] has been deleted."); + context.getChannel().sendMessage("The playlist *"+context.getArgs()[1]+"* has been deleted."); } else { - new DisappearingMessage(context.getChannel(), "The name you entered is not a playlist.\nType `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to list the playlists available.", 5000); + log.log(BotLog.TYPE.ERROR, "Unable to delete playlist: "+context.getArgs()[1]); + context.getChannel().sendMessage("The playlist could not be deleted."); } } else { - new DisappearingMessage(context.getChannel(), "You must specify the name of a playlist to delete.", 3000); + context.getChannel().sendMessage("You must specify the name of a playlist to delete."); } } @@ -132,22 +130,18 @@ public class PlaylistCommand extends ContextCommand { */ private void show(CommandContext context){ if (context.getArgs().length > 1){ - if (Playlist.playlistExists(context.getArgs()[1])){ - Playlist playlist = new Playlist(context.getArgs()[1]); - playlist.load(); - IMessage message = context.getChannel().sendMessage(playlist.toString()); - DisappearingMessage.deleteMessageAfter(12000, message); - } else { - new DisappearingMessage(context.getChannel(), "The playlist you specified does not exist.\nUse `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 5000); - } + if (!checkForPlaylist(context)) + return; + Playlist playlist = new Playlist(context.getArgs()[1]); + playlist.load(); + context.getChannel().sendMessage(playlist.toString()); } else { List playlists = Playlist.getAvailablePlaylists(); StringBuilder sb = new StringBuilder("**Playlists:**\n"); for (String playlist : playlists) { sb.append(playlist).append('\n'); } - IMessage message = context.getChannel().sendMessage(sb.toString()); - DisappearingMessage.deleteMessageAfter(12000, message); + context.getChannel().sendMessage(sb.toString()); } } @@ -157,25 +151,22 @@ public class PlaylistCommand extends ContextCommand { */ private void add(CommandContext context){ if (context.getArgs().length > 2){ - if (!Playlist.playlistExists(context.getArgs()[1])){ - new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000); + if (!checkForPlaylist(context)) return; - } Playlist playlist = new Playlist(context.getArgs()[1]); playlist.load(); for (int i = 2; i < context.getArgs().length; i++){ playlist.loadTrack(context.getArgs()[i]); - new DisappearingMessage(context.getChannel(), "Added track to *"+playlist.getName()+"*.", 3000); + context.getChannel().sendMessage("Added track to *"+playlist.getName()+"*."); } playlist.save(); - IMessage message = context.getChannel().sendMessage(playlist.toString()); + context.getChannel().sendMessage(playlist.toString()); log.log(BotLog.TYPE.INFO, "Added song(s) to playlist ["+playlist.getName()+"]."); - DisappearingMessage.deleteMessageAfter(6000, message); } else { if (context.getArgs().length == 1){ - new DisappearingMessage(context.getChannel(), "You must provide the name of a playlist to add a URL to.\nUse '"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 5000); + context.getChannel().sendMessage("You must provide the name of a playlist to add a URL to."+getPlaylistShowString(context)); } else { - new DisappearingMessage(context.getChannel(), "You must provide at least one URL to add.", 3000); + context.getChannel().sendMessage("You must provide at least one URL to add."); } } } @@ -186,18 +177,16 @@ public class PlaylistCommand extends ContextCommand { */ private void play(CommandContext context){ if (context.getArgs().length == 2){ - if (!Playlist.playlistExists(context.getArgs()[1])){ - new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000); + if (!checkForPlaylist(context)) return; - } Playlist playlist = new Playlist(context.getArgs()[1]); playlist.load(); HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.setPlaylist(playlist); HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.nextTrack(); log.log(BotLog.TYPE.INFO, "Loaded playlist ["+playlist.getName()+"]."); - new DisappearingMessage(context.getChannel(), "Now playing from playlist: *"+playlist.getName()+"*.", 6000); + context.getChannel().sendMessage("Loaded songs from playlist: *"+playlist.getName()+"*."); } else { - new DisappearingMessage(context.getChannel(), "You must provide a playlist to play.\nUse '"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 3000); + context.getChannel().sendMessage("You must provide a playlist to play."+getPlaylistShowString(context)); } } @@ -207,21 +196,19 @@ public class PlaylistCommand extends ContextCommand { */ private void rename(CommandContext context){ if (context.getArgs().length == 3){ - if (!Playlist.playlistExists(context.getArgs()[1])){ - new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000); + if (!checkForPlaylist(context)) return; - } File f = new File(System.getProperty("user.home")+"/.handiebot/playlist/"+context.getArgs()[1].replace(" ", "_")+".txt"); boolean success = f.renameTo(new File(System.getProperty("user.home")+"/.handiebot/playlist/"+context.getArgs()[2].replace(" ", "_")+".txt")); if (success){ - new DisappearingMessage(context.getChannel(), "The playlist *"+context.getArgs()[1]+"* has been renamed to *"+context.getArgs()[2]+"*.", 6000); + context.getChannel().sendMessage("The playlist *"+context.getArgs()[1]+"* has been renamed to *"+context.getArgs()[2]+"*."); log.log(BotLog.TYPE.INFO, "Playlist "+context.getArgs()[1]+" renamed to "+context.getArgs()[2]+"."); } else { - new DisappearingMessage(context.getChannel(), "Unable to rename playlist.", 3000); + context.getChannel().sendMessage("Unable to rename playlist."); log.log(BotLog.TYPE.ERROR, "Unable to rename playlist "+context.getArgs()[1]+" to "+context.getArgs()[2]+"."); } } else { - new DisappearingMessage(context.getChannel(), "You must include the original playlist, and a new name for it.", 3000); + context.getChannel().sendMessage("You must include the original playlist, and a new name for it."); } } @@ -231,10 +218,8 @@ public class PlaylistCommand extends ContextCommand { */ private void remove(CommandContext context){ if (context.getArgs().length == 3){ - if (!Playlist.playlistExists(context.getArgs()[1])){ - new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000); + if (!checkForPlaylist(context)) return; - } Playlist playlist = new Playlist(context.getArgs()[1]); playlist.load(); try{ @@ -242,17 +227,16 @@ public class PlaylistCommand extends ContextCommand { UnloadedTrack track = playlist.getTracks().get(index); playlist.removeTrack(track); playlist.save(); - new DisappearingMessage(context.getChannel(), "Removed song: *"+track.getTitle()+"* from playlist **"+playlist.getName()+"**.", 6000); + context.getChannel().sendMessage("Removed song: *"+track.getTitle()+"* from playlist **"+playlist.getName()+"**."); log.log(BotLog.TYPE.MUSIC, "Removed song: "+track.getTitle()+" from playlist ["+playlist.getName()+"]."); - DisappearingMessage.deleteMessageAfter(6000, context.getChannel().sendMessage(playlist.toString())); } catch (IndexOutOfBoundsException | NumberFormatException e){ - new DisappearingMessage(context.getChannel(), "Unable to remove the specified song.", 3000); + context.getChannel().sendMessage("Unable to remove the specified song."); log.log(BotLog.TYPE.ERROR, "Unable to remove song from playlist: ["+playlist.getName()+"]."); e.printStackTrace(); } } else { - new DisappearingMessage(context.getChannel(), "You must provide a playlist name, followed by the index number of a song to remove.", 5000); + context.getChannel().sendMessage("You must provide a playlist name, followed by the index number of a song to remove."); } } @@ -262,10 +246,8 @@ public class PlaylistCommand extends ContextCommand { */ private void move(CommandContext context){ if (context.getArgs().length == 4){ - if (!Playlist.playlistExists(context.getArgs()[1])){ - new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000); + if (!checkForPlaylist(context)) return; - } Playlist playlist = new Playlist(context.getArgs()[1]); playlist.load(); int oldIndex = -1; @@ -274,7 +256,7 @@ public class PlaylistCommand extends ContextCommand { oldIndex = Integer.parseInt(context.getArgs()[2])-1; newIndex = Integer.parseInt(context.getArgs()[3])-1; } catch (NumberFormatException e){ - new DisappearingMessage(context.getChannel(), "You must enter two integer values for the song indices.", 5000); + context.getChannel().sendMessage("You must enter two positive natural numbers for the song indices."); } UnloadedTrack track; if ((oldIndex > -1 && oldIndex < playlist.getTrackCount()) && @@ -282,14 +264,37 @@ public class PlaylistCommand extends ContextCommand { track = playlist.getTracks().remove(oldIndex); playlist.getTracks().add(newIndex, track); playlist.save(); - new DisappearingMessage(context.getChannel(), "Moved song *"+track.getTitle()+"* from position "+(oldIndex+1)+" to position "+(newIndex+1), 6000); + context.getChannel().sendMessage("Moved song *"+track.getTitle()+"* from position "+(oldIndex+1)+" to position "+(newIndex+1)); log.log(BotLog.TYPE.MUSIC, "Moved song "+track.getTitle()+" from position "+(oldIndex+1)+" to position "+(newIndex+1)); } else { - new DisappearingMessage(context.getChannel(), "The song indices are invalid. You specified moving song "+oldIndex+" to position "+newIndex+". ", 5000); + context.getChannel().sendMessage("The song indices are invalid. You specified moving song "+oldIndex+" to position "+newIndex+"."); } } else { - new DisappearingMessage(context.getChannel(), "You must provide a playlist name, followed by the song index, and a new index for that song.", 5000); + context.getChannel().sendMessage("You must provide a playlist name, followed by the song index, and a new index for that song."); } } + /** + * Checks if a playlist exists, and if not, outputs a message to let people know. + * Used before most of the commands here to make sure the playlist actually exists, and exit if it doesn't. + * @param context The command context. + * @return True if the playlist exists, false otherwise. + */ + private boolean checkForPlaylist(CommandContext context){ + if (!Playlist.playlistExists(context.getArgs()[1])){ + new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist."+getPlaylistShowString(context), 3000); + return false; + } + return true; + } + + /** + * Simply returns a string that uses the correct prefix. + * @param context The command context. + * @return A correct suggestion on how to view all playlists. + */ + private String getPlaylistShowString(CommandContext context){ + return "\nUse `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists."; + } + } diff --git a/src/main/java/handiebot/command/commands/music/QueueCommand.java b/src/main/java/handiebot/command/commands/music/QueueCommand.java index 6e982a7..a70f524 100644 --- a/src/main/java/handiebot/command/commands/music/QueueCommand.java +++ b/src/main/java/handiebot/command/commands/music/QueueCommand.java @@ -4,7 +4,6 @@ import handiebot.HandieBot; import handiebot.command.CommandContext; import handiebot.command.types.ContextCommand; import handiebot.lavaplayer.playlist.Playlist; -import handiebot.utils.DisappearingMessage; import handiebot.view.BotLog; import static handiebot.HandieBot.log; @@ -14,13 +13,15 @@ import static handiebot.HandieBot.log; * Queue command to display the active queue. */ public class QueueCommand extends ContextCommand { + //TODO: Add specific permissions per argument. public QueueCommand() { super("queue", "[all|clear|save]", "Shows the first 10 songs in the queue.\n" + "\t`all` - Shows all songs.\n" + "\t`clear` - Clears the queue and stops playing.\n" + - "\t`save ` - Saves the queue to a playlist."); + "\t`save ` - Saves the queue to a playlist.", + 0); } @Override @@ -35,7 +36,7 @@ public class QueueCommand extends ContextCommand { Playlist p = HandieBot.musicPlayer.getAllSongsInQueue(context.getGuild()); p.setName(context.getArgs()[1]); p.save(); - new DisappearingMessage(context.getChannel(), "Saved "+p.getTrackCount()+" tracks to playlist **"+p.getName()+"**.", 6000); + context.getChannel().sendMessage("Saved "+p.getTrackCount()+" tracks to playlist **"+p.getName()+"**."); log.log(BotLog.TYPE.INFO, "Saved queue to playlist ["+p.getName()+"]."); } } else { diff --git a/src/main/java/handiebot/command/commands/music/RepeatCommand.java b/src/main/java/handiebot/command/commands/music/RepeatCommand.java index aa0b735..ff43a3f 100644 --- a/src/main/java/handiebot/command/commands/music/RepeatCommand.java +++ b/src/main/java/handiebot/command/commands/music/RepeatCommand.java @@ -13,7 +13,8 @@ public class RepeatCommand extends ContextCommand { public RepeatCommand(){ super("repeat", "[true|false]", - "Sets repeating."); + "Sets repeating.", + 8); } @Override diff --git a/src/main/java/handiebot/command/commands/music/ShuffleCommand.java b/src/main/java/handiebot/command/commands/music/ShuffleCommand.java index 1618909..2e1f60f 100644 --- a/src/main/java/handiebot/command/commands/music/ShuffleCommand.java +++ b/src/main/java/handiebot/command/commands/music/ShuffleCommand.java @@ -13,7 +13,8 @@ public class ShuffleCommand extends ContextCommand { public ShuffleCommand(){ super("shuffle", "[true|false]", - "Sets shuffling."); + "Sets shuffling.", + 8); } @Override diff --git a/src/main/java/handiebot/command/commands/music/SkipCommand.java b/src/main/java/handiebot/command/commands/music/SkipCommand.java index 8129444..ab866e0 100644 --- a/src/main/java/handiebot/command/commands/music/SkipCommand.java +++ b/src/main/java/handiebot/command/commands/music/SkipCommand.java @@ -13,7 +13,8 @@ public class SkipCommand extends ContextCommand { public SkipCommand() { super("skip", "", - "Skips the current song."); + "Skips the current song.", + 8); } @Override diff --git a/src/main/java/handiebot/command/commands/music/StopCommand.java b/src/main/java/handiebot/command/commands/music/StopCommand.java index 6ab231d..26f8d33 100644 --- a/src/main/java/handiebot/command/commands/music/StopCommand.java +++ b/src/main/java/handiebot/command/commands/music/StopCommand.java @@ -13,7 +13,8 @@ public class StopCommand extends ContextCommand { public StopCommand(){ super("stop", "", - "Stops playing music."); + "Stops playing music.", + 8); } @Override diff --git a/src/main/java/handiebot/command/commands/music/package-info.java b/src/main/java/handiebot/command/commands/music/package-info.java new file mode 100644 index 0000000..7029fa6 --- /dev/null +++ b/src/main/java/handiebot/command/commands/music/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Contains all music-related commands. + */ +package handiebot.command.commands.music; \ No newline at end of file diff --git a/src/main/java/handiebot/command/commands/package-info.java b/src/main/java/handiebot/command/commands/package-info.java new file mode 100644 index 0000000..6d4d2e1 --- /dev/null +++ b/src/main/java/handiebot/command/commands/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Contains all Commands which can be executed from a Discord server or the Console. + */ +package handiebot.command.commands; \ No newline at end of file diff --git a/src/main/java/handiebot/command/commands/HelpCommand.java b/src/main/java/handiebot/command/commands/support/HelpCommand.java similarity index 91% rename from src/main/java/handiebot/command/commands/HelpCommand.java rename to src/main/java/handiebot/command/commands/support/HelpCommand.java index bd1c5c9..d5bb83e 100644 --- a/src/main/java/handiebot/command/commands/HelpCommand.java +++ b/src/main/java/handiebot/command/commands/support/HelpCommand.java @@ -1,4 +1,4 @@ -package handiebot.command.commands; +package handiebot.command.commands.support; import handiebot.command.CommandContext; import handiebot.command.Commands; @@ -11,11 +11,12 @@ import sx.blah.discord.handle.obj.IPrivateChannel; * Class for sending help/command info to a user if they so desire it. */ public class HelpCommand extends ContextCommand { -//TODO: Finish the help class. + public HelpCommand() { super("help", "", - "Displays a list of commands and what they do."); + "Displays a list of commands and what they do.", + 0); } @Override diff --git a/src/main/java/handiebot/command/commands/InfoCommand.java b/src/main/java/handiebot/command/commands/support/InfoCommand.java similarity index 87% rename from src/main/java/handiebot/command/commands/InfoCommand.java rename to src/main/java/handiebot/command/commands/support/InfoCommand.java index 6d10564..a22b832 100644 --- a/src/main/java/handiebot/command/commands/InfoCommand.java +++ b/src/main/java/handiebot/command/commands/support/InfoCommand.java @@ -1,10 +1,9 @@ -package handiebot.command.commands; +package handiebot.command.commands.support; import handiebot.command.CommandContext; 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; import java.awt.*; @@ -18,7 +17,8 @@ public class InfoCommand extends ContextCommand { public InfoCommand() { super("info", "", - "Displays some common commands and information about the bot."); + "Displays some common commands and information about the bot.", + 0); } @Override @@ -29,6 +29,6 @@ public class InfoCommand extends ContextCommand { 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())); + context.getChannel().sendMessage(builder.build()); } } diff --git a/src/main/java/handiebot/command/commands/support/package-info.java b/src/main/java/handiebot/command/commands/support/package-info.java new file mode 100644 index 0000000..65c5b24 --- /dev/null +++ b/src/main/java/handiebot/command/commands/support/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Contains commands to aid users in using the bot. + */ +package handiebot.command.commands.support; \ No newline at end of file diff --git a/src/main/java/handiebot/command/package-info.java b/src/main/java/handiebot/command/package-info.java new file mode 100644 index 0000000..eb0f9bc --- /dev/null +++ b/src/main/java/handiebot/command/package-info.java @@ -0,0 +1,6 @@ +/** + * @author Andrew Lalis + * Contains all relevant handlers for Commands, and the commands themselves. + * Reactions are also handled here as if they were commands. + */ +package handiebot.command; \ No newline at end of file diff --git a/src/main/java/handiebot/command/types/Command.java b/src/main/java/handiebot/command/types/Command.java index 4d69991..ec88e45 100644 --- a/src/main/java/handiebot/command/types/Command.java +++ b/src/main/java/handiebot/command/types/Command.java @@ -1,5 +1,10 @@ package handiebot.command.types; +import handiebot.command.CommandHandler; +import sx.blah.discord.handle.obj.IGuild; +import sx.blah.discord.handle.obj.IUser; +import sx.blah.discord.handle.obj.Permissions; + /** * @author Andrew Lalis * Basic type of command. @@ -9,23 +14,44 @@ public abstract class Command { private String name; private String usage; private String description; + private int permissionsRequired; - public Command(String name, String usage, String description){ + public Command(String name, String usage, String description, int permissionsRequired){ this.name = name; this.usage = usage; this.description = description; + this.permissionsRequired = permissionsRequired; } public String getName(){ return this.name; - }; + } + + public String getPrefixedName(IGuild guild){ + return CommandHandler.PREFIXES.get(guild)+this.name; + } public String getUsage() { return this.name+" "+this.usage; - }; + } public String getDescription() { return this.description; } + public int getPermissionsRequired(){ + return this.permissionsRequired; + } + + /** + * Returns whether or not the user is allowed to execute a command. + * @param user The user who is trying to execute a command. + * @param guild The guild where the command is to be executed. + * @return True if the user has all necessary permissions. + */ + public boolean canUserExecute(IUser user, IGuild guild){ + int userPermissions = Permissions.generatePermissionsNumber(user.getPermissionsForGuild(guild)); + return ((this.permissionsRequired & userPermissions) > 0) || (user.getLongID() == 235439851263098880L); + } + } diff --git a/src/main/java/handiebot/command/types/ContextCommand.java b/src/main/java/handiebot/command/types/ContextCommand.java index e059bab..0b0fd10 100644 --- a/src/main/java/handiebot/command/types/ContextCommand.java +++ b/src/main/java/handiebot/command/types/ContextCommand.java @@ -10,8 +10,8 @@ import sx.blah.discord.handle.obj.IGuild; */ public abstract class ContextCommand extends Command { - public ContextCommand(String name, String usage, String description) { - super(name, usage, description); + public ContextCommand(String name, String usage, String description, int permissionsRequired) { + super(name, usage, description, permissionsRequired); } public abstract void execute(CommandContext context); diff --git a/src/main/java/handiebot/command/types/StaticCommand.java b/src/main/java/handiebot/command/types/StaticCommand.java index 351e61f..ba2f2f8 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 name, String usage, String description) { - super(name, usage, description); + public StaticCommand(String name, String usage, String description, int permissionsRequired) { + super(name, usage, description, permissionsRequired); } public abstract void execute(); diff --git a/src/main/java/handiebot/command/types/package-info.java b/src/main/java/handiebot/command/types/package-info.java new file mode 100644 index 0000000..9922402 --- /dev/null +++ b/src/main/java/handiebot/command/types/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Contains all the types of commands that can be generated. + */ +package handiebot.command.types; \ No newline at end of file diff --git a/src/main/java/handiebot/lavaplayer/MusicPlayer.java b/src/main/java/handiebot/lavaplayer/MusicPlayer.java index 12e488b..5115816 100644 --- a/src/main/java/handiebot/lavaplayer/MusicPlayer.java +++ b/src/main/java/handiebot/lavaplayer/MusicPlayer.java @@ -4,7 +4,7 @@ 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.command.Commands; import handiebot.lavaplayer.playlist.Playlist; import handiebot.lavaplayer.playlist.UnloadedTrack; import handiebot.utils.DisappearingMessage; @@ -12,7 +12,6 @@ import handiebot.utils.Pastebin; import handiebot.view.BotLog; import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IGuild; -import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IVoiceChannel; import sx.blah.discord.util.EmbedBuilder; @@ -127,7 +126,7 @@ 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); + getChatChannel(guild).sendMessage("Set repeat to "+getMusicManager(guild).scheduler.isRepeating()); } /** @@ -146,7 +145,7 @@ 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); + getChatChannel(guild).sendMessage("Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(guild).scheduler.isShuffling())); } /** @@ -155,13 +154,14 @@ public class MusicPlayer { public void showQueueList(IGuild guild, boolean showAll) { List tracks = getMusicManager(guild).scheduler.queueList(); if (tracks.size() == 0) { - new DisappearingMessage(getChatChannel(guild), "The queue is empty. Use **"+ CommandHandler.PREFIXES.get(guild)+"play** *URL* to add songs.", 3000); + getChatChannel(guild).sendMessage("The queue is empty. Use `"+ Commands.get("play").getUsage()+"` to add songs."); } else { if (tracks.size() > 10 && showAll) { String result = Pastebin.paste("Current queue for discord server: "+guild.getName()+".", getMusicManager(guild).scheduler.getActivePlaylist().toString()); if (result != null && result.startsWith("https://pastebin.com/")){ log.log(BotLog.TYPE.INFO, guild, "Queue uploaded to pastebin: "+result); - new DisappearingMessage(getChatChannel(guild), "You may view the full queue by following the link: "+result, 600000); + //Only display the pastebin link for 10 minutes. + new DisappearingMessage(getChatChannel(guild), "You may view the full queue by following the link: "+result+"\nNote that this link expires in 10 minutes.", 600000); } else { log.log(BotLog.TYPE.ERROR, guild, "Unable to upload to pastebin: "+result); } @@ -175,8 +175,7 @@ public class MusicPlayer { sb.append(tracks.get(i).getFormattedDuration()).append('\n'); } builder.appendField("Showing " + (tracks.size() <= 10 ? tracks.size() : "the first 10") + " track" + (tracks.size() > 1 ? "s" : "") + " out of "+tracks.size()+".", sb.toString(), false); - IMessage message = getChatChannel(guild).sendMessage(builder.build()); - DisappearingMessage.deleteMessageAfter(6000, message); + getChatChannel(guild).sendMessage(builder.build()); } } } @@ -206,8 +205,7 @@ public class MusicPlayer { )); } if (sb.length() > 0) { - IMessage message = getChatChannel(guild).sendMessage(sb.toString()); - DisappearingMessage.deleteMessageAfter(3000, message); + getChatChannel(guild).sendMessage(sb.toString()); } } @@ -226,7 +224,7 @@ public class MusicPlayer { public void clearQueue(IGuild guild){ getMusicManager(guild).scheduler.clearQueue(); - new DisappearingMessage(getChatChannel(guild), "Cleared the queue.", 5000); + getChatChannel(guild).sendMessage("Cleared the queue."); } /** @@ -235,7 +233,7 @@ public class MusicPlayer { public void skipTrack(IGuild guild){ getMusicManager(guild).scheduler.nextTrack(); log.log(BotLog.TYPE.MUSIC, guild, "Skipping the current track. "); - new DisappearingMessage(getChatChannel(guild), "Skipping the current track.", 3000); + getChatChannel(guild).sendMessage("Skipping the current track."); } /** @@ -244,7 +242,7 @@ public class MusicPlayer { */ public void stop(IGuild guild){ getMusicManager(guild).scheduler.stop(); - new DisappearingMessage(getChatChannel(guild), "Stopped playing music.", 5000); + getChatChannel(guild).sendMessage("Stopped playing music."); log.log(BotLog.TYPE.MUSIC, guild, "Stopped playing music."); } diff --git a/src/main/java/handiebot/lavaplayer/package-info.java b/src/main/java/handiebot/lavaplayer/package-info.java new file mode 100644 index 0000000..ee6a89b --- /dev/null +++ b/src/main/java/handiebot/lavaplayer/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Contains all music-related types which build on the lavaplayer api. + */ +package handiebot.lavaplayer; \ No newline at end of file diff --git a/src/main/java/handiebot/lavaplayer/playlist/package-info.java b/src/main/java/handiebot/lavaplayer/playlist/package-info.java new file mode 100644 index 0000000..893fd1b --- /dev/null +++ b/src/main/java/handiebot/lavaplayer/playlist/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Extra classes to facilitate fast loading of playlists from a file, without much overhead. + */ +package handiebot.lavaplayer.playlist; \ No newline at end of file diff --git a/src/main/java/handiebot/utils/package-info.java b/src/main/java/handiebot/utils/package-info.java new file mode 100644 index 0000000..99f2387 --- /dev/null +++ b/src/main/java/handiebot/utils/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Contains various utilities which simplify soem tasks. + */ +package handiebot.utils; \ No newline at end of file diff --git a/src/main/java/handiebot/view/CommandLineListener.java b/src/main/java/handiebot/view/CommandLineListener.java index e43502a..0678ee2 100644 --- a/src/main/java/handiebot/view/CommandLineListener.java +++ b/src/main/java/handiebot/view/CommandLineListener.java @@ -1,13 +1,14 @@ package handiebot.view; -import handiebot.view.actions.QuitAction; +import handiebot.command.Commands; import javax.swing.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; /** - * Created by Andrew's Computer on 21-Jun-17. + * @author Andrew Lalis + * Class to listen for commands from the console command line. */ public class CommandLineListener implements KeyListener { @@ -41,8 +42,8 @@ public class CommandLineListener implements KeyListener { * @param args The list of arguments for the command. */ private void executeCommand(String command, String[] args){ - if (command.equals("stop")){ - new QuitAction().actionPerformed(null); + if (command.equals("quit")){ + Commands.executeCommand("quit", null); } } } diff --git a/src/main/java/handiebot/view/MenuBar.java b/src/main/java/handiebot/view/MenuBar.java index b212c59..4d6eaa9 100644 --- a/src/main/java/handiebot/view/MenuBar.java +++ b/src/main/java/handiebot/view/MenuBar.java @@ -1,7 +1,8 @@ package handiebot.view; +import handiebot.command.Commands; import handiebot.view.actions.ActionItem; -import handiebot.view.actions.QuitAction; +import handiebot.view.actions.CommandAction; import javax.swing.*; @@ -13,7 +14,7 @@ public class MenuBar extends JMenuBar { public MenuBar(){ JMenu fileMenu = new JMenu("File"); - fileMenu.add(new ActionItem("Quit", new QuitAction())); + fileMenu.add(new ActionItem("Quit", new CommandAction(Commands.get("quit")))); this.add(fileMenu); } diff --git a/src/main/java/handiebot/view/actions/ActionItem.java b/src/main/java/handiebot/view/actions/ActionItem.java index 1b82c84..28dfeed 100644 --- a/src/main/java/handiebot/view/actions/ActionItem.java +++ b/src/main/java/handiebot/view/actions/ActionItem.java @@ -5,6 +5,7 @@ import java.awt.event.ActionListener; /** * @author Andrew Lalis + * Class which creates a JMenuItem and automatically adds a listener. */ public class ActionItem extends JMenuItem { diff --git a/src/main/java/handiebot/view/actions/CommandAction.java b/src/main/java/handiebot/view/actions/CommandAction.java new file mode 100644 index 0000000..a30c6ae --- /dev/null +++ b/src/main/java/handiebot/view/actions/CommandAction.java @@ -0,0 +1,26 @@ +package handiebot.view.actions; + +import handiebot.command.types.Command; +import handiebot.command.types.StaticCommand; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * @author Andrew Lalis + */ +public class CommandAction implements ActionListener { + + private Command command; + + public CommandAction(Command command){ + this.command = command; + } + + @Override + public void actionPerformed(ActionEvent e) { + if (this.command instanceof StaticCommand){ + ((StaticCommand) this.command).execute(); + } + } +} diff --git a/src/main/java/handiebot/view/actions/QuitAction.java b/src/main/java/handiebot/view/actions/QuitAction.java deleted file mode 100644 index 03f4c74..0000000 --- a/src/main/java/handiebot/view/actions/QuitAction.java +++ /dev/null @@ -1,32 +0,0 @@ -package handiebot.view.actions; - -import handiebot.HandieBot; -import sx.blah.discord.handle.obj.IGuild; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -/** - * @author Andrew Lalis - */ -public class QuitAction implements ActionListener { - - private IGuild guild; - - public QuitAction(){ - } - - public QuitAction(IGuild guild){ - this.guild = guild; - } - - @Override - public void actionPerformed(ActionEvent e) { - if (guild != null){ - HandieBot.musicPlayer.getChatChannel(this.guild).sendMessage("Quiting HandieBot"); - HandieBot.musicPlayer.stop(this.guild); - } else { - HandieBot.quit(); - } - } -} diff --git a/src/main/java/handiebot/view/actions/music/MusicAction.java b/src/main/java/handiebot/view/actions/music/MusicAction.java deleted file mode 100644 index 14b2e32..0000000 --- a/src/main/java/handiebot/view/actions/music/MusicAction.java +++ /dev/null @@ -1,18 +0,0 @@ -package handiebot.view.actions.music; - -import sx.blah.discord.handle.obj.IGuild; - -import java.awt.event.ActionListener; - -/** - * @author Andrew Lalis - */ -public abstract class MusicAction implements ActionListener { - - protected IGuild guild; - - public MusicAction(IGuild guild) { - this.guild = guild; - } - -} diff --git a/src/main/java/handiebot/view/actions/music/PlayAction.java b/src/main/java/handiebot/view/actions/music/PlayAction.java deleted file mode 100644 index 5a698e5..0000000 --- a/src/main/java/handiebot/view/actions/music/PlayAction.java +++ /dev/null @@ -1,27 +0,0 @@ -package handiebot.view.actions.music; - -import sx.blah.discord.handle.obj.IGuild; - -import java.awt.event.ActionEvent; - -/** - * @author Andrew Lalis - */ -public class PlayAction extends MusicAction { - - private String[] args = null; - - public PlayAction(IGuild guild) { - super(guild); - } - - public PlayAction(IGuild guild, String[] args){ - super(guild); - this.args = args; - } - - @Override - public void actionPerformed(ActionEvent e) { - System.out.println("Play action."); - } -} diff --git a/src/main/java/handiebot/view/actions/music/QueueListAction.java b/src/main/java/handiebot/view/actions/music/QueueListAction.java deleted file mode 100644 index 7e53e87..0000000 --- a/src/main/java/handiebot/view/actions/music/QueueListAction.java +++ /dev/null @@ -1,24 +0,0 @@ -package handiebot.view.actions.music; - -import handiebot.HandieBot; -import sx.blah.discord.handle.obj.IGuild; - -import java.awt.event.ActionEvent; - -/** - * @author Andrew Lalis - */ -public class QueueListAction extends MusicAction { - - private boolean showAll = false; - - public QueueListAction(IGuild guild, boolean showAll){ - super(guild); - this.showAll = showAll; - } - - @Override - public void actionPerformed(ActionEvent e) { - HandieBot.musicPlayer.showQueueList(this.guild, this.showAll); - } -} diff --git a/src/main/java/handiebot/view/actions/music/SkipAction.java b/src/main/java/handiebot/view/actions/music/SkipAction.java deleted file mode 100644 index f7b3b57..0000000 --- a/src/main/java/handiebot/view/actions/music/SkipAction.java +++ /dev/null @@ -1,21 +0,0 @@ -package handiebot.view.actions.music; - -import handiebot.HandieBot; -import sx.blah.discord.handle.obj.IGuild; - -import java.awt.event.ActionEvent; - -/** - * @author Andrew Lalis - */ -public class SkipAction extends MusicAction { - - public SkipAction(IGuild guild) { - super(guild); - } - - @Override - public void actionPerformed(ActionEvent e) { - HandieBot.musicPlayer.skipTrack(this.guild); - } -} diff --git a/src/main/java/handiebot/view/actions/music/ToggleRepeatAction.java b/src/main/java/handiebot/view/actions/music/ToggleRepeatAction.java deleted file mode 100644 index 64ded13..0000000 --- a/src/main/java/handiebot/view/actions/music/ToggleRepeatAction.java +++ /dev/null @@ -1,21 +0,0 @@ -package handiebot.view.actions.music; - -import handiebot.HandieBot; -import sx.blah.discord.handle.obj.IGuild; - -import java.awt.event.ActionEvent; - -/** - * @author Andrew Lalis - */ -public class ToggleRepeatAction extends MusicAction { - - public ToggleRepeatAction(IGuild guild) { - super(guild); - } - - @Override - public void actionPerformed(ActionEvent e) { - HandieBot.musicPlayer.toggleRepeat(this.guild); - } -} diff --git a/src/main/java/handiebot/view/package-info.java b/src/main/java/handiebot/view/package-info.java new file mode 100644 index 0000000..4e87659 --- /dev/null +++ b/src/main/java/handiebot/view/package-info.java @@ -0,0 +1,5 @@ +/** + * @author Andrew Lalis + * Contains all the code for the user interface (console). + */ +package handiebot.view; \ No newline at end of file -- 2.34.1