/Jockie-Utils

A Discord command framework built on top of JDA

Primary LanguageJavaApache License 2.0Apache-2.0

This is a beta library

All suggestions are welcome and feel free to contribute
Be aware, this project does lack examples of all the features avaliable

Libraries:

https://github.com/DV8FromTheWorld/JDA (The Discord wrapper which the library is built upon)
https://github.com/google/guava (Used for simplified adding of commands, adding commands by package)

Gradle:

repositories {
	maven { url 'https://jitpack.io' }
}

dependencies {
	implementation 'com.github.21Joakim:Jockie-Utils:{latest-version}'
}

Command structure (Not complete, check examples for more):

All commands need to extend ICommand and the standard implementation of that is CommandImpl. CommandImpl allows arguments to be specified as the parameters of a function named onCommand.

Allowed parameters:

Any objects are allowed to be present as the parameters however if the object's class is not registered in the ArgumentFactory it will not generate any default arguments for it and throw an exception therefore you have to specifiy them yourself. There is one exception to this with two objects which can not be arguments, MessageReceivedEvent and CommandEvent. Any amount of MessageReceivedEvent and CommandEvent used in your parameters won't matter they will always refer to the context and nothing else. Both MessageReceivedEvent and CommandEvent are optional so none of them have to be specified in the parameters but most of the time you would want at least one of them.

The ArgumentFactory has a few already registered classes
All primitive data types
String and any Enum class
User, Member, TextChannel, VoiceChannel, Role and Emote from JDA

onCommand:

To specfiy the command you create a method called onCommand, here's a simple ping command
public class CommandPing extends CommandImpl {
	public CommandPing() {
		super("ping");
		
		super.setDescription("Simple ping command");
	}
	
	public void onCommand(MessageReceivedEvent event) {
		event.getChannel().sendMessage(event.getJDA().getPing() + " ms").queue();
	}
}

If you want to specify commands you simply add more parameters, here's a simple decide command which will decide between two statements

public class CommandDecide extends CommandImpl {
	
	/* No need to create a new one each time someone uses it */
	private Random random = new Random();

	public CommandDecide() {
		super("decide");
		
		super.setDescription("Give me two sentences and I will choose one of them");
	}
	
	public void onCommand(MessageReceivedEvent event, @Argument(description="statement", acceptQuote=true) String statement, @Argument(description="statement 2", acceptQuote=true) String statement2) {
		event.getChannel().sendMessage("**" + (this.random.nextBoolean() ? statement : statement2) + "**" + " seems more reasonable to me!").queue();
	}
}

The Argument annotation is used when you want to give the parameter specific properties, some of the properties include
endless - This is used when a String for instance needs to go over spaces, it will basically just take everything that is left, this therefore has to be used on the last parameter.
acceptEmpty - This is for when the argument can accept empty input, most of the time this won't be used.
acceptQuote - If you want a String for instance to be endless but not the last argument you can use this parameter to force the user to surround the argument with quotes if it is multiple words
nullDefault - This is used if you want an argument to be optional, if the argument was not provided it will be null (There are other ways to do optional arguments too)
description - A simple description/name of the parameter so that the user knowns what they are inputing


There is also a way to specify each argument manually, this way allows for custom arguments to be specified and optional arguments to be defined more in depth

public class CommandAvatar extends CommandImpl {

	public CommandAvatar() {
		super("avatar", ArgumentFactory.of(User.class).setDescription("user").setDefaultValue(event -> event.getAuthor()).build());
		
		super.setDescription("Get the avatar of a user");
	}
	
	public void onCommand(MessageReceivedEvent event, User user) {
		event.getChannel().sendMessage(new EmbedBuilder().setImage(user.getAvatarUrl()).build()).queue();
	}
}

In this example we use the ArgumentFactory to get a Builder of the type User, then we simply set the default argument to the author (so that if they do not define a user they will get their own avatar, how convenient!) and then build it.