|
|
|
@ -1,9 +1,12 @@
|
|
|
|
|
package net.pingex.dcf.util;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.ClassUtils;
|
|
|
|
|
|
|
|
|
|
import sx.blah.discord.api.IDiscordClient;
|
|
|
|
|
import sx.blah.discord.handle.obj.IUser;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Util class used to parse a String into primitive types.
|
|
|
|
@ -21,7 +24,7 @@ public class ArgumentParser
|
|
|
|
|
*/
|
|
|
|
|
public static Object parse(Class<?> target, String value) throws IllegalArgumentException
|
|
|
|
|
{
|
|
|
|
|
if(value == null || value.equals("null") ) return null;
|
|
|
|
|
if(value == null || value.equalsIgnoreCase("null")) return null;
|
|
|
|
|
if(Boolean.class == target || Boolean.TYPE == target) return Boolean.parseBoolean(value);
|
|
|
|
|
if(Byte.class == target || Byte.TYPE == target) return Byte.parseByte(value);
|
|
|
|
|
if(Short.class == target || Short.TYPE == target) return Short.parseShort(value);
|
|
|
|
@ -77,6 +80,43 @@ public class ArgumentParser
|
|
|
|
|
return outputList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Pattern usernamePattern = Pattern.compile("^.*#\\d{4}$");
|
|
|
|
|
private static Pattern idPattern = Pattern.compile("^\\d{18}$");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Binds an username or an UID to an actual {@code IUser}.
|
|
|
|
|
* The following username input formats are allowed:
|
|
|
|
|
* <ul>
|
|
|
|
|
* <li>18-digit UID: "123456789012345678"</li>
|
|
|
|
|
* <li>Username and 4-digit discriminator: "Username#1234"</li>
|
|
|
|
|
* </ul>
|
|
|
|
|
*
|
|
|
|
|
* Unknown formats will cause a {@link ParserException}.
|
|
|
|
|
*
|
|
|
|
|
* @param input String to parse
|
|
|
|
|
* @param client Discord instance to look for actual users
|
|
|
|
|
* @return A filled Optional when an user was found, an empty one otherwise.
|
|
|
|
|
* @throws ParserException Unknown format
|
|
|
|
|
*/
|
|
|
|
|
public static Optional<IUser> checkParseUsernameOrID(String input, IDiscordClient client) throws ParserException
|
|
|
|
|
{
|
|
|
|
|
IUser foundUser = null;
|
|
|
|
|
|
|
|
|
|
if(idPattern.matcher(input).matches()) foundUser = client.getUserByID(input); // UID
|
|
|
|
|
else if(usernamePattern.matcher(input).matches()) // username#1234
|
|
|
|
|
{
|
|
|
|
|
for(IUser iUser : client.getUsers()) // Greedy
|
|
|
|
|
if(input.equalsIgnoreCase(iUser.getName() + "#" + iUser.getDiscriminator()))
|
|
|
|
|
{
|
|
|
|
|
foundUser = iUser;
|
|
|
|
|
break; // Limit greediness
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else throw new ParserException("Invalid argument."); // ?
|
|
|
|
|
|
|
|
|
|
return Optional.ofNullable(foundUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Thrown whenever any parse operation failed.
|
|
|
|
|
*/
|
|
|
|
|