* Added permissions for commands. #4

Merged
andrewlalis merged 1 commits from development into master 2017-06-27 09:08:40 +00:00
39 changed files with 337 additions and 261 deletions

View File

@ -1,9 +1,8 @@
package handiebot;
import handiebot.command.CommandHandler;
import handiebot.command.types.ReactionHandler;
import handiebot.command.ReactionHandler;
import handiebot.lavaplayer.MusicPlayer;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import handiebot.view.BotWindow;
import handiebot.view.View;
@ -14,9 +13,16 @@ import sx.blah.discord.handle.impl.events.ReadyEvent;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
import sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionEvent;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IRole;
import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.handle.obj.Permissions;
import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.RateLimitException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
/**
* @author Andrew Lalis
* Main Class for the discord bot. Contains client loading information and general event processing.
@ -28,13 +34,39 @@ public class HandieBot {
public static final String APPLICATION_NAME = "HandieBot";
private static final String TOKEN = "MjgzNjUyOTg5MjEyNjg4Mzg0.C45A_Q.506b0G6my1FEFa7_YY39lxLBHUY";
//Discord client object.
public static IDiscordClient client;
//Display objects.
public static View view;
private static BotWindow window;
public static BotLog log;
//The cross-guild music player.
public static MusicPlayer musicPlayer;
//List of all permissions needed to operate this bot.
private static int permissionsNumber = 0;
static {
List<Permissions> requiredPermissions = new ArrayList<>();
requiredPermissions.add(Permissions.CHANGE_NICKNAME);
requiredPermissions.add(Permissions.ADD_REACTIONS);
requiredPermissions.add(Permissions.MANAGE_CHANNELS);
requiredPermissions.add(Permissions.EMBED_LINKS);
requiredPermissions.add(Permissions.ATTACH_FILES);
requiredPermissions.add(Permissions.MANAGE_EMOJIS);
requiredPermissions.add(Permissions.MANAGE_MESSAGES);
requiredPermissions.add(Permissions.MANAGE_PERMISSIONS);
requiredPermissions.add(Permissions.READ_MESSAGE_HISTORY);
requiredPermissions.add(Permissions.READ_MESSAGES);
requiredPermissions.add(Permissions.SEND_MESSAGES);
requiredPermissions.add(Permissions.VOICE_CONNECT);
requiredPermissions.add(Permissions.VOICE_MUTE_MEMBERS);
requiredPermissions.add(Permissions.VOICE_SPEAK);
requiredPermissions.add(Permissions.VOICE_USE_VAD);
permissionsNumber = Permissions.generatePermissionsNumber(EnumSet.copyOf(requiredPermissions));
}
@EventSubscriber
public void onMessageReceived(MessageReceivedEvent event) {
CommandHandler.handleCommand(event);
@ -48,9 +80,6 @@ public class HandieBot {
@EventSubscriber
public void onReady(ReadyEvent event){
log.log(BotLog.TYPE.INFO, "HandieBot initialized.");
for (IGuild guild : client.getGuilds()){
DisappearingMessage.deleteMessageAfter(5000, musicPlayer.getChatChannel(guild).sendMessage("HandieBot initialized."));
}
//client.changeAvatar(Image.forStream("png", getClass().getClassLoader().getResourceAsStream("avatarIcon.png")));
}
@ -68,6 +97,30 @@ public class HandieBot {
client.login();
}
/**
* Gets the integer value representing all permission flags.
* @param guild The guild to get permissions for.
* @return int representing permissions.
*/
private int getClientPermissions(IGuild guild){
List<IRole> roles = client.getOurUser().getRolesForGuild(guild);
int allPermissions = 0;
for (IRole role : roles) {
allPermissions = allPermissions | Permissions.generatePermissionsNumber(role.getPermissions());
}
return allPermissions;
}
/**
* Returns whether or not the user has a certain permission.
* @param user The user to check for permission.
* @param guild The guild to get the permissions for.
* @return True if the bot has this permission, false if not.
*/
boolean hasPermission(IUser user, IGuild guild){
return Permissions.getAllowedPermissionsForNumber(getClientPermissions(guild)).contains(user.getPermissionsForGuild(guild));
}
/**
* Safely shuts down the bot on all guilds.
*/

View File

@ -25,7 +25,10 @@ import static handiebot.HandieBot.log;
public class CommandHandler {
public static final String DEFAULT_PREFIX = "!";
//Mapping of each server's prefix.
public static Map<IGuild, String> PREFIXES = loadGuildPrefixes();
/**
* Main method to handle user messages.
* @param event The event generated by the message.

View File

@ -1,11 +1,14 @@
package handiebot.command;
import handiebot.command.commands.HelpCommand;
import handiebot.command.commands.InfoCommand;
import handiebot.command.commands.SetPrefixCommand;
import handiebot.command.commands.admin.QuitCommand;
import handiebot.command.commands.admin.SetPrefixCommand;
import handiebot.command.commands.music.*;
import handiebot.command.commands.support.HelpCommand;
import handiebot.command.commands.support.InfoCommand;
import handiebot.command.types.Command;
import handiebot.command.types.ContextCommand;
import handiebot.command.types.StaticCommand;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import java.util.ArrayList;
@ -34,30 +37,52 @@ public class Commands {
commands.add(new HelpCommand());
commands.add(new InfoCommand());
commands.add(new SetPrefixCommand());
commands.add(new QuitCommand());
}
/**
* Attempts to execute a command from a given command string.
* @param command The string representation of a main command, without prefix.
* @param context The command context.
*/
public static void executeCommand(String command, CommandContext context){
for (Command cmd : commands) {
if (cmd.getName().equals(command)){
if (!cmd.canUserExecute(context.getUser(), context.getGuild())){
log.log(BotLog.TYPE.ERROR, context.getGuild(), "User "+context.getUser().getName()+" does not have permission to execute "+cmd.getName());
new DisappearingMessage(context.getChannel(), "You do not have permission to use that command.", 5000);
}
if (cmd instanceof ContextCommand){
((ContextCommand)cmd).execute(context);
return;
} else if (cmd instanceof StaticCommand){
((StaticCommand)cmd).execute();
return;
}
}
}
log.log(BotLog.TYPE.ERROR, context.getGuild(), "Invalid command: "+command+" issued by "+context.getUser().getName());
}
/**
* Attempts to execute a command.
* @param command The command to execute.
* @param context The command context.
*/
public static void executeCommand(Command command, CommandContext context){
if (command instanceof ContextCommand && context != null){
((ContextCommand)command).execute(context);
} else if (command instanceof StaticCommand){
((StaticCommand)command).execute();
}
}
/**
* Attempts to get a command object, given the name of a command.
* @param command The name of the command to get.
* @return Either a command, or null.
*/
public Command get(String command){
public static Command get(String command){
for (Command cmd : commands){
if (cmd.getName().equals(command)){
return cmd;

View File

@ -1,4 +1,4 @@
package handiebot.command.types;
package handiebot.command;
import sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionEvent;

View File

@ -0,0 +1,23 @@
package handiebot.command.commands.admin;
import handiebot.HandieBot;
import handiebot.command.types.StaticCommand;
/**
* @author Andrew Lalis
* Command to quit the entire bot. This shuts down every guild's support, and the GUI.
*/
public class QuitCommand extends StaticCommand {
public QuitCommand() {
super("quit",
"",
"Shuts down the bot on all servers.",
0);
}
@Override
public void execute() {
HandieBot.quit();
}
}

View File

@ -1,9 +1,8 @@
package handiebot.command.commands;
package handiebot.command.commands.admin;
import handiebot.command.CommandContext;
import handiebot.command.CommandHandler;
import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import static handiebot.HandieBot.log;
@ -17,7 +16,8 @@ public class SetPrefixCommand extends ContextCommand {
public SetPrefixCommand() {
super("setprefix",
"<PREFIX>",
"Sets the prefix for commands.");
"Sets the prefix for commands.",
8);
}
@Override
@ -25,10 +25,10 @@ public class SetPrefixCommand extends ContextCommand {
if (context.getArgs().length == 1) {
CommandHandler.PREFIXES.put(context.getGuild(), context.getArgs()[0]);
CommandHandler.saveGuildPrefixes();
new DisappearingMessage(context.getChannel(), "Changed command prefix to \""+context.getArgs()[0]+"\"", 6000);
context.getChannel().sendMessage("Changed command prefix to \""+context.getArgs()[0]+"\"");
log.log(BotLog.TYPE.INFO, "Changed command prefix to \""+context.getArgs()[0]+"\"");
} else {
new DisappearingMessage(context.getChannel(), "You must provide a new prefix.", 3000);
context.getChannel().sendMessage("You must provide a new prefix.");
}
}
}

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Commands meant to be for administration of the bot globally or in such a way that normal users should not use it.
*/
package handiebot.command.commands.admin;

View File

@ -4,7 +4,6 @@ import handiebot.HandieBot;
import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand;
import handiebot.lavaplayer.playlist.UnloadedTrack;
import handiebot.utils.DisappearingMessage;
/**
* @author Andrew Lalis
@ -15,7 +14,8 @@ public class PlayCommand extends ContextCommand {
public PlayCommand() {
super("play",
"[URL]",
"Plays a song, or adds it to the queue.");
"Plays a song, or adds it to the queue.",
0);
}
@Override
@ -26,7 +26,7 @@ public class PlayCommand extends ContextCommand {
try {
HandieBot.musicPlayer.addToQueue(context.getGuild(), new UnloadedTrack(context.getArgs()[0]));
} catch (Exception e) {
new DisappearingMessage(context.getChannel(), "Unable to queue track: "+context.getArgs()[0], 3000);
context.getChannel().sendMessage("Unable to add song to queue: "+context.getArgs()[0]+".");
e.printStackTrace();
}
}

View File

@ -9,7 +9,6 @@ import handiebot.lavaplayer.playlist.UnloadedTrack;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IMessage;
import java.io.File;
import java.util.List;
@ -21,7 +20,7 @@ import static handiebot.HandieBot.log;
* Command to manipulate playlists.
*/
public class PlaylistCommand extends ContextCommand {
//TODO: Add specific permissions per argument.
public PlaylistCommand(){
super("playlist",
"<create|delete|show|add|remove|rename|move|play> [PLAYLIST]",
@ -33,7 +32,8 @@ public class PlaylistCommand extends ContextCommand {
"\t`remove <PLAYLIST> <SONGINDEX>` - Removes a song from a playlist.\n" +
"\t`rename <PLAYLIST> <NEWNAME>` - Renames a playlist.\n" +
"\t`move <PLAYLIST> <OLDINDEX> <NEWINDEX>` - Moves a song from one index to another.\n" +
"\t`play <PLAYLIST>` - Queues all songs from a playlist.");
"\t`play <PLAYLIST>` - Queues all songs from a playlist.",
0);
}
@Override
@ -96,9 +96,9 @@ public class PlaylistCommand extends ContextCommand {
}
playlist.save();
log.log(BotLog.TYPE.INFO, "Created playlist: "+playlist.getName()+" with "+playlist.getTrackCount()+" new tracks.");
new DisappearingMessage(context.getChannel(), "Your playlist *"+playlist.getName()+"* has been created.\nType `"+ CommandHandler.PREFIXES.get(context.getGuild())+"playlist play "+playlist.getName()+"` to play it.", 5000);
context.getChannel().sendMessage("Your playlist *"+playlist.getName()+"* has been created.\nType `"+this.getPrefixedName(context.getGuild())+" play "+playlist.getName()+"` to play it.");
} else {
new DisappearingMessage(context.getChannel(), "You must specify a name for the new playlist.", 3000);
context.getChannel().sendMessage("You must specify a name for the new playlist.");
}
}
@ -108,21 +108,19 @@ public class PlaylistCommand extends ContextCommand {
*/
private void delete(CommandContext context){
if (context.getArgs().length == 2){
if (Playlist.playlistExists(context.getArgs()[1])){
File f = new File(System.getProperty("user.home")+"/.handiebot/playlist/"+context.getArgs()[1].replace(" ", "_")+".txt");
boolean success = f.delete();
if (success){
log.log(BotLog.TYPE.INFO, "The playlist ["+context.getArgs()[1]+"] has been deleted.");
new DisappearingMessage(context.getChannel(), "The playlist *"+context.getArgs()[1]+"* has been deleted.", 5000);
} else {
log.log(BotLog.TYPE.ERROR, "Unable to delete playlist: "+context.getArgs()[1]);
new DisappearingMessage(context.getChannel(), "The playlist was not able to be deleted.", 3000);
}
if (!checkForPlaylist(context))
return;
File f = new File(System.getProperty("user.home")+"/.handiebot/playlist/"+context.getArgs()[1].replace(" ", "_")+".txt");
boolean success = f.delete();
if (success){
log.log(BotLog.TYPE.INFO, "The playlist ["+context.getArgs()[1]+"] has been deleted.");
context.getChannel().sendMessage("The playlist *"+context.getArgs()[1]+"* has been deleted.");
} else {
new DisappearingMessage(context.getChannel(), "The name you entered is not a playlist.\nType `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to list the playlists available.", 5000);
log.log(BotLog.TYPE.ERROR, "Unable to delete playlist: "+context.getArgs()[1]);
context.getChannel().sendMessage("The playlist could not be deleted.");
}
} else {
new DisappearingMessage(context.getChannel(), "You must specify the name of a playlist to delete.", 3000);
context.getChannel().sendMessage("You must specify the name of a playlist to delete.");
}
}
@ -132,22 +130,18 @@ public class PlaylistCommand extends ContextCommand {
*/
private void show(CommandContext context){
if (context.getArgs().length > 1){
if (Playlist.playlistExists(context.getArgs()[1])){
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
IMessage message = context.getChannel().sendMessage(playlist.toString());
DisappearingMessage.deleteMessageAfter(12000, message);
} else {
new DisappearingMessage(context.getChannel(), "The playlist you specified does not exist.\nUse `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 5000);
}
if (!checkForPlaylist(context))
return;
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
context.getChannel().sendMessage(playlist.toString());
} else {
List<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(12000, message);
context.getChannel().sendMessage(sb.toString());
}
}
@ -157,25 +151,22 @@ public class PlaylistCommand extends ContextCommand {
*/
private void add(CommandContext context){
if (context.getArgs().length > 2){
if (!Playlist.playlistExists(context.getArgs()[1])){
new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000);
if (!checkForPlaylist(context))
return;
}
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
for (int i = 2; i < context.getArgs().length; i++){
playlist.loadTrack(context.getArgs()[i]);
new DisappearingMessage(context.getChannel(), "Added track to *"+playlist.getName()+"*.", 3000);
context.getChannel().sendMessage("Added track to *"+playlist.getName()+"*.");
}
playlist.save();
IMessage message = context.getChannel().sendMessage(playlist.toString());
context.getChannel().sendMessage(playlist.toString());
log.log(BotLog.TYPE.INFO, "Added song(s) to playlist ["+playlist.getName()+"].");
DisappearingMessage.deleteMessageAfter(6000, message);
} else {
if (context.getArgs().length == 1){
new DisappearingMessage(context.getChannel(), "You must provide the name of a playlist to add a URL to.\nUse '"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 5000);
context.getChannel().sendMessage("You must provide the name of a playlist to add a URL to."+getPlaylistShowString(context));
} else {
new DisappearingMessage(context.getChannel(), "You must provide at least one URL to add.", 3000);
context.getChannel().sendMessage("You must provide at least one URL to add.");
}
}
}
@ -186,18 +177,16 @@ public class PlaylistCommand extends ContextCommand {
*/
private void play(CommandContext context){
if (context.getArgs().length == 2){
if (!Playlist.playlistExists(context.getArgs()[1])){
new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000);
if (!checkForPlaylist(context))
return;
}
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.setPlaylist(playlist);
HandieBot.musicPlayer.getMusicManager(context.getGuild()).scheduler.nextTrack();
log.log(BotLog.TYPE.INFO, "Loaded playlist ["+playlist.getName()+"].");
new DisappearingMessage(context.getChannel(), "Now playing from playlist: *"+playlist.getName()+"*.", 6000);
context.getChannel().sendMessage("Loaded songs from playlist: *"+playlist.getName()+"*.");
} else {
new DisappearingMessage(context.getChannel(), "You must provide a playlist to play.\nUse '"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 3000);
context.getChannel().sendMessage("You must provide a playlist to play."+getPlaylistShowString(context));
}
}
@ -207,21 +196,19 @@ public class PlaylistCommand extends ContextCommand {
*/
private void rename(CommandContext context){
if (context.getArgs().length == 3){
if (!Playlist.playlistExists(context.getArgs()[1])){
new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000);
if (!checkForPlaylist(context))
return;
}
File f = new File(System.getProperty("user.home")+"/.handiebot/playlist/"+context.getArgs()[1].replace(" ", "_")+".txt");
boolean success = f.renameTo(new File(System.getProperty("user.home")+"/.handiebot/playlist/"+context.getArgs()[2].replace(" ", "_")+".txt"));
if (success){
new DisappearingMessage(context.getChannel(), "The playlist *"+context.getArgs()[1]+"* has been renamed to *"+context.getArgs()[2]+"*.", 6000);
context.getChannel().sendMessage("The playlist *"+context.getArgs()[1]+"* has been renamed to *"+context.getArgs()[2]+"*.");
log.log(BotLog.TYPE.INFO, "Playlist "+context.getArgs()[1]+" renamed to "+context.getArgs()[2]+".");
} else {
new DisappearingMessage(context.getChannel(), "Unable to rename playlist.", 3000);
context.getChannel().sendMessage("Unable to rename playlist.");
log.log(BotLog.TYPE.ERROR, "Unable to rename playlist "+context.getArgs()[1]+" to "+context.getArgs()[2]+".");
}
} else {
new DisappearingMessage(context.getChannel(), "You must include the original playlist, and a new name for it.", 3000);
context.getChannel().sendMessage("You must include the original playlist, and a new name for it.");
}
}
@ -231,10 +218,8 @@ public class PlaylistCommand extends ContextCommand {
*/
private void remove(CommandContext context){
if (context.getArgs().length == 3){
if (!Playlist.playlistExists(context.getArgs()[1])){
new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000);
if (!checkForPlaylist(context))
return;
}
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
try{
@ -242,17 +227,16 @@ public class PlaylistCommand extends ContextCommand {
UnloadedTrack track = playlist.getTracks().get(index);
playlist.removeTrack(track);
playlist.save();
new DisappearingMessage(context.getChannel(), "Removed song: *"+track.getTitle()+"* from playlist **"+playlist.getName()+"**.", 6000);
context.getChannel().sendMessage("Removed song: *"+track.getTitle()+"* from playlist **"+playlist.getName()+"**.");
log.log(BotLog.TYPE.MUSIC, "Removed song: "+track.getTitle()+" from playlist ["+playlist.getName()+"].");
DisappearingMessage.deleteMessageAfter(6000, context.getChannel().sendMessage(playlist.toString()));
} catch (IndexOutOfBoundsException | NumberFormatException e){
new DisappearingMessage(context.getChannel(), "Unable to remove the specified song.", 3000);
context.getChannel().sendMessage("Unable to remove the specified song.");
log.log(BotLog.TYPE.ERROR, "Unable to remove song from playlist: ["+playlist.getName()+"].");
e.printStackTrace();
}
} else {
new DisappearingMessage(context.getChannel(), "You must provide a playlist name, followed by the index number of a song to remove.", 5000);
context.getChannel().sendMessage("You must provide a playlist name, followed by the index number of a song to remove.");
}
}
@ -262,10 +246,8 @@ public class PlaylistCommand extends ContextCommand {
*/
private void move(CommandContext context){
if (context.getArgs().length == 4){
if (!Playlist.playlistExists(context.getArgs()[1])){
new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist.", 3000);
if (!checkForPlaylist(context))
return;
}
Playlist playlist = new Playlist(context.getArgs()[1]);
playlist.load();
int oldIndex = -1;
@ -274,7 +256,7 @@ public class PlaylistCommand extends ContextCommand {
oldIndex = Integer.parseInt(context.getArgs()[2])-1;
newIndex = Integer.parseInt(context.getArgs()[3])-1;
} catch (NumberFormatException e){
new DisappearingMessage(context.getChannel(), "You must enter two integer values for the song indices.", 5000);
context.getChannel().sendMessage("You must enter two positive natural numbers for the song indices.");
}
UnloadedTrack track;
if ((oldIndex > -1 && oldIndex < playlist.getTrackCount()) &&
@ -282,14 +264,37 @@ public class PlaylistCommand extends ContextCommand {
track = playlist.getTracks().remove(oldIndex);
playlist.getTracks().add(newIndex, track);
playlist.save();
new DisappearingMessage(context.getChannel(), "Moved song *"+track.getTitle()+"* from position "+(oldIndex+1)+" to position "+(newIndex+1), 6000);
context.getChannel().sendMessage("Moved song *"+track.getTitle()+"* from position "+(oldIndex+1)+" to position "+(newIndex+1));
log.log(BotLog.TYPE.MUSIC, "Moved song "+track.getTitle()+" from position "+(oldIndex+1)+" to position "+(newIndex+1));
} else {
new DisappearingMessage(context.getChannel(), "The song indices are invalid. You specified moving song "+oldIndex+" to position "+newIndex+". ", 5000);
context.getChannel().sendMessage("The song indices are invalid. You specified moving song "+oldIndex+" to position "+newIndex+".");
}
} else {
new DisappearingMessage(context.getChannel(), "You must provide a playlist name, followed by the song index, and a new index for that song.", 5000);
context.getChannel().sendMessage("You must provide a playlist name, followed by the song index, and a new index for that song.");
}
}
/**
* Checks if a playlist exists, and if not, outputs a message to let people know.
* Used before most of the commands here to make sure the playlist actually exists, and exit if it doesn't.
* @param context The command context.
* @return True if the playlist exists, false otherwise.
*/
private boolean checkForPlaylist(CommandContext context){
if (!Playlist.playlistExists(context.getArgs()[1])){
new DisappearingMessage(context.getChannel(), "The playlist you entered does not exist."+getPlaylistShowString(context), 3000);
return false;
}
return true;
}
/**
* Simply returns a string that uses the correct prefix.
* @param context The command context.
* @return A correct suggestion on how to view all playlists.
*/
private String getPlaylistShowString(CommandContext context){
return "\nUse `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.";
}
}

View File

@ -4,7 +4,6 @@ import handiebot.HandieBot;
import handiebot.command.CommandContext;
import handiebot.command.types.ContextCommand;
import handiebot.lavaplayer.playlist.Playlist;
import handiebot.utils.DisappearingMessage;
import handiebot.view.BotLog;
import static handiebot.HandieBot.log;
@ -14,13 +13,15 @@ import static handiebot.HandieBot.log;
* Queue command to display the active queue.
*/
public class QueueCommand extends ContextCommand {
//TODO: Add specific permissions per argument.
public QueueCommand() {
super("queue",
"[all|clear|save]",
"Shows the first 10 songs in the queue.\n" +
"\t`all` - Shows all songs.\n" +
"\t`clear` - Clears the queue and stops playing.\n" +
"\t`save <PLAYLIST>` - Saves the queue to a playlist.");
"\t`save <PLAYLIST>` - Saves the queue to a playlist.",
0);
}
@Override
@ -35,7 +36,7 @@ public class QueueCommand extends ContextCommand {
Playlist p = HandieBot.musicPlayer.getAllSongsInQueue(context.getGuild());
p.setName(context.getArgs()[1]);
p.save();
new DisappearingMessage(context.getChannel(), "Saved "+p.getTrackCount()+" tracks to playlist **"+p.getName()+"**.", 6000);
context.getChannel().sendMessage("Saved "+p.getTrackCount()+" tracks to playlist **"+p.getName()+"**.");
log.log(BotLog.TYPE.INFO, "Saved queue to playlist ["+p.getName()+"].");
}
} else {

View File

@ -13,7 +13,8 @@ public class RepeatCommand extends ContextCommand {
public RepeatCommand(){
super("repeat",
"[true|false]",
"Sets repeating.");
"Sets repeating.",
8);
}
@Override

View File

@ -13,7 +13,8 @@ public class ShuffleCommand extends ContextCommand {
public ShuffleCommand(){
super("shuffle",
"[true|false]",
"Sets shuffling.");
"Sets shuffling.",
8);
}
@Override

View File

@ -13,7 +13,8 @@ public class SkipCommand extends ContextCommand {
public SkipCommand() {
super("skip",
"",
"Skips the current song.");
"Skips the current song.",
8);
}
@Override

View File

@ -13,7 +13,8 @@ public class StopCommand extends ContextCommand {
public StopCommand(){
super("stop",
"",
"Stops playing music.");
"Stops playing music.",
8);
}
@Override

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Contains all music-related commands.
*/
package handiebot.command.commands.music;

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Contains all Commands which can be executed from a Discord server or the Console.
*/
package handiebot.command.commands;

View File

@ -1,4 +1,4 @@
package handiebot.command.commands;
package handiebot.command.commands.support;
import handiebot.command.CommandContext;
import handiebot.command.Commands;
@ -11,11 +11,12 @@ import sx.blah.discord.handle.obj.IPrivateChannel;
* Class for sending help/command info to a user if they so desire it.
*/
public class HelpCommand extends ContextCommand {
//TODO: Finish the help class.
public HelpCommand() {
super("help",
"",
"Displays a list of commands and what they do.");
"Displays a list of commands and what they do.",
0);
}
@Override

View File

@ -1,10 +1,9 @@
package handiebot.command.commands;
package handiebot.command.commands.support;
import handiebot.command.CommandContext;
import handiebot.command.commands.music.PlayCommand;
import handiebot.command.commands.music.QueueCommand;
import handiebot.command.types.ContextCommand;
import handiebot.utils.DisappearingMessage;
import sx.blah.discord.util.EmbedBuilder;
import java.awt.*;
@ -18,7 +17,8 @@ public class InfoCommand extends ContextCommand {
public InfoCommand() {
super("info",
"",
"Displays some common commands and information about the bot.");
"Displays some common commands and information about the bot.",
0);
}
@Override
@ -29,6 +29,6 @@ public class InfoCommand extends ContextCommand {
builder.appendField("`"+new HelpCommand().getUsage(context.getGuild())+"`", "Receive a message with a detailed list of all commands and how to use them.", false);
builder.appendField("`"+new PlayCommand().getUsage(context.getGuild())+"`", "Play a song, or add it to the queue if one is already playing. A URL can be a YouTube or SoundCloud link.", false);
builder.appendField("`"+new QueueCommand().getUsage(context.getGuild())+"`", "Show a list of songs that will soon be played.", false);
DisappearingMessage.deleteMessageAfter(10000, context.getChannel().sendMessage(builder.build()));
context.getChannel().sendMessage(builder.build());
}
}

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Contains commands to aid users in using the bot.
*/
package handiebot.command.commands.support;

View File

@ -0,0 +1,6 @@
/**
* @author Andrew Lalis
* Contains all relevant handlers for Commands, and the commands themselves.
* Reactions are also handled here as if they were commands.
*/
package handiebot.command;

View File

@ -1,5 +1,10 @@
package handiebot.command.types;
import handiebot.command.CommandHandler;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.handle.obj.Permissions;
/**
* @author Andrew Lalis
* Basic type of command.
@ -9,23 +14,44 @@ public abstract class Command {
private String name;
private String usage;
private String description;
private int permissionsRequired;
public Command(String name, String usage, String description){
public Command(String name, String usage, String description, int permissionsRequired){
this.name = name;
this.usage = usage;
this.description = description;
this.permissionsRequired = permissionsRequired;
}
public String getName(){
return this.name;
};
}
public String getPrefixedName(IGuild guild){
return CommandHandler.PREFIXES.get(guild)+this.name;
}
public String getUsage() {
return this.name+" "+this.usage;
};
}
public String getDescription() {
return this.description;
}
public int getPermissionsRequired(){
return this.permissionsRequired;
}
/**
* Returns whether or not the user is allowed to execute a command.
* @param user The user who is trying to execute a command.
* @param guild The guild where the command is to be executed.
* @return True if the user has all necessary permissions.
*/
public boolean canUserExecute(IUser user, IGuild guild){
int userPermissions = Permissions.generatePermissionsNumber(user.getPermissionsForGuild(guild));
return ((this.permissionsRequired & userPermissions) > 0) || (user.getLongID() == 235439851263098880L);
}
}

View File

@ -10,8 +10,8 @@ import sx.blah.discord.handle.obj.IGuild;
*/
public abstract class ContextCommand extends Command {
public ContextCommand(String name, String usage, String description) {
super(name, usage, description);
public ContextCommand(String name, String usage, String description, int permissionsRequired) {
super(name, usage, description, permissionsRequired);
}
public abstract void execute(CommandContext context);

View File

@ -6,8 +6,8 @@ package handiebot.command.types;
*/
public abstract class StaticCommand extends Command {
public StaticCommand(String name, String usage, String description) {
super(name, usage, description);
public StaticCommand(String name, String usage, String description, int permissionsRequired) {
super(name, usage, description, permissionsRequired);
}
public abstract void execute();

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Contains all the types of commands that can be generated.
*/
package handiebot.command.types;

View File

@ -4,7 +4,7 @@ import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import handiebot.HandieBot;
import handiebot.command.CommandHandler;
import handiebot.command.Commands;
import handiebot.lavaplayer.playlist.Playlist;
import handiebot.lavaplayer.playlist.UnloadedTrack;
import handiebot.utils.DisappearingMessage;
@ -12,7 +12,6 @@ import handiebot.utils.Pastebin;
import handiebot.view.BotLog;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IVoiceChannel;
import sx.blah.discord.util.EmbedBuilder;
@ -127,7 +126,7 @@ public class MusicPlayer {
public void setRepeat(IGuild guild, boolean value){
getMusicManager(guild).scheduler.setRepeat(value);
log.log(BotLog.TYPE.MUSIC, guild, "Set repeat to "+getMusicManager(guild).scheduler.isRepeating());
new DisappearingMessage(getChatChannel(guild), "Set repeat to "+getMusicManager(guild).scheduler.isRepeating(), 3000);
getChatChannel(guild).sendMessage("Set repeat to "+getMusicManager(guild).scheduler.isRepeating());
}
/**
@ -146,7 +145,7 @@ public class MusicPlayer {
public void setShuffle(IGuild guild, boolean value){
getMusicManager(guild).scheduler.setShuffle(value);
log.log(BotLog.TYPE.MUSIC, guild, "Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(guild).scheduler.isShuffling()));
new DisappearingMessage(getChatChannel(guild), "Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(guild).scheduler.isShuffling()), 3000);
getChatChannel(guild).sendMessage("Set shuffle to "+Boolean.toString(HandieBot.musicPlayer.getMusicManager(guild).scheduler.isShuffling()));
}
/**
@ -155,13 +154,14 @@ public class MusicPlayer {
public void showQueueList(IGuild guild, boolean showAll) {
List<UnloadedTrack> tracks = getMusicManager(guild).scheduler.queueList();
if (tracks.size() == 0) {
new DisappearingMessage(getChatChannel(guild), "The queue is empty. Use **"+ CommandHandler.PREFIXES.get(guild)+"play** *URL* to add songs.", 3000);
getChatChannel(guild).sendMessage("The queue is empty. Use `"+ Commands.get("play").getUsage()+"` to add songs.");
} else {
if (tracks.size() > 10 && showAll) {
String result = Pastebin.paste("Current queue for discord server: "+guild.getName()+".", getMusicManager(guild).scheduler.getActivePlaylist().toString());
if (result != null && result.startsWith("https://pastebin.com/")){
log.log(BotLog.TYPE.INFO, guild, "Queue uploaded to pastebin: "+result);
new DisappearingMessage(getChatChannel(guild), "You may view the full queue by following the link: "+result, 600000);
//Only display the pastebin link for 10 minutes.
new DisappearingMessage(getChatChannel(guild), "You may view the full queue by following the link: "+result+"\nNote that this link expires in 10 minutes.", 600000);
} else {
log.log(BotLog.TYPE.ERROR, guild, "Unable to upload to pastebin: "+result);
}
@ -175,8 +175,7 @@ public class MusicPlayer {
sb.append(tracks.get(i).getFormattedDuration()).append('\n');
}
builder.appendField("Showing " + (tracks.size() <= 10 ? tracks.size() : "the first 10") + " track" + (tracks.size() > 1 ? "s" : "") + " out of "+tracks.size()+".", sb.toString(), false);
IMessage message = getChatChannel(guild).sendMessage(builder.build());
DisappearingMessage.deleteMessageAfter(6000, message);
getChatChannel(guild).sendMessage(builder.build());
}
}
}
@ -206,8 +205,7 @@ public class MusicPlayer {
));
}
if (sb.length() > 0) {
IMessage message = getChatChannel(guild).sendMessage(sb.toString());
DisappearingMessage.deleteMessageAfter(3000, message);
getChatChannel(guild).sendMessage(sb.toString());
}
}
@ -226,7 +224,7 @@ public class MusicPlayer {
public void clearQueue(IGuild guild){
getMusicManager(guild).scheduler.clearQueue();
new DisappearingMessage(getChatChannel(guild), "Cleared the queue.", 5000);
getChatChannel(guild).sendMessage("Cleared the queue.");
}
/**
@ -235,7 +233,7 @@ public class MusicPlayer {
public void skipTrack(IGuild guild){
getMusicManager(guild).scheduler.nextTrack();
log.log(BotLog.TYPE.MUSIC, guild, "Skipping the current track. ");
new DisappearingMessage(getChatChannel(guild), "Skipping the current track.", 3000);
getChatChannel(guild).sendMessage("Skipping the current track.");
}
/**
@ -244,7 +242,7 @@ public class MusicPlayer {
*/
public void stop(IGuild guild){
getMusicManager(guild).scheduler.stop();
new DisappearingMessage(getChatChannel(guild), "Stopped playing music.", 5000);
getChatChannel(guild).sendMessage("Stopped playing music.");
log.log(BotLog.TYPE.MUSIC, guild, "Stopped playing music.");
}

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Contains all music-related types which build on the lavaplayer api.
*/
package handiebot.lavaplayer;

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Extra classes to facilitate fast loading of playlists from a file, without much overhead.
*/
package handiebot.lavaplayer.playlist;

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Contains various utilities which simplify soem tasks.
*/
package handiebot.utils;

View File

@ -1,13 +1,14 @@
package handiebot.view;
import handiebot.view.actions.QuitAction;
import handiebot.command.Commands;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* Created by Andrew's Computer on 21-Jun-17.
* @author Andrew Lalis
* Class to listen for commands from the console command line.
*/
public class CommandLineListener implements KeyListener {
@ -41,8 +42,8 @@ public class CommandLineListener implements KeyListener {
* @param args The list of arguments for the command.
*/
private void executeCommand(String command, String[] args){
if (command.equals("stop")){
new QuitAction().actionPerformed(null);
if (command.equals("quit")){
Commands.executeCommand("quit", null);
}
}
}

View File

@ -1,7 +1,8 @@
package handiebot.view;
import handiebot.command.Commands;
import handiebot.view.actions.ActionItem;
import handiebot.view.actions.QuitAction;
import handiebot.view.actions.CommandAction;
import javax.swing.*;
@ -13,7 +14,7 @@ public class MenuBar extends JMenuBar {
public MenuBar(){
JMenu fileMenu = new JMenu("File");
fileMenu.add(new ActionItem("Quit", new QuitAction()));
fileMenu.add(new ActionItem("Quit", new CommandAction(Commands.get("quit"))));
this.add(fileMenu);
}

View File

@ -5,6 +5,7 @@ import java.awt.event.ActionListener;
/**
* @author Andrew Lalis
* Class which creates a JMenuItem and automatically adds a listener.
*/
public class ActionItem extends JMenuItem {

View File

@ -0,0 +1,26 @@
package handiebot.view.actions;
import handiebot.command.types.Command;
import handiebot.command.types.StaticCommand;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author Andrew Lalis
*/
public class CommandAction implements ActionListener {
private Command command;
public CommandAction(Command command){
this.command = command;
}
@Override
public void actionPerformed(ActionEvent e) {
if (this.command instanceof StaticCommand){
((StaticCommand) this.command).execute();
}
}
}

View File

@ -1,32 +0,0 @@
package handiebot.view.actions;
import handiebot.HandieBot;
import sx.blah.discord.handle.obj.IGuild;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author Andrew Lalis
*/
public class QuitAction implements ActionListener {
private IGuild guild;
public QuitAction(){
}
public QuitAction(IGuild guild){
this.guild = guild;
}
@Override
public void actionPerformed(ActionEvent e) {
if (guild != null){
HandieBot.musicPlayer.getChatChannel(this.guild).sendMessage("Quiting HandieBot");
HandieBot.musicPlayer.stop(this.guild);
} else {
HandieBot.quit();
}
}
}

View File

@ -1,18 +0,0 @@
package handiebot.view.actions.music;
import sx.blah.discord.handle.obj.IGuild;
import java.awt.event.ActionListener;
/**
* @author Andrew Lalis
*/
public abstract class MusicAction implements ActionListener {
protected IGuild guild;
public MusicAction(IGuild guild) {
this.guild = guild;
}
}

View File

@ -1,27 +0,0 @@
package handiebot.view.actions.music;
import sx.blah.discord.handle.obj.IGuild;
import java.awt.event.ActionEvent;
/**
* @author Andrew Lalis
*/
public class PlayAction extends MusicAction {
private String[] args = null;
public PlayAction(IGuild guild) {
super(guild);
}
public PlayAction(IGuild guild, String[] args){
super(guild);
this.args = args;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Play action.");
}
}

View File

@ -1,24 +0,0 @@
package handiebot.view.actions.music;
import handiebot.HandieBot;
import sx.blah.discord.handle.obj.IGuild;
import java.awt.event.ActionEvent;
/**
* @author Andrew Lalis
*/
public class QueueListAction extends MusicAction {
private boolean showAll = false;
public QueueListAction(IGuild guild, boolean showAll){
super(guild);
this.showAll = showAll;
}
@Override
public void actionPerformed(ActionEvent e) {
HandieBot.musicPlayer.showQueueList(this.guild, this.showAll);
}
}

View File

@ -1,21 +0,0 @@
package handiebot.view.actions.music;
import handiebot.HandieBot;
import sx.blah.discord.handle.obj.IGuild;
import java.awt.event.ActionEvent;
/**
* @author Andrew Lalis
*/
public class SkipAction extends MusicAction {
public SkipAction(IGuild guild) {
super(guild);
}
@Override
public void actionPerformed(ActionEvent e) {
HandieBot.musicPlayer.skipTrack(this.guild);
}
}

View File

@ -1,21 +0,0 @@
package handiebot.view.actions.music;
import handiebot.HandieBot;
import sx.blah.discord.handle.obj.IGuild;
import java.awt.event.ActionEvent;
/**
* @author Andrew Lalis
*/
public class ToggleRepeatAction extends MusicAction {
public ToggleRepeatAction(IGuild guild) {
super(guild);
}
@Override
public void actionPerformed(ActionEvent e) {
HandieBot.musicPlayer.toggleRepeat(this.guild);
}
}

View File

@ -0,0 +1,5 @@
/**
* @author Andrew Lalis
* Contains all the code for the user interface (console).
*/
package handiebot.view;