Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from BtoBastian/development
Browse files Browse the repository at this point in the history
Merge development branch
  • Loading branch information
Bastian committed Apr 5, 2016
2 parents 994e1fb + 35ca42b commit 7da34c2
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 16 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ A **s**imple **D**iscord **c**ommand **f**ramework **for** **J**ava, supporting
<dependency>
<groupId>de.btobastian.sdcf4j</groupId>
<artifactId>sdcf4j-core</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
</dependency>
<!-- The module for your prefered lib-->
<dependency>
<groupId>de.btobastian.sdcf4j</groupId>
<!-- Possible artifact ids: sdcf4j-javacord, sdcf4j-jda, sdcf4j-discord4j -->
<artifactId>sdcf4j-javacord</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
</dependency>
```

Expand All @@ -28,7 +28,7 @@ A **s**imple **D**iscord **c**ommand **f**ramework **for** **J**ava, supporting
* [Javacord server](https://discord.gg/0qJ2jjyneLEgG7y3)
* [DiscordAPI #java_javacord channel](https://discord.gg/0SBTUU1wZTVXVKEo)

You can find me on one of these servers/channels. Fell free to contact me if you need help. :)
You can find me on one of these servers/channels. Feel free to contact me if you need help. :)

#Download
For those of you how don't use maven: [Jenkins](http://ci.ketrwu.de/job/sdcf4j/lastSuccessfulBuild/)
Expand Down Expand Up @@ -93,4 +93,4 @@ CommandHandler cmdHandler = new Discord4JHandler(client);

// register the command
cmdHandler.registerCommand(new PingCommand());
```
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>de.btobastian.sdcf4j</groupId>
<artifactId>sdcf4j</artifactId>
<packaging>pom</packaging>
<version>1.0.1</version>
<version>1.0.2</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion sdcf4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sdcf4j</artifactId>
<groupId>de.btobastian.sdcf4j</groupId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

Expand All @@ -32,6 +33,8 @@ public abstract class CommandHandler {
protected final List<SimpleCommand> commandList = new ArrayList<>();
private final HashMap<String, List<String>> permissions = new HashMap<>();

protected String defaultPrefix = "";

/**
* Registers an executor.
*
Expand All @@ -49,7 +52,7 @@ public void registerCommand(CommandExecutor executor) {
SimpleCommand command = new SimpleCommand(annotation, method, executor);
for (String alias : annotation.aliases()) {
// add command to map. It's faster to access it from the map than iterating to the whole list
commands.put(alias.toLowerCase(), command);
commands.put(defaultPrefix + alias.toLowerCase().replace(" ", ""), command);
}
// we need a list, too, because a HashMap is not ordered.
commandList.add(command);
Expand Down Expand Up @@ -96,6 +99,39 @@ public boolean hasPermission(String userId, String permission) {
return false;
}

/**
* Gets a list with all commands in the order they were registered.
* This is useful for automatic help commands.
*
* @return A list with all commands the the order they were registered.
*/
public List<SimpleCommand> getCommands() {
return Collections.unmodifiableList(commandList);
}

/**
* Sets the default command prefix.
* Changing the default prefix after registering a command has no effect!
*
* @param defaultPrefix The default command prefix.
*/
public void setDefaultPrefix(String defaultPrefix) {
if (defaultPrefix == null) {
this.defaultPrefix = "";
} else {
this.defaultPrefix = defaultPrefix.replace(" ", "");
}
}

/**
* Gets the default command prefix.
*
* @return The default command prefix.
*/
public String getDefaultPrefix() {
return defaultPrefix;
}

/**
* Checks if you are allowed to do something with the given permission.
*
Expand Down
2 changes: 1 addition & 1 deletion sdcf4j-discord4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sdcf4j</artifactId>
<groupId>de.btobastian.sdcf4j</groupId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,18 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
String[] args = Arrays.copyOfRange(splitMessage, 1, splitMessage.length);
Class<?>[] parameterTypes = command.getMethod().getParameterTypes();
final Object[] parameters = new Object[parameterTypes.length];
int stringCounter = 0;
for (int i = 0; i < parameterTypes.length; i++) { // check all parameters
Class<?> type = parameterTypes[i];
if (type == String.class) {
parameters[i] = splitMessage[0];
if (stringCounter++ == 0) {
parameters[i] = splitMessage[0]; // the first split is the command
} else {
if (args.length + 2 > stringCounter) {
// the first string parameter is the command, the other ones are the arguments
parameters[i] = args[stringCounter - 2];
}
}
} else if (type == String[].class) {
parameters[i] = args;
} else if (type == IMessage.class) {
Expand All @@ -184,6 +192,8 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
parameters[i] = event.getMessage().getAuthor();
} else if (type == IGuild.class) {
parameters[i] = event.getMessage().getChannel().getGuild();
} else if (type == Object[].class) {
parameters[i] = getObjectsFromString(event.getClient(), args);
} else {
// unknown type
parameters[i] = null;
Expand All @@ -192,4 +202,50 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
return parameters;
}

/**
* Tries to get objects (like channel, user, integer) from the given strings.
*
* @param client The client.
* @param args The string array.
* @return An object array.
*/
private Object[] getObjectsFromString(IDiscordClient client, String[] args) {
Object[] objects = new Object[args.length];
for (int i = 0; i < args.length; i++) {
objects[i] = getObjectFromString(client, args[i]);
}
return objects;
}

/**
* Tries to get an object (like channel, user, integer) from the given string.
*
* @param client The client.
* @param arg The string.
* @return The object.
*/
private Object getObjectFromString(IDiscordClient client, String arg) {
try {
// test int
return Integer.valueOf(arg);
} catch (NumberFormatException e) {}
// test user
if (arg.matches("<@([0-9]*)>")) {
String id = arg.substring(2, arg.length() - 1);
IUser user = client.getUserByID(id);
if (user != null) {
return user;
}
}
// test channel
if (arg.matches("<#([0-9]*)>")) {
String id = arg.substring(2, arg.length() - 1);
IChannel channel = client.getChannelByID(id);
if (channel != null) {
return channel;
}
}
return arg;
}

}
2 changes: 1 addition & 1 deletion sdcf4j-javacord/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sdcf4j</artifactId>
<groupId>de.btobastian.sdcf4j</groupId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import de.btobastian.sdcf4j.Sdcf4jMessage;
import org.slf4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

Expand Down Expand Up @@ -148,8 +147,8 @@ private void invokeMethod(SimpleCommand command, Message message, Object[] param
Object reply = null;
try {
reply = method.invoke(command.getExecutor(), parameters);
} catch (IllegalAccessException | InvocationTargetException e) {
logger.warn("Cannot invoke method {}!", method.getName(), e);
} catch (Exception e) {
logger.warn("An error occurred while invoking method {}!", method.getName(), e);
}
if (reply != null) {
message.reply(String.valueOf(reply));
Expand All @@ -169,10 +168,18 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
String[] args = Arrays.copyOfRange(splitMessage, 1, splitMessage.length);
Class<?>[] parameterTypes = command.getMethod().getParameterTypes();
final Object[] parameters = new Object[parameterTypes.length];
int stringCounter = 0;
for (int i = 0; i < parameterTypes.length; i++) { // check all parameters
Class<?> type = parameterTypes[i];
if (type == String.class) {
parameters[i] = splitMessage[0]; // the first split is the command
if (stringCounter++ == 0) {
parameters[i] = splitMessage[0]; // the first split is the command
} else {
if (args.length + 2 > stringCounter) {
// the first string parameter is the command, the other ones are the arguments
parameters[i] = args[stringCounter - 2];
}
}
} else if (type == String[].class) {
parameters[i] = args;
} else if (type == Message.class) {
Expand All @@ -189,6 +196,8 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
if (message.getChannelReceiver() != null) {
parameters[i] = message.getChannelReceiver().getServer();
}
} else if (type == Object[].class) {
parameters[i] = getObjectsFromString(api, args);
} else {
// unknown type
parameters[i] = null;
Expand All @@ -197,4 +206,50 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
return parameters;
}

/**
* Tries to get objects (like channel, user, integer) from the given strings.
*
* @param api The api.
* @param args The string array.
* @return An object array.
*/
private Object[] getObjectsFromString(DiscordAPI api, String[] args) {
Object[] objects = new Object[args.length];
for (int i = 0; i < args.length; i++) {
objects[i] = getObjectFromString(api, args[i]);
}
return objects;
}

/**
* Tries to get an object (like channel, user, integer) from the given string.
*
* @param api The api.
* @param arg The string.
* @return The object.
*/
private Object getObjectFromString(DiscordAPI api, String arg) {
try {
// test int
return Integer.valueOf(arg);
} catch (NumberFormatException e) {}
// test user
if (arg.matches("<@([0-9]*)>")) {
String id = arg.substring(2, arg.length() - 1);
User user = api.getCachedUserById(id);
if (user != null) {
return user;
}
}
// test channel
if (arg.matches("<#([0-9]*)>")) {
String id = arg.substring(2, arg.length() - 1);
Channel channel = api.getChannelById(id);
if (channel != null) {
return channel;
}
}
return arg;
}

}
2 changes: 1 addition & 1 deletion sdcf4j-jda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sdcf4j</artifactId>
<groupId>de.btobastian.sdcf4j</groupId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>
<packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion>
Expand Down
Loading

0 comments on commit 7da34c2

Please sign in to comment.