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

Commit

Permalink
Added Object-array and more Strings as parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian committed Apr 1, 2016
1 parent 570fee8 commit 705e572
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 6 deletions.
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;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,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 == MessageReceivedEvent.class) {
Expand All @@ -186,6 +194,8 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
parameters[i] = event.getGuild();
} else if (type == Integer.class || type == int.class) {
parameters[i] = event.getResponseNumber();
} else if (type == Object[].class) {
parameters[i] = getObjectsFromString(event.getJDA(), args);
} else {
// unknown type
parameters[i] = null;
Expand All @@ -194,4 +204,50 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
return parameters;
}

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

/**
* Tries to get an object (like channel, user, integer) from the given string.
*
* @param jda The jda object.
* @param arg The string.
* @return The object.
*/
private Object getObjectFromString(JDA jda, 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 = jda.getUserById(id);
if (user != null) {
return user;
}
}
// test channel
if (arg.matches("<#([0-9]*)>")) {
String id = arg.substring(2, arg.length() - 1);
Channel channel = jda.getTextChannelById(id);
if (channel != null) {
return channel;
}
}
return arg;
}

}

0 comments on commit 705e572

Please sign in to comment.