Release 1.1 #2

Merged
andrewlalis merged 2 commits from development into master 2017-06-23 12:27:00 +00:00
29 changed files with 1157 additions and 440 deletions

View File

@ -1,11 +1,61 @@
# 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 <URL>`
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] <required 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 <create|show|play|delete|add|remove|rename>` - Various commands to manipulate playlists. The specific sub-commands are explained below.
* `create <PLAYLIST> [URL]...` - Creates a new playlist, optionally with some starting URLs.
* `delete <PLAYLIST>` - Deletes a playlist with the given name.
* `show [PLAYLIST]` - If a name is given, shows the songs in a given playlist; otherwise it lists the names of the playlists.
* `play <PLAYLIST>` - Loads and begins playing the specified playlist.
* `add <PLAYLIST> <URL> [URL]...` - Adds the specified URL, or multiple URLs to the playlist given by `PLAYLIST`.
* `remove <PLAYLIST> <SONGNUMBER>` - Removes the specified song name, or the one that most closely matches the song name given, from the playlist given by `PLAYLIST`.
* `rename <PLAYLIST> <NEWNAME>` - Renames the playlist to the new name.
* `move <PLAYLIST> <SONGNUMBER> <NEWNUMBER>` - Moves a song from one index to another index, shifting other elements as necessary.

15
pom.xml
View File

@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.agspace.handiebot</groupId>
<artifactId>handiebot</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>com.github.andrewlalis</groupId>
<artifactId>HandieBot</artifactId>
<version>1.1.0</version>
<build>
<plugins>
<plugin>
@ -21,10 +21,6 @@
</build>
<packaging>jar</packaging>
<properties>
<pastebin4j.version>1.1.0</pastebin4j.version>
</properties>
<repositories>
<repository>
<id>jcenter</id>
@ -47,11 +43,6 @@
<artifactId>lavaplayer</artifactId>
<version>1.2.39</version>
</dependency>
<dependency>
<groupId>com.github.kennedyoliveira</groupId>
<artifactId>pastebin4j</artifactId>
<version>${pastebin4j.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -1,13 +1,15 @@
package handiebot.command;
import com.sun.istack.internal.NotNull;
import handiebot.command.commands.music.PlayCommand;
import handiebot.command.commands.music.PlaylistCommand;
import handiebot.command.commands.music.RepeatCommand;
import handiebot.command.commands.music.ShuffleCommand;
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;
@ -34,11 +36,12 @@ 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")){
//Play or queue a song.
new PlayAction(guild, args).actionPerformed(null);
new PlayCommand().execute(context);
} else if (command.equals("skip") && args.length == 0){
//Skip the current song.
new SkipAction(guild).actionPerformed(null);
@ -48,9 +51,11 @@ public class CommandHandler {
} 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")){
} else if (command.equals("repeat")) {
//Toggle repeat.
new ToggleRepeatAction(guild).actionPerformed(null);
new RepeatCommand().execute(context);
} else if (command.equals("shuffle")){
new ShuffleCommand().execute(context);
} else if (command.equals("clear")){
//TODO clear command.
} else if (command.equals("quit")){
@ -58,7 +63,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){

View File

@ -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<Command> commands = new ArrayList<Command>();
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());
}
}

View File

@ -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());
}
}

View File

@ -0,0 +1,33 @@
package handiebot.command.commands.music;
import handiebot.HandieBot;
import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand;
import handiebot.lavaplayer.playlist.UnloadedTrack;
import handiebot.utils.DisappearingMessage;
/**
* @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 {
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);
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,286 @@
package handiebot.command.commands.music;
import handiebot.HandieBot;
import handiebot.command.CommandContext;
import handiebot.command.CommandHandler;
import handiebot.command.types.ContextCommand;
import handiebot.lavaplayer.playlist.Playlist;
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;
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"):
add(context);
break;
case ("play"):
play(context);
break;
case ("remove"):
remove(context);
break;
case ("rename"):
rename(context);
break;
case ("move"):
move(context);
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`<create|delete|show|play|add|remove|rename>`", 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]);
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.getTrackCount()+" 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){
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);
}
} 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){
if (Playlist.playlistExists(context.getArgs()[1])){
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
IMessage message = context.getChannel().sendMessage(playlist.toString());
DisappearingMessage.deleteMessageAfter(6000, message);
} else {
new DisappearingMessage(context.getChannel(), "The playlist you specified does not exist.\nUse `"+CommandHandler.PREFIX+"playlist show` to view available playlists.", 5000);
}
} else {
List<String> 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(6000, message);
}
}
/**
* Attempts to add a song or multiple songs to a playlist.
* @param context The command context.
*/
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);
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);
}
playlist.save();
IMessage message = 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.PREFIX+"playlist show` to view available playlists.", 5000);
} else {
new DisappearingMessage(context.getChannel(), "You must provide at least one URL to add.", 3000);
}
}
}
/**
* Shifts the named playlist to the active playlist and begins playback in accordance with the Music Player.
* @param context The command context.
*/
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);
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);
} else {
new DisappearingMessage(context.getChannel(), "You must provide a playlist to play.\nUse '"+CommandHandler.PREFIX+"playlist show` to view available playlists.", 3000);
}
}
/**
* Attempts to rename a playlist.
* @param context The command context.
*/
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);
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);
log.log(BotLog.TYPE.INFO, "Playlist "+context.getArgs()[1]+" renamed to "+context.getArgs()[2]+".");
} else {
new DisappearingMessage(context.getChannel(), "Unable to rename playlist.", 3000);
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);
}
}
/**
* Attempst to remove the song at a specified index of the playlist.
* @param context The command context.
*/
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);
return;
}
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
try{
int index = Integer.parseInt(context.getArgs()[2]);
UnloadedTrack track = playlist.getTracks().get(index);
playlist.removeTrack(track);
new DisappearingMessage(context.getChannel(), "Removed song: *"+track.getTitle()+"* from playlist **"+playlist.getName()+"**.", 6000);
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);
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);
}
}
/**
* Moves a song from one index to another.
* @param context The command context.
*/
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);
return;
}
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
int oldIndex = -1;
int newIndex = -1;
try {
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);
}
UnloadedTrack track = null;
if (oldIndex > -1 && oldIndex < playlist.getTrackCount()){
track = playlist.getTracks().remove(oldIndex);
if (newIndex > -1 && newIndex <= playlist.getTrackCount()){
playlist.getTracks().add(newIndex, track);
new DisappearingMessage(context.getChannel(), "Moved song *"+track.getTitle()+"* from position "+(oldIndex+1)+" to position "+(newIndex+1), 6000);
log.log(BotLog.TYPE.MUSIC, "Moved song "+track.getTitle()+" from position "+(oldIndex+1)+" to position "+(newIndex+1));
} else {
new DisappearingMessage(context.getChannel(), "The index of the song's new position is invalid. You entered "+newIndex, 5000);
}
} else {
new DisappearingMessage(context.getChannel(), "The index of the song is invalid. You entered "+oldIndex, 5000);
}
} else {
new DisappearingMessage(context.getChannel(), "You must provide a playlist name, followed by the song index, and a new index for that song.", 5000);
}
}
}

View File

@ -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")));
}
}

View File

@ -0,0 +1,32 @@
package handiebot.command.commands.music;
import handiebot.HandieBot;
import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import static handiebot.HandieBot.log;
/**
* @author Andrew Lalis
* 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());
}
log.log(BotLog.TYPE.MUSIC, context.getGuild(), "Set repeat to "+HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.isRepeating());
new DisappearingMessage(context.getChannel(), "Set repeat to "+HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.isRepeating(), 3000);
}
}

View File

@ -0,0 +1,32 @@
package handiebot.command.commands.music;
import handiebot.HandieBot;
import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import static handiebot.HandieBot.log;
/**
* @author Andrew Lalis
* 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());
}
log.log(BotLog.TYPE.MUSIC, context.getGuild(), "Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.isShuffling()));
new DisappearingMessage(context.getChannel(), "Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.isShuffling()), 3000);
}
}

View File

@ -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());
}
}

View File

@ -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;
};
}

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -1,14 +1,12 @@
package handiebot.lavaplayer;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import handiebot.command.CommandHandler;
import handiebot.lavaplayer.playlist.UnloadedTrack;
import handiebot.utils.DisappearingMessage;
import handiebot.utils.Pastebin;
import handiebot.view.BotLog;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IGuild;
@ -32,7 +30,6 @@ public class MusicPlayer {
//Name for the message and voice channels dedicated to this bot.
static String CHANNEL_NAME = "HandieBotMusic";
private static String PASTEBIN_KEY = "769adc01154922ece448cabd7a33b57c";
private final AudioPlayerManager playerManager;
@ -55,12 +52,16 @@ 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.
* @return The music manager for a guild.
*/
private GuildMusicManager getMusicManager(IGuild guild){
public GuildMusicManager getMusicManager(IGuild guild){
if (!this.musicManagers.containsKey(guild)){
log.log(BotLog.TYPE.MUSIC, guild, "Creating new music manager and audio provider for guild: "+guild.getName());
this.musicManagers.put(guild, new GuildMusicManager(this.playerManager, guild));
@ -113,107 +114,73 @@ 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){
List<AudioTrack> tracks = getMusicManager(guild).scheduler.queueList();
public void showQueueList(IGuild guild, boolean showAll) {
List<UnloadedTrack> 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);
} else {
if (!showAll) {
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);
} else {
log.log(BotLog.TYPE.ERROR, guild, "Unable to upload to pastebin: "+result);
}
} else {
EmbedBuilder builder = new EmbedBuilder();
builder.withColor(255, 0, 0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < (tracks.size() <= 10 ? tracks.size() : 10); i++) {
sb.append(i + 1);
sb.append(". ");
sb.append('[');
sb.append(tracks.get(i).getInfo().title);
sb.append("](");
sb.append(tracks.get(i).getInfo().uri);
sb.append(")");
int seconds = (int) (tracks.get(i).getInfo().length / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
String time = String.format(" [%d:%02d]\n", minutes, seconds);
sb.append(time);
sb.append(i + 1).append(". [").append(tracks.get(i).getTitle()).append("](");
sb.append(tracks.get(i).getURL()).append(")");
sb.append(tracks.get(i).getFormattedDuration()).append('\n');
}
builder.withTimestamp(System.currentTimeMillis());
builder.appendField("Showing " + (tracks.size() <= 10 ? tracks.size() : "the first 10") + " track" + (tracks.size() > 1 ? "s" : "") + ".", sb.toString(), false);
IMessage message = getChatChannel(guild).sendMessage(builder.build());
DisappearingMessage.deleteMessageAfter(6000, message);
} else {
StringBuilder sb = new StringBuilder("Queue for Discord Server: "+guild.getName()+"\n");
for (int i = 0; i < tracks.size(); i++){
sb.append(i+1).append(". ").append(tracks.get(i).getInfo().title);
int seconds = (int) (tracks.get(i).getInfo().length / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
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);
*/
}
}
}
/**
* Loads a URL to the queue, or outputs an error message if it fails.
* @param trackURL A string representing a youtube/soundcloud URL.
*/
public void loadToQueue(IGuild guild, String trackURL){
this.playerManager.loadItemOrdered(getMusicManager(guild), trackURL, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack audioTrack) {
addToQueue(guild, audioTrack);
}
@Override
public void playlistLoaded(AudioPlaylist audioPlaylist) {
if (audioPlaylist.getTracks().size() > 0){
AudioTrack firstTrack = audioPlaylist.getSelectedTrack();
if (firstTrack == null){
firstTrack = audioPlaylist.getTracks().get(0);
}
addToQueue(guild, firstTrack);
}
}
@Override
public void noMatches() {
log.log(BotLog.TYPE.ERROR, guild, "No matches found for: "+trackURL);
new DisappearingMessage(getChatChannel(guild), "Unable to find a result for: "+trackURL, 3000);
}
@Override
public void loadFailed(FriendlyException e) {
log.log(BotLog.TYPE.ERROR, guild, "Unable to load song: "+trackURL+". "+e.getMessage());
new DisappearingMessage(getChatChannel(guild), "Unable to load. "+e.getMessage(), 3000);
}
});
}
/**
* Adds a track to the queue and sends a message to the appropriate channel notifying users.
* @param track The track to queue.
*/
private void addToQueue(IGuild guild, AudioTrack track){
public void addToQueue(IGuild guild, UnloadedTrack track){
IVoiceChannel voiceChannel = getVoiceChannel(guild);
if (voiceChannel != null){
if (!voiceChannel.isConnected()) {
@ -224,7 +191,7 @@ public class MusicPlayer {
//Build message.
StringBuilder sb = new StringBuilder();
if (timeUntilPlay > 0) {
sb.append("Added **").append(track.getInfo().title).append("** to the queue.");
sb.append("Added **").append(track.getTitle()).append("** to the queue.");
}
//If there's some tracks in the queue, get the time until this one plays.
if (timeUntilPlay > 0){

View File

@ -1,212 +0,0 @@
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.view.BotLog;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
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.
*/
public class Playlist {
private String name;
private long creatorUID;
private List<AudioTrack> tracks;
/**
* Creates an empty playlist template.
* @param name The name of the playlist.
* @param creatorUID The ID of the user who created it.
*/
public Playlist(String name, long creatorUID){
this.name = name;
this.creatorUID = creatorUID;
this.tracks = new ArrayList<>();
}
/**
* Creates a playlist from a file with the given name.
* @param name The name of the file.
*/
public Playlist(String name, AudioPlayerManager playerManager){
this.name = name;
this.load(playerManager);
}
public String getName(){
return this.name;
}
public long getCreatorUID(){
return this.creatorUID;
}
public List<AudioTrack> getTracks(){
return this.tracks;
}
/**
* Adds a track to the end of the playlist.
* @param track The track to add.
*/
public void addTrack(AudioTrack track){
this.tracks.add(track);
}
/**
* Removes a track from the playlist.
* @param track The track to remove.
*/
public void removeTrack(AudioTrack track){
this.tracks.remove(track);
}
/**
* Copies all tracks from a specified playlist to this one.
* @param other The other playlist to make a copy of.
*/
public void copyFrom(Playlist other){
this.tracks.clear();
other.getTracks().forEach(track -> this.tracks.add(track.makeClone()));
}
/**
* Returns the next track, i.e. the first one in the list, and removes it from the internal list.
* @return The AudioTrack that should be played next.
*/
public AudioTrack getNextTrackAndRemove(boolean shouldShuffle){
if (this.tracks.isEmpty()){
return null;
}
return this.tracks.remove((shouldShuffle ? getShuffledIndex(this.tracks.size()) : 0));
}
/**
* Returns the next track to be played, and re-adds it to the end of the playlist, as it would do in a loop.
* @return The next track to be played.
*/
public AudioTrack getNextTrackAndRequeue(boolean shouldShuffle){
if (this.tracks.isEmpty()){
return null;
}
AudioTrack track = this.tracks.remove((shouldShuffle ? getShuffledIndex(this.tracks.size()) : 0));
this.tracks.add(track);
return track;
}
/**
* Gets a 'shuffled index' from a given list length. That means:
* - A random number from 0 to (listLength-1) - threshold*(listLength), where threshold is some percentage of
* recent songs that should be ignored; for example, the most recent 20% of the playlist can be ignored.
* - A greater likelihood for numbers closer to 0 (those which have not been played in a while).
* @param listLength The number of items in a potential list to choose from.
* @return A pseudo-random choice as to which item to pick from the list.
*/
public static int getShuffledIndex(int listLength){
float threshold = 0.2f;
int trueLength = listLength - (int)threshold*listLength;
Random rand = new Random();
//TODO Add in a small gradient in chance for a song to be picked.
return rand.nextInt(trueLength);
}
/**
* Saves the playlist to a file in its name. The playlists are saved into a file in the user's home directory.
*/
public void save(){
String homeDir = System.getProperty("user.home");
File playlistDir = new File(homeDir+"/.handiebot/playlist");
if (!playlistDir.exists()){
if (!playlistDir.mkdirs()){
log.log(BotLog.TYPE.ERROR, "Unable to make directory: "+playlistDir.getPath());
return;
}
}
File playlistFile = new File(playlistDir.getPath()+"/"+this.name.replace(" ", "_")+".txt");
log.log(BotLog.TYPE.INFO, "Saving playlist to: "+playlistFile.getAbsolutePath());
try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(playlistFile)))){
writer.write(this.name+'\n');
writer.write(Long.toString(this.creatorUID)+'\n');
writer.write(Integer.toString(this.tracks.size())+'\n');
for (AudioTrack track : this.tracks){
writer.write(track.getInfo().uri);
writer.write('\n');
}
} catch (FileNotFoundException e) {
log.log(BotLog.TYPE.ERROR, "Unable to find file to write playlist: "+this.name);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Loads the playlist from a file with the playlist's name.
*/
public void load(AudioPlayerManager playerManager){
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);
if (playlistFile.exists()){
try {
List<String> lines = Files.readAllLines(Paths.get(playlistFile.toURI()));
this.name = lines.remove(0);
this.creatorUID = Long.parseLong(lines.remove(0));
int trackCount = Integer.parseInt(lines.remove(0));
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();
}
} 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();
}
}
}
}

View File

@ -1,12 +1,13 @@
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;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
import handiebot.HandieBot;
import handiebot.lavaplayer.playlist.Playlist;
import handiebot.lavaplayer.playlist.UnloadedTrack;
import handiebot.view.BotLog;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IGuild;
@ -36,11 +37,24 @@ 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){
super();
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");
//this.activePlaylist = new Playlist("HandieBot Active Playlist");
}
/**
* Fills the playlist with the tracks from a given playlist, or if null,
* @param playlist the playlist to load from.
*/
public void setPlaylist(Playlist playlist){
this.activePlaylist = playlist;
}
public Playlist getActivePlaylist(){
return this.activePlaylist;
}
/**
@ -85,7 +99,7 @@ public class TrackScheduler extends AudioEventAdapter {
if (currentTrack != null){
t += currentTrack.getDuration() - currentTrack.getPosition();
}
for (AudioTrack track : this.queueList()){
for (UnloadedTrack track : this.queueList()){
t += track.getDuration();
}
return t;
@ -95,7 +109,7 @@ public class TrackScheduler extends AudioEventAdapter {
* Returns a list of tracks in the queue.
* @return A list of tracks in the queue.
*/
public List<AudioTrack> queueList(){
public List<UnloadedTrack> queueList(){
return this.activePlaylist.getTracks();
}
@ -103,9 +117,9 @@ public class TrackScheduler extends AudioEventAdapter {
* Add the next track to the queue or play right away if nothing is in the queue.
* @param track The track to play or add to the queue.
*/
public void queue(AudioTrack track){
public void queue(UnloadedTrack track){
if (player.getPlayingTrack() == null){
player.startTrack(track, false);
player.startTrack(track.loadAudioTrack(), false);
} else {
this.activePlaylist.addTrack(track);
}
@ -119,7 +133,7 @@ public class TrackScheduler extends AudioEventAdapter {
if (currentTrack != null){
this.player.stopTrack();
}
AudioTrack track = (this.repeat ? this.activePlaylist.getNextTrackAndRequeue(this.shuffle) : this.activePlaylist.getNextTrackAndRemove(this.shuffle));
AudioTrack track = this.activePlaylist.loadNextTrack(this.shuffle);
if (track != null) {
IVoiceChannel voiceChannel = HandieBot.musicPlayer.getVoiceChannel(this.guild);
if (!voiceChannel.isConnected()){
@ -155,6 +169,9 @@ public class TrackScheduler extends AudioEventAdapter {
@Override
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
if (this.repeat){
this.activePlaylist.addTrack(new UnloadedTrack(track));
}
if (endReason.mayStartNext){
nextTrack();
} else {

View File

@ -0,0 +1,198 @@
package handiebot.lavaplayer.playlist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
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 static handiebot.HandieBot.log;
/**
* @author Andrew Lalis
* A Playlist is a list of Tracks 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 {
private String name;
private List<UnloadedTrack> tracks;
/**
* Creates an empty playlist template.
* Depending on the circumstances, you may need to call {@code load()} to fill the playlist from a file.
* @param name The name of the playlist.
*/
public Playlist(String name){
this.name = name;
this.tracks = new ArrayList<>();
}
public String getName() {
return this.name;
}
public int getTrackCount(){
return this.tracks.size();
}
public List<UnloadedTrack> getTracks(){
return this.tracks;
}
public void addTrack(UnloadedTrack track){
this.tracks.add(track);
}
public void removeTrack(UnloadedTrack track){
this.tracks.remove(track);
}
/**
* Loads and returns the audio track that's first on the list.
* This removes that track from the playlist.
* @param shouldShuffle If this is true, the track returned will be chosen randomly.
* @return The AudioTrack corresponding to the next UnloadedTrack in the list.
*/
public AudioTrack loadNextTrack(boolean shouldShuffle){
if (shouldShuffle){
return this.tracks.remove(getShuffledIndex(this.tracks.size())).loadAudioTrack();
} else {
return this.tracks.remove(0).loadAudioTrack();
}
}
/**
* 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 {
UnloadedTrack track = new UnloadedTrack(url);
this.tracks.add(track);
log.log(BotLog.TYPE.MUSIC, "Added "+track.getTitle()+" to playlist ["+this.name+"].");
} catch (Exception e) {
log.log(BotLog.TYPE.ERROR, "Unable to add "+url+" to the playlist ["+this.name+"].");
e.printStackTrace();
}
}
/**
* Gets a 'shuffled index' from a given list length. That means:
* - A random number from 0 to (listLength-1) - threshold*(listLength), where threshold is some percentage of
* recent songs that should be ignored; for example, the most recent 20% of the playlist can be ignored.
* - A greater likelihood for numbers closer to 0 (those which have not been played in a while).
* @param listLength The number of items in a potential list to choose from.
* @return A pseudo-random choice as to which item to pick from the list.
*/
public static int getShuffledIndex(int listLength){
float threshold = 0.2f;
int trueLength = listLength - (int)threshold*listLength;
Random rand = new Random();
//TODO Add in a small gradient in chance for a song to be picked.
return rand.nextInt(trueLength);
}
/**
* Saves the playlist to a file in its name. The playlists are saved into a file in the user's home directory.
*/
public void save(){
String homeDir = System.getProperty("user.home");
File playlistDir = new File(homeDir+"/.handiebot/playlist");
if (!playlistDir.exists()){
if (!playlistDir.mkdirs()){
log.log(BotLog.TYPE.ERROR, "Unable to make directory: "+playlistDir.getPath());
return;
}
}
File playlistFile = new File(playlistDir.getPath()+"/"+this.name.replace(" ", "_")+".txt");
log.log(BotLog.TYPE.INFO, "Saving playlist to: "+playlistFile.getAbsolutePath());
try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(playlistFile)))){
writer.write(Integer.toString(this.tracks.size())+'\n');
for (UnloadedTrack track : this.tracks){
writer.write(track.toString());
writer.write('\n');
}
} catch (FileNotFoundException e) {
log.log(BotLog.TYPE.ERROR, "Unable to find file to write playlist: "+this.name);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Loads the playlist from a file with the playlist's name.
*/
public void load(){
String path = System.getProperty("user.home")+"/.handiebot/playlist/"+name.replace(" ", "_")+".txt";
log.log(BotLog.TYPE.INFO, "Loading playlist from: "+path);
File playlistFile = new File(path);
if (playlistFile.exists()){
try {
List<String> lines = Files.readAllLines(Paths.get(playlistFile.toURI()));
int trackCount = Integer.parseInt(lines.remove(0));
this.name = name;
this.tracks = new ArrayList<>(trackCount);
for (int i = 0; i < trackCount; i++){
String[] words = lines.remove(0).split(" / ");
this.tracks.add(new UnloadedTrack(words[0], words[1], Long.parseLong(words[2])));
}
} catch (IOException e) {
log.log(BotLog.TYPE.ERROR, "IOException 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<String> getAvailablePlaylists(){
File playlistFolder = new File(System.getProperty("user.home")+"/.handiebot/playlist");
List<String> names = new ArrayList<String>(Arrays.asList(playlistFolder.list()));
for (int i = 0; i < names.size(); i++){
String name = names.get(i);
name = name.replace(".txt", "");
name = name.replace("_", " ");
names.set(i, name);
}
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<String> names = getAvailablePlaylists();
for (String n : names){
if (n.equals(name)){
return true;
}
}
return false;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder("HandieBot Playlist: "+this.getName()+'\n');
for (int i = 0; i < this.getTrackCount(); i++){
sb.append(i+1).append(". ").append(this.tracks.get(i).getTitle()).append(" ").append(this.tracks.get(i).getFormattedDuration()).append("\n");
}
return sb.toString();
}
}

View File

@ -0,0 +1,168 @@
package handiebot.lavaplayer.playlist;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
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.util.concurrent.ExecutionException;
import static handiebot.HandieBot.log;
/**
* @author Andrew Lalis
* Class for describing a track without the actual audio track.
* This is useful for quickly loading playlists and only loading a track when it is needed.
*/
public class UnloadedTrack implements Cloneable {
private String title;
private String url;
private long duration;
/**
* Constructs a new unloaded track.
* This assumes that the url is known to be error free, so it will avoid a time consuming validation check.
* @param title The title of the track.
* @param url The url of the track, used when loading.
* @param duration The duration, in milliseconds(ms) of the song.
*/
public UnloadedTrack(String title, String url, long duration){
this.title = title;
this.url = url;
this.duration = duration;
}
/**
* Constructs a new unloaded track from a given url.
* Therefore, this method will take time to query youtube/soundcloud to receive a valid audio track.
* This is meant to ensure that this unloaded track is reliable.
* @param songURL The url to load from.
*/
public UnloadedTrack(String songURL) throws Exception {
this.title = null;
this.url = null;
this.duration = 0;
try {
HandieBot.musicPlayer.getPlayerManager().loadItem(songURL, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack audioTrack) {
title = audioTrack.getInfo().title;
url = audioTrack.getInfo().uri;
duration = audioTrack.getDuration();
}
@Override
public void playlistLoaded(AudioPlaylist audioPlaylist) {
log.log(BotLog.TYPE.ERROR, "Attempt to load playlist to create unloaded track.");
}
@Override
public void noMatches() {
log.log(BotLog.TYPE.ERROR, "No matches found for " + songURL);
}
@Override
public void loadFailed(FriendlyException e) {
log.log(BotLog.TYPE.ERROR, "Loading track failed for " + songURL);
}
}).get();
} catch (InterruptedException | ExecutionException e) {
log.log(BotLog.TYPE.ERROR, "Exception occurred while loading item from URL: "+songURL);
e.printStackTrace();
}
if (this.title == null){
throw new Exception("Invalid URL: "+songURL);
}
}
/**
* Constructs a new unloaded track from an already existing audio track.
* @param track The track to use.
*/
public UnloadedTrack(AudioTrack track){
this.title = track.getInfo().title;
this.url = track.getInfo().uri;
this.duration = track.getDuration();
}
public String getTitle(){
return this.title;
}
public String getURL(){
return this.url;
}
public long getDuration(){
return this.duration;
}
/**
* Loads the real audio track from the internet, and returns it.
* @return an AudioTrack representing this track.
*/
public AudioTrack loadAudioTrack(){
final AudioTrack[] track = {null};
try {
HandieBot.musicPlayer.getPlayerManager().loadItem(this.url, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack audioTrack) {
track[0] = audioTrack;
}
@Override
public void playlistLoaded(AudioPlaylist audioPlaylist) {
log.log(BotLog.TYPE.ERROR, "Attempt to load playlist to create unloaded track.");
}
@Override
public void noMatches() {
log.log(BotLog.TYPE.ERROR, "No matches found for " + url);
}
@Override
public void loadFailed(FriendlyException e) {
log.log(BotLog.TYPE.ERROR, "Loading track failed for " + url);
}
}).get();
} catch (InterruptedException | ExecutionException e) {
log.log(BotLog.TYPE.ERROR, "Exception occurred while loading item from URL: "+url);
e.printStackTrace();
}
return track[0];
}
/**
* Returns the duration of the track in an aesthetically pleasing way.
* Format is as follows: [mm:ss]
* @return A string representation of the duration of a track.
*/
public String getFormattedDuration(){
int seconds = (int) (this.duration / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
return String.format("[%d:%02d]", minutes, seconds);
}
@Override
public String toString(){
return this.title + " / " + this.url + " / " + Long.toString(this.duration);
}
/**
* Creates a clone of this track.
* @return A clone of this track.
*/
public UnloadedTrack clone(){
try {
super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return new UnloadedTrack(this.title, this.url, this.duration);
}
}

View File

@ -0,0 +1,56 @@
package handiebot.utils;
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 java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
/**
* @author Andrew Lalis
* Class to easily paste to pastebin.
*/
public class Pastebin {
private static String PASTEBIN_KEY = "769adc01154922ece448cabd7a33b57c";
public static String paste(String title, String content){
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://www.pastebin.com/api/api_post.php");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("api_dev_key", PASTEBIN_KEY));
params.add(new BasicNameValuePair("api_option", "paste"));
params.add(new BasicNameValuePair("api_paste_code", content));
params.add(new BasicNameValuePair("api_paste_private", "0"));
params.add(new BasicNameValuePair("api_paste_name", title));
params.add(new BasicNameValuePair("api_paste_expire_date", "10M"));
params.add(new BasicNameValuePair("api_user_key", ""));
try {
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
if (entity != null){
try (InputStream in = entity.getContent()){
StringWriter writer = new StringWriter();
IOUtils.copy(in, writer, "UTF-8");
return writer.toString();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -2,10 +2,12 @@ package handiebot.view;
import handiebot.HandieBot;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
/**
* @author Andrew Lalis
@ -27,6 +29,11 @@ public class BotWindow extends JFrame {
}
}
});
try {
setIconImage(ImageIO.read(getClass().getClassLoader().getResourceAsStream("avatarIcon.png")));
} catch (IOException e) {
e.printStackTrace();
}
setContentPane(view.mainPanel);
setJMenuBar(new MenuBar());
setPreferredSize(new Dimension(800, 600));

View File

@ -1,6 +1,5 @@
package handiebot.view.actions.music;
import handiebot.HandieBot;
import sx.blah.discord.handle.obj.IGuild;
import java.awt.event.ActionEvent;
@ -23,10 +22,6 @@ public class PlayAction extends MusicAction {
@Override
public void actionPerformed(ActionEvent e) {
if (this.args == null || this.args.length < 1){
HandieBot.musicPlayer.playQueue(this.guild);
} else {
HandieBot.musicPlayer.loadToQueue(this.guild, this.args[0]);
}
System.out.println("Play action.");
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

View File

@ -1,108 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="256"
height="256"
viewBox="0 0 256 256"
id="svg4136"
version="1.1"
inkscape:version="0.91 r13725"
sodipodi:docname="icon.svg"
inkscape:export-filename="C:\Users\AndrewComputer\Documents\Programming\IntelliJ_Projects\handiebot\src\main\resources\icon.png"
inkscape:export-xdpi="360"
inkscape:export-ydpi="360">
<defs
id="defs4138">
<marker
inkscape:stockid="Arrow1Lstart"
orient="auto"
refY="0.0"
refX="0.0"
id="Arrow1Lstart"
style="overflow:visible"
inkscape:isstock="true">
<path
id="path4701"
d="M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z "
style="fill-rule:evenodd;stroke:#ffffff;stroke-width:1pt;stroke-opacity:1;fill:#ffffff;fill-opacity:1"
transform="scale(0.8) translate(12.5,0)" />
</marker>
</defs>
<sodipodi:namedview
id="base"
pagecolor="#00024b"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="1"
inkscape:pageshadow="2"
inkscape:zoom="2.8"
inkscape:cx="140.27403"
inkscape:cy="113.84621"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
units="px"
inkscape:window-width="1920"
inkscape:window-height="1017"
inkscape:window-x="-8"
inkscape:window-y="-8"
inkscape:window-maximized="1" />
<metadata
id="metadata4141">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-796.36219)">
<path
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#ffffff;stroke-width:12;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="M 26.071429,935.93362 C 83.571429,805.57648 182.26443,803.4602 238.57143,935.57647 226.96157,777.68558 38.855639,775.90407 26.071429,935.93362 Z"
id="path4689"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccc" />
<ellipse
style="opacity:1;fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#ffffff;stroke-width:12;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="path4979"
cx="223.82457"
cy="925.21936"
rx="19.245705"
ry="31.492556" />
<use
x="0"
y="0"
xlink:href="#path4979"
id="use4986"
transform="translate(-181.78571,-4.4666172e-7)"
width="100%"
height="100%"
style="stroke-width:12;stroke-miterlimit:4;stroke-dasharray:none" />
<g
id="g4981">
<ellipse
ry="73.582115"
rx="83.534172"
cy="936.95587"
cx="132.5"
id="path4691"
style="opacity:1;fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#ff6900;stroke-width:12;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB