package handiebot.command; import com.sun.istack.internal.NotNull; import handiebot.utils.DisappearingMessage; import handiebot.view.BotLog; import handiebot.view.actions.QuitAction; import handiebot.view.actions.music.PlayAction; import handiebot.view.actions.music.QueueListAction; import handiebot.view.actions.music.SkipAction; import handiebot.view.actions.music.ToggleRepeatAction; import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent; import sx.blah.discord.handle.obj.*; import sx.blah.discord.util.EmbedBuilder; import java.awt.*; import static handiebot.HandieBot.log; /** * @author Andrew Lalis * Class to process commands. */ public class CommandHandler { public static String PREFIX = "!"; /** * Main method to handle user messages. * @param event The event generated by the message. */ public static void handleCommand(MessageReceivedEvent event){ IMessage message = event.getMessage(); IUser user = event.getAuthor(); IChannel channel = event.getChannel(); IGuild guild = event.getGuild(); String command = extractCommand(message); String[] args = extractArgs(message); if (guild != null && command != null){ DisappearingMessage.deleteMessageAfter(1000, message); if (command.equals("play")){ //Play or queue a song. new PlayAction(guild, args).actionPerformed(null); } else if (command.equals("skip") && args.length == 0){ //Skip the current song. new SkipAction(guild).actionPerformed(null); } else if (command.equals("help")){ //Send a PM to the user with help info. sendHelpInfo(user);//TODO finish the help command and fill in with new descriptions each time. } else if (command.equals("queue")){ //Display the first few items of the queue. new QueueListAction(guild, (args.length == 1) && args[0].equals("all")).actionPerformed(null); } else if (command.equals("repeat")){ //Toggle repeat. new ToggleRepeatAction(guild).actionPerformed(null); } else if (command.equals("clear")){ //TODO clear command. } else if (command.equals("quit")){ //Quit the application. new QuitAction(guild).actionPerformed(null); } else if (command.equals("playlist")){ //Do playlist actions. //TODO perform actions! } else if (command.equals("prefix") && args.length == 1){ //Set the prefix to the first argument. if (args[0].length() != 1){ new DisappearingMessage(channel, "You may only set the prefix to 1 character. To do otherwise is simply foolish.", 3000); } else { new DisappearingMessage(channel, "Command prefix set to "+PREFIX, 10000); log.log(BotLog.TYPE.INFO, guild, "Prefix set to "+PREFIX); setPrefix(args[0]); } } else { log.log(BotLog.TYPE.ERROR, guild, "Invalid command: "+command+" issued by "+user.getName()); } } } /** * Returns a command word, if one exists, from a given message. * @param message The message to get a command from. * @return The command word, minus the prefix, or null. */ private static String extractCommand(IMessage message){ String[] words = message.getContent().split(" "); if (words[0].startsWith(PREFIX)){ return words[0].replaceFirst(PREFIX, "").toLowerCase(); } return null; } /** * Extracts a list of arguments from a message, assuming a command exists. * @param message The message to parse. * @return A list of strings representing args. */ @NotNull private static String[] extractArgs(IMessage message){ String[] words = message.getContent().split(" "); if (words[0].startsWith(PREFIX)){ String[] args = new String[words.length-1]; for (int i = 0; i < words.length-1; i++){ args[i] = words[i+1]; } return args; } return new String[0]; } /** * Method to send a useful list of commands to any user if they desire. * @param user The user to send the message to. */ private static void sendHelpInfo(IUser user){ IPrivateChannel pm = user.getOrCreatePMChannel(); EmbedBuilder builder = new EmbedBuilder(); builder.withAuthorName("HandieBot"); builder.withAuthorUrl("https://github.com/andrewlalis/HandieBot"); builder.withAuthorIcon("https://github.com/andrewlalis/HandieBot/blob/master/src/main/resources/icon.png"); 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); pm.sendMessage(builder.build()); } /** * Sets the prefix used to identify commands. * @param prefix The prefix appended to the beginning of commands. */ public static void setPrefix(String prefix){ PREFIX = prefix; } }