diff --git a/README.md b/README.md index 480d668..c671fcc 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,59 @@ # HandieBot +![AvatarIcon](/src/main/resources/avatarIcon.png) + HandieBot is a bot for Discord, written using Java, the [Discord4J](https://github.com/austinv11/Discord4J) library, and the [Lavaplayer](https://github.com/sedmelluq/lavaplayer) library for sound processing. It is a fully-fledged bot with the ability to manage playlists, get music from URLs, and perform other necessary functions for a clean and enjoyable user experience. +## Description + +This Bot is designed to run as one executable Jar file, to represent one Discord bot. The bot itself keeps track of which servers (`guilds`) that it's connected to, and can independently handle requests from each one, provided it has enough bandwidth. + +In each guild, the bot will use, or create both a voice and a text channel for it to use. These values are set in the source code as `HandieBotMusic` and `handiebotmusic`, respectively. From these channels, the bot will send messages about what song it's currently playing, responses to player requests, and any possible errors that occur. The voice channel is specifically only for playing music, and the bot will try to only connect when it is doing so. + ## Commands -### `play ` +HandieBot contains some commands, most of which should be quite intuitive to the user. However, for completions' sake, the format for commands is as follows: -Issuing the `play` command attempts to load a song from a given URL, and append it to the active queue. The bot will tell the user quite obviously if their link does not work, or if there was an internal error. If there are already some songs in the queue, then this will also, if successful, tell the user approximately how long it will be until their song is played. +All commands begin with a prefix, which will not be shown with all the following commands, as it can be configured by users. This prefix is by default `!`. + +`command [optional arguments] ` + +In particular, if the optional argument is shown as capital letters, this means that you must give a value, but if the optional argument is given in lowercase letters, simply write this argument. For example, the following commands are valid: + +```text +play +play https://www.youtube.com/watch?v=9bZkp7q19f0 +queue +queue all +``` + +Because the play command is defined as `play [URL]`, and the queue command is defined as `queue [all]`. +### 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. + +* `skip` - If a song is playing, the bot will skip it and play the next song in the queue. + +* `queue [all]` - Lists up to the first 10 items on the queue, if no argument is given. If you add `all`, the bot will upload a list to [PasteBin](http://pastebin.com) of the entire queue, and give you + +* `repeat [true|false]` - Sets the bot to repeat the playlist, as in once a song is removed from the queue to be played, it is added back to the end of the playlist. + +* `shuffle [true|false]` - Sets the bot to shuffle the playlist, as in pull a random song from the playlist, with some filters to prevent repeating songs. + +* `playlist ` - Various commands to manipulate playlists. The specific sub-commands are explained below. + * `create [URL]...` - Creates a new playlist, optionally with some starting URLs. + + * `delete ` - Deletes a playlist with the given name. + + * `show [NAME]` - If a name is given, shows the songs in a given playlist; otherwise it lists the names of the playlists. + + * `play ` - Loads and begins playing the specified playlist. + + * `add [URL]...` - Adds the specified URL, or multiple URLs to the playlist given by `NAME`. + + * `remove ` - Removes the specified song name, or the one that most closely matches the song name given, from the playlist given by `NAME`. + + * `rename ` - Renames the playlist to the new name. + diff --git a/pom.xml b/pom.xml index 21b6797..dea48d3 100644 --- a/pom.xml +++ b/pom.xml @@ -4,9 +4,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - net.agspace.handiebot - handiebot - 1.0-SNAPSHOT + com.github.andrewlalis + HandieBot + 1.1.0 @@ -21,10 +21,6 @@ jar - - 1.1.0 - - jcenter @@ -47,11 +43,6 @@ lavaplayer 1.2.39 - - com.github.kennedyoliveira - pastebin4j - ${pastebin4j.version} - \ No newline at end of file diff --git a/src/main/java/handiebot/HandieBot.java b/src/main/java/handiebot/HandieBot.java index e3b05f8..7e4ca6e 100644 --- a/src/main/java/handiebot/HandieBot.java +++ b/src/main/java/handiebot/HandieBot.java @@ -16,13 +16,15 @@ import sx.blah.discord.util.RateLimitException; /** * @author Andrew Lalis * Main Class for the discord bot. Contains client loading information and general event processing. + * Most variables are static here because this is the main file for the Bot across many possible guilds it could + * be runnnig on, so it is no problem to have only one copy. */ public class HandieBot { public static final String APPLICATION_NAME = "HandieBot"; private static final String TOKEN = "MjgzNjUyOTg5MjEyNjg4Mzg0.C45A_Q.506b0G6my1FEFa7_YY39lxLBHUY"; - private static IDiscordClient client; + public static IDiscordClient client; public static View view; private static BotWindow window; public static BotLog log; @@ -38,6 +40,7 @@ public class HandieBot { @EventSubscriber public void onReady(ReadyEvent event){ log.log(BotLog.TYPE.INFO, "HandieBot initialized."); + //client.changeAvatar(Image.forStream("png", getClass().getClassLoader().getResourceAsStream("avatarIcon.png"))); } public static void main(String[] args) throws DiscordException, RateLimitException { diff --git a/src/main/java/handiebot/command/CommandContext.java b/src/main/java/handiebot/command/CommandContext.java new file mode 100644 index 0000000..03fbaec --- /dev/null +++ b/src/main/java/handiebot/command/CommandContext.java @@ -0,0 +1,41 @@ +package handiebot.command; + +import sx.blah.discord.handle.obj.IChannel; +import sx.blah.discord.handle.obj.IGuild; +import sx.blah.discord.handle.obj.IUser; + +/** + * @author Andrew Lalis + * Class to hold important data for a command, such as user, channel, and guild. + */ +public class CommandContext { + + private IUser user; + private IChannel channel; + private IGuild guild; + private String[] args; + + public CommandContext(IUser user, IChannel channel, IGuild guild, String[] args){ + this.user = user; + this.channel = channel; + this.guild = guild; + this.args = args; + } + + public IUser getUser(){ + return this.user; + } + + public IChannel getChannel(){ + return this.channel; + } + + public IGuild getGuild(){ + return this.guild; + } + + public String[] getArgs(){ + return this.args; + } + +} diff --git a/src/main/java/handiebot/command/CommandHandler.java b/src/main/java/handiebot/command/CommandHandler.java index 642682a..4ea8ee2 100644 --- a/src/main/java/handiebot/command/CommandHandler.java +++ b/src/main/java/handiebot/command/CommandHandler.java @@ -1,6 +1,7 @@ package handiebot.command; import com.sun.istack.internal.NotNull; +import handiebot.command.commands.music.PlaylistCommand; import handiebot.utils.DisappearingMessage; import handiebot.view.BotLog; import handiebot.view.actions.QuitAction; @@ -34,6 +35,7 @@ public class CommandHandler { IGuild guild = event.getGuild(); String command = extractCommand(message); String[] args = extractArgs(message); + CommandContext context = new CommandContext(user, channel, guild, args); if (guild != null && command != null){ DisappearingMessage.deleteMessageAfter(1000, message); if (command.equals("play")){ @@ -58,7 +60,7 @@ public class CommandHandler { new QuitAction(guild).actionPerformed(null); } else if (command.equals("playlist")){ //Do playlist actions. - //TODO perform actions! + new PlaylistCommand().execute(context); } else if (command.equals("prefix") && args.length == 1){ //Set the prefix to the first argument. if (args[0].length() != 1){ diff --git a/src/main/java/handiebot/command/Commands.java b/src/main/java/handiebot/command/Commands.java new file mode 100644 index 0000000..67a1aba --- /dev/null +++ b/src/main/java/handiebot/command/Commands.java @@ -0,0 +1,27 @@ +package handiebot.command; + +import handiebot.command.commands.music.*; +import handiebot.command.types.Command; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Andrew Lalis + * Class to hold a list of commands, as static definitions that can be called upon by {@code CommandHandler}. + */ +public class Commands { + + public static List commands = new ArrayList(); + + static { + //Music commands. + commands.add(new PlayCommand()); + commands.add(new QueueCommand()); + commands.add(new SkipCommand()); + commands.add(new RepeatCommand()); + commands.add(new ShuffleCommand()); + commands.add(new PlaylistCommand()); + } + +} diff --git a/src/main/java/handiebot/command/commands/HelpCommand.java b/src/main/java/handiebot/command/commands/HelpCommand.java new file mode 100644 index 0000000..6142cf2 --- /dev/null +++ b/src/main/java/handiebot/command/commands/HelpCommand.java @@ -0,0 +1,35 @@ +package handiebot.command.commands; + +import handiebot.command.CommandContext; +import handiebot.command.types.ContextCommand; +import sx.blah.discord.handle.obj.IPrivateChannel; +import sx.blah.discord.util.EmbedBuilder; + +import java.awt.*; + +/** + * @author Andrew Lalis + * Class for sending help/command info to a user if they so desire it. + */ +public class HelpCommand extends ContextCommand { + + public HelpCommand() { + super("help"); + } + + @Override + public void execute(CommandContext context) { + IPrivateChannel pm = context.getUser().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()); + } +} diff --git a/src/main/java/handiebot/command/commands/music/PlayCommand.java b/src/main/java/handiebot/command/commands/music/PlayCommand.java new file mode 100644 index 0000000..f782d72 --- /dev/null +++ b/src/main/java/handiebot/command/commands/music/PlayCommand.java @@ -0,0 +1,26 @@ +package handiebot.command.commands.music; + +import handiebot.HandieBot; +import handiebot.command.CommandContext; +import handiebot.command.types.ContextCommand; + +/** + * @author Andrew Lalis + * Command to play a song from the queue or load a new song. + */ +public class PlayCommand extends ContextCommand { + + public PlayCommand() { + super("play"); + } + + @Override + public void execute(CommandContext context) { + if (context.getArgs() == null || context.getArgs().length == 0){ + HandieBot.musicPlayer.playQueue(context.getGuild()); + } else { + HandieBot.musicPlayer.loadToQueue(context.getGuild(), context.getArgs()[0]); + } + } + +} diff --git a/src/main/java/handiebot/command/commands/music/PlaylistCommand.java b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java new file mode 100644 index 0000000..0924e02 --- /dev/null +++ b/src/main/java/handiebot/command/commands/music/PlaylistCommand.java @@ -0,0 +1,126 @@ +package handiebot.command.commands.music; + +import handiebot.command.CommandContext; +import handiebot.command.CommandHandler; +import handiebot.command.types.ContextCommand; +import handiebot.lavaplayer.Playlist; +import handiebot.utils.DisappearingMessage; +import handiebot.view.BotLog; +import sx.blah.discord.handle.obj.IChannel; + +import java.io.File; +import java.util.List; + +import static handiebot.HandieBot.log; + +/** + * @author Andrew Lalis + * Command to manipulate playlists. + */ +public class PlaylistCommand extends ContextCommand { + + public PlaylistCommand(){ + super("playlist"); + } + + @Override + public void execute(CommandContext context) { + String[] args = context.getArgs(); + if (args.length > 0){ + switch (args[0]){ + case ("create"): + create(context); + break; + case ("delete"): + delete(context); + break; + case ("show"): + show(context); + break; + case ("add"): + + break; + case ("remove"): + + break; + case ("rename"): + + break; + default: + incorrectMainArg(context.getChannel()); + break; + } + } else { + incorrectMainArg(context.getChannel()); + } + } + + /** + * Error message to show if the main argument is incorrect. + * @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); + } + + /** + * Creates a new playlist. + * @param context The important data such as user and arguments to be passed. + */ + private void create(CommandContext context){ + if (context.getArgs().length >= 2) { + Playlist playlist = new Playlist(context.getArgs()[1], context.getUser().getLongID()); + playlist.save(); + for (int i = 2; i < context.getArgs().length; i++){ + String url = context.getArgs()[i]; + playlist.loadTrack(url); + playlist.save(); + } + log.log(BotLog.TYPE.INFO, "Created playlist: "+playlist.getName()+" with "+playlist.getTracks().size()+" new tracks."); + new DisappearingMessage(context.getChannel(), "Your playlist *"+playlist.getName()+"* has been created.\nType `"+ CommandHandler.PREFIX+"playlist play "+playlist.getName()+"` to play it.", 5000); + } else { + new DisappearingMessage(context.getChannel(), "You must specify a name for the new playlist.", 3000); + } + } + + /** + * Attempts to delete a playlist. + * @param context The context of the command. + */ + 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){ + 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); + } + } else { + new DisappearingMessage(context.getChannel(), "The name you entered is not a playlist.\nType `"+CommandHandler.PREFIX+"playlist show` to list the playlists available.", 5000); + } + } else { + new DisappearingMessage(context.getChannel(), "You must specify the name of a playlist to delete.", 3000); + } + } + + /** + * Displays the list of playlists, or a specific playlist's songs. + * @param context The data to be passed, containing channel and arguments. + */ + private void show(CommandContext context){ + if (context.getArgs().length > 1){ + + } else { + List playlists = Playlist.getAvailablePlaylists(); + StringBuilder sb = new StringBuilder("**Playlists:**\n"); + for (String playlist : playlists) { + sb.append(playlist).append('\n'); + } + context.getChannel().sendMessage(sb.toString()); + } + } + +} diff --git a/src/main/java/handiebot/command/commands/music/QueueCommand.java b/src/main/java/handiebot/command/commands/music/QueueCommand.java new file mode 100644 index 0000000..85b8919 --- /dev/null +++ b/src/main/java/handiebot/command/commands/music/QueueCommand.java @@ -0,0 +1,21 @@ +package handiebot.command.commands.music; + +import handiebot.HandieBot; +import handiebot.command.CommandContext; +import handiebot.command.types.ContextCommand; + +/** + * @author Andrew Lalis + * Queue command to display the active queue. + */ +public class QueueCommand extends ContextCommand { + public QueueCommand() { + super("queue"); + } + + @Override + public void execute(CommandContext context) { + HandieBot.musicPlayer.showQueueList(context.getGuild(), (context.getArgs() != null && context.getArgs()[0].equals("all"))); + } + +} diff --git a/src/main/java/handiebot/command/commands/music/RepeatCommand.java b/src/main/java/handiebot/command/commands/music/RepeatCommand.java new file mode 100644 index 0000000..f80cf05 --- /dev/null +++ b/src/main/java/handiebot/command/commands/music/RepeatCommand.java @@ -0,0 +1,26 @@ +package handiebot.command.commands.music; + +import handiebot.HandieBot; +import handiebot.command.CommandContext; +import handiebot.command.types.ContextCommand; + +/** + * @author Andrew Lalis + * Command to toggle repeating of the active playlist. + */ +public class RepeatCommand extends ContextCommand { + + public RepeatCommand(){ + super("repeat"); + } + + @Override + public void execute(CommandContext context) { + if (context.getArgs().length == 1){ + boolean shouldRepeat = Boolean.getBoolean(context.getArgs()[0].toLowerCase()); + HandieBot.musicPlayer.setRepeat(context.getGuild(), shouldRepeat); + } else { + HandieBot.musicPlayer.toggleRepeat(context.getGuild()); + } + } +} diff --git a/src/main/java/handiebot/command/commands/music/ShuffleCommand.java b/src/main/java/handiebot/command/commands/music/ShuffleCommand.java new file mode 100644 index 0000000..fe58fa3 --- /dev/null +++ b/src/main/java/handiebot/command/commands/music/ShuffleCommand.java @@ -0,0 +1,26 @@ +package handiebot.command.commands.music; + +import handiebot.HandieBot; +import handiebot.command.CommandContext; +import handiebot.command.types.ContextCommand; + +/** + * @author Andrew Lalis + * Command to set shuffling of the active playlist. + */ +public class ShuffleCommand extends ContextCommand { + + public ShuffleCommand(){ + super("shuffle"); + } + + @Override + public void execute(CommandContext context) { + if (context.getArgs().length == 1){ + boolean shouldShuffle = Boolean.getBoolean(context.getArgs()[0].toLowerCase()); + HandieBot.musicPlayer.setShuffle(context.getGuild(), shouldShuffle); + } else { + HandieBot.musicPlayer.toggleShuffle(context.getGuild()); + } + } +} diff --git a/src/main/java/handiebot/command/commands/music/SkipCommand.java b/src/main/java/handiebot/command/commands/music/SkipCommand.java new file mode 100644 index 0000000..69ed235 --- /dev/null +++ b/src/main/java/handiebot/command/commands/music/SkipCommand.java @@ -0,0 +1,22 @@ +package handiebot.command.commands.music; + +import handiebot.HandieBot; +import handiebot.command.CommandContext; +import handiebot.command.types.ContextCommand; + +/** + * @author Andrew Lalis + * Skips the current song, if there is one playing. + */ +public class SkipCommand extends ContextCommand { + + public SkipCommand() { + super("skip"); + } + + @Override + public void execute(CommandContext context) { + HandieBot.musicPlayer.skipTrack(context.getGuild()); + } + +} diff --git a/src/main/java/handiebot/command/types/Command.java b/src/main/java/handiebot/command/types/Command.java new file mode 100644 index 0000000..7f6ca26 --- /dev/null +++ b/src/main/java/handiebot/command/types/Command.java @@ -0,0 +1,19 @@ +package handiebot.command.types; + +/** + * @author Andrew Lalis + * Basic type of command. + */ +public abstract class Command { + + private String name; + + public Command(String name){ + this.name = name; + } + + public String getName(){ + return this.name; + }; + +} diff --git a/src/main/java/handiebot/command/types/ContextCommand.java b/src/main/java/handiebot/command/types/ContextCommand.java new file mode 100644 index 0000000..6ac4e91 --- /dev/null +++ b/src/main/java/handiebot/command/types/ContextCommand.java @@ -0,0 +1,17 @@ +package handiebot.command.types; + +import handiebot.command.CommandContext; + +/** + * @author Andrew Lalis + * Type of command which requires a guild to function properly. + */ +public abstract class ContextCommand extends Command { + + public ContextCommand(String s) { + super(s); + } + + 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 new file mode 100644 index 0000000..60274dc --- /dev/null +++ b/src/main/java/handiebot/command/types/StaticCommand.java @@ -0,0 +1,15 @@ +package handiebot.command.types; + +/** + * @author Andrew Lalis + * Class for commands which require no context, so execute on a global scale. + */ +public abstract class StaticCommand extends Command { + + public StaticCommand(String s) { + super(s); + } + + public abstract void execute(); + +} diff --git a/src/main/java/handiebot/lavaplayer/GuildMusicManager.java b/src/main/java/handiebot/lavaplayer/GuildMusicManager.java index bd5320f..54f8268 100644 --- a/src/main/java/handiebot/lavaplayer/GuildMusicManager.java +++ b/src/main/java/handiebot/lavaplayer/GuildMusicManager.java @@ -16,7 +16,7 @@ public class GuildMusicManager { public GuildMusicManager(AudioPlayerManager manager, IGuild guild){ this.player = manager.createPlayer(); - this.scheduler = new TrackScheduler(this.player, guild, manager); + this.scheduler = new TrackScheduler(this.player, guild); this.player.addListener(this.scheduler); } diff --git a/src/main/java/handiebot/lavaplayer/MusicPlayer.java b/src/main/java/handiebot/lavaplayer/MusicPlayer.java index 79296f6..f38abb2 100644 --- a/src/main/java/handiebot/lavaplayer/MusicPlayer.java +++ b/src/main/java/handiebot/lavaplayer/MusicPlayer.java @@ -10,12 +10,26 @@ import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import handiebot.command.CommandHandler; import handiebot.utils.DisappearingMessage; import handiebot.view.BotLog; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; 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; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -55,6 +69,10 @@ public class MusicPlayer { this.voiceChannels = new HashMap<>(); } + public AudioPlayerManager getPlayerManager(){ + return this.playerManager; + } + /** * Gets the music manager specific to a particular guild. * @param guild The guild to get the music manager for. @@ -113,14 +131,40 @@ public class MusicPlayer { */ public void toggleRepeat(IGuild guild){ GuildMusicManager musicManager = this.getMusicManager(guild); - musicManager.scheduler.setRepeat(!musicManager.scheduler.isRepeating()); } + /** + * Sets the repeating of songs for a particular guild. + * @param guild The guild to set repeat for. + * @param value True to repeat, false otherwise. + */ + public void setRepeat(IGuild guild, boolean value){ + getMusicManager(guild).scheduler.setRepeat(value); + } + + /** + * Toggles shuffling for a specific guild. + * @param guild The guild to toggle shuffling for. + */ + public void toggleShuffle(IGuild guild){ + GuildMusicManager musicManager = this.getMusicManager(guild); + musicManager.scheduler.setShuffle(!musicManager.scheduler.isShuffling()); + } + + /** + * Sets shuffling for a specific guild. + * @param guild The guild to set shuffling for. + * @param value The value to set. True for shuffling, false for linear play. + */ + public void setShuffle(IGuild guild, boolean value){ + getMusicManager(guild).scheduler.setShuffle(value); + } + /** * Sends a formatted message to the guild about the first few items in a queue. */ - public void showQueueList(IGuild guild, boolean showAll){ + 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.PREFIX+"play** *URL* to add songs.", 3000); @@ -157,18 +201,60 @@ public class MusicPlayer { String time = String.format(" [%d:%02d]\n", minutes, seconds); sb.append(time); } - //TODO: get pastebin working. - /* - PasteBin pasteBin = new PasteBin(new AccountCredentials(PASTEBIN_KEY)); - Paste paste = new Paste(PASTEBIN_KEY); - paste.setTitle("Music Queue for Discord Server: "+guild.getName()); - paste.setContent(sb.toString()); - paste.setExpiration(PasteExpiration.ONE_HOUR); - paste.setVisibility(PasteVisibility.PUBLIC); - final String pasteURL = pasteBin.createPaste(paste); - log.log(BotLog.TYPE.INFO, guild, "Uploaded full queue to "+pasteURL); - new DisappearingMessage(getChatChannel(guild), "You may view the full queue here. "+pasteURL, 60000); - */ + + HttpClient httpclient = HttpClients.createDefault(); + HttpPost httppost = new HttpPost("https://www.pastebin.com/api/api_post.php"); + + // Request parameters and other properties. + List params = new ArrayList(2); + params.add(new BasicNameValuePair("api_dev_key", PASTEBIN_KEY)); + params.add(new BasicNameValuePair("api_option", "paste")); + params.add(new BasicNameValuePair("api_paste_code", sb.toString())); + params.add(new BasicNameValuePair("api_paste_private", "0")); + params.add(new BasicNameValuePair("api_paste_name", "Music Queue for Discord Server: "+guild.getName())); + params.add(new BasicNameValuePair("api_paste_expire_date", "10M")); + //params.add(new BasicNameValuePair("api_paste_format", "text")); + params.add(new BasicNameValuePair("api_user_key", "")); + + try { + httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + //Execute and get the response. + HttpResponse response = null; + try { + response = httpclient.execute(httppost); + } catch (IOException e) { + e.printStackTrace(); + } + HttpEntity entity = response.getEntity(); + + if (entity != null) { + InputStream instream = null; + try { + instream = entity.getContent(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + StringWriter writer = new StringWriter(); + IOUtils.copy(instream, writer, "UTF-8"); + String pasteURL = writer.toString(); + log.log(BotLog.TYPE.INFO, guild, "Uploaded full queue to "+pasteURL); + new DisappearingMessage(getChatChannel(guild), "You may view the full queue here. "+pasteURL, 60000); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + instream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } } } diff --git a/src/main/java/handiebot/lavaplayer/Playlist.java b/src/main/java/handiebot/lavaplayer/Playlist.java index 3d2a8f8..070e287 100644 --- a/src/main/java/handiebot/lavaplayer/Playlist.java +++ b/src/main/java/handiebot/lavaplayer/Playlist.java @@ -1,16 +1,17 @@ package handiebot.lavaplayer; import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler; -import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; +import handiebot.HandieBot; import handiebot.view.BotLog; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.concurrent.ExecutionException; @@ -21,6 +22,8 @@ import static handiebot.HandieBot.log; * @author Andrew Lalis * A Playlist is a list of AudioTracks which a track scheduler can pull from to create a queue filled with songs. The * playlist is persistent, i.e. it is saved into a file. + * Be careful, though, as the playlist is not saved in this class, but must be saved manually by whoever is operating + * on the playlist. */ public class Playlist { @@ -44,9 +47,9 @@ public class Playlist { * Creates a playlist from a file with the given name. * @param name The name of the file. */ - public Playlist(String name, AudioPlayerManager playerManager){ + public Playlist(String name){ this.name = name; - this.load(playerManager); + this.load(); } public String getName(){ @@ -69,6 +72,44 @@ public class Playlist { this.tracks.add(track); } + /** + * Attempts to load a track or playlist from a URL, and add it to the tracks list. + * @param url The URL to get the song/playlist from. + */ + public void loadTrack(String url){ + try { + HandieBot.musicPlayer.getPlayerManager().loadItem(url, new AudioLoadResultHandler() { + @Override + public void trackLoaded(AudioTrack audioTrack) { + tracks.add(audioTrack); + } + + @Override + public void playlistLoaded(AudioPlaylist audioPlaylist) { + tracks.addAll(audioPlaylist.getTracks()); + } + + @Override + public void noMatches() { + log.log(BotLog.TYPE.ERROR, "No matches found for: "+url+"."); + //Do nothing. This should not happen. + } + + @Override + public void loadFailed(FriendlyException e) { + log.log(BotLog.TYPE.ERROR, "Unable to load song from URL: "+url+". "+e.getMessage()); + //Do nothing. This should not happen. + } + }).get(); + } catch (InterruptedException e) { + log.log(BotLog.TYPE.ERROR, "Loading of playlist ["+this.name+"] interrupted. "+e.getMessage()); + e.printStackTrace(); + } catch (ExecutionException e) { + log.log(BotLog.TYPE.ERROR, "Execution exception while loading playlist ["+this.name+"]. "+e.getMessage()); + e.printStackTrace(); + } + } + /** * Removes a track from the playlist. * @param track The track to remove. @@ -159,7 +200,7 @@ public class Playlist { /** * Loads the playlist from a file with the playlist's name. */ - public void load(AudioPlayerManager playerManager){ + public void load(){ String path = System.getProperty("user.home")+"/.handiebot/playlist/"+this.name.replace(" ", "_")+".txt"; log.log(BotLog.TYPE.INFO, "Loading playlist from: "+path); File playlistFile = new File(path); @@ -172,41 +213,41 @@ public class Playlist { this.tracks = new ArrayList<>(trackCount); for (int i = 0; i < trackCount; i++){ String url = lines.remove(0); - playerManager.loadItem(url, new AudioLoadResultHandler() { - @Override - public void trackLoaded(AudioTrack audioTrack) { - tracks.add(audioTrack); - } - - @Override - public void playlistLoaded(AudioPlaylist audioPlaylist) { - //Do nothing. This should not happen. - } - - @Override - public void noMatches() { - System.out.println("No matches for: "+url); - //Do nothing. This should not happen. - } - - @Override - public void loadFailed(FriendlyException e) { - System.out.println("Load failed: "+e.getMessage()); - //Do nothing. This should not happen. - } - }).get(); + loadTrack(url); } } catch (IOException e) { log.log(BotLog.TYPE.ERROR, "IOException while loading playlist ["+this.name+"]. "+e.getMessage()); e.printStackTrace(); - } catch (InterruptedException e) { - log.log(BotLog.TYPE.ERROR, "Loading of playlist ["+this.name+"] interrupted. "+e.getMessage()); - e.printStackTrace(); - } catch (ExecutionException e) { - log.log(BotLog.TYPE.ERROR, "Execution exception while loading playlist ["+this.name+"]. "+e.getMessage()); - e.printStackTrace(); } + } else { + log.log(BotLog.TYPE.ERROR, "The playlist ["+this.name+"] does not exist."); } } + /** + * Returns a list of all playlists, or essentially all playlist files. + * @return A list of all playlists. + */ + public static List getAvailablePlaylists(){ + File playlistFolder = new File(System.getProperty("user.home")+"/.handiebot/playlist"); + List names = new ArrayList(Arrays.asList(playlistFolder.list())); + names.forEach(name -> name = name.replace("_", " ")); + return names; + } + + /** + * Returns true if a playlist exists. + * @param name The name of the playlist. + * @return True if the playlist exists. + */ + public static boolean playlistExists(String name){ + List names = getAvailablePlaylists(); + for (String n : names){ + if (n.equals(name)){ + return true; + } + } + return false; + } + } diff --git a/src/main/java/handiebot/lavaplayer/TrackScheduler.java b/src/main/java/handiebot/lavaplayer/TrackScheduler.java index 30d6d6b..284290f 100644 --- a/src/main/java/handiebot/lavaplayer/TrackScheduler.java +++ b/src/main/java/handiebot/lavaplayer/TrackScheduler.java @@ -1,7 +1,6 @@ package handiebot.lavaplayer; import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; -import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter; import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; @@ -36,11 +35,11 @@ public class TrackScheduler extends AudioEventAdapter { * Constructs a new track scheduler with the given player. * @param player The audio player this scheduler uses. */ - public TrackScheduler(AudioPlayer player, IGuild guild, AudioPlayerManager playerManager){ + public TrackScheduler(AudioPlayer player, IGuild guild){ this.player = player; this.guild = guild; //this.activePlaylist = new Playlist("HandieBot Active Playlist", 283652989212688384L); - this.activePlaylist = new Playlist("HandieBot Active Playlist", playerManager); + this.activePlaylist = new Playlist("HandieBot Active Playlist"); } /** diff --git a/src/main/resources/avatarIcon.png b/src/main/resources/avatarIcon.png new file mode 100644 index 0000000..e18afd7 Binary files /dev/null and b/src/main/resources/avatarIcon.png differ diff --git a/src/main/resources/icon.png b/src/main/resources/icon.png deleted file mode 100644 index cb2d220..0000000 Binary files a/src/main/resources/icon.png and /dev/null differ diff --git a/src/main/resources/icon.svg b/src/main/resources/icon.svg deleted file mode 100644 index 5babeb4..0000000 --- a/src/main/resources/icon.svg +++ /dev/null @@ -1,108 +0,0 @@ - - - - - - - - - - - - - - image/svg+xml - - - - - - - - - - - - - - diff --git a/src/main/resources/iconTemplate.png b/src/main/resources/iconTemplate.png new file mode 100644 index 0000000..dac4cef Binary files /dev/null and b/src/main/resources/iconTemplate.png differ