IRole parser.

master
Pingex aka Raphaël 9 years ago
parent ee4e1e0c84
commit 5033de0355

@ -3,6 +3,7 @@ package net.pingex.dcf.util;
import org.apache.commons.lang3.ClassUtils;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IRole;
import sx.blah.discord.handle.obj.IUser;
import java.util.ArrayList;
import java.util.List;
@ -85,7 +86,7 @@ public class ArgumentParser
private static Pattern idPattern = Pattern.compile("^\\d{18}$");
/**
* Binds an username or an UID to an actual {@code IUser}.
* Binds an username or an UID to an actual {@link IUser}.
* The following username input formats are allowed:
* <ul>
* <li>18-digit UID: "123456789012345678"</li>
@ -122,7 +123,7 @@ public class ArgumentParser
* Binds a guild or an ID to an actual {@link IGuild}.
* The following guild input formats are allowed:
* <ul>
* <li>18-digit UID: "123456789012345678"</li>
* <li>18-digit GID: "123456789012345678"</li>
* <li>Guild name: "Discord Developers"</li>
* </ul>
*
@ -146,6 +147,34 @@ public class ArgumentParser
return Optional.ofNullable(foundGuild);
}
/**
* Binds a role or an ID to an actual {@link IRole}.
* The following role input formats are allowed:
* <ul>
* <li>18-digit RID: "123456789012345678"</li>
* <li>Role name: "Admin"</li>
* </ul>
*
* @param input String to parse
* @param guild Guild to look for roles
* @return A filled Optional when a role was found, an empty one otherwise.
*/
public static Optional<IRole> checkParseRoleOrID(String input, IGuild guild)
{
IRole foundRole = null;
if(idPattern.matcher(input).matches()) foundRole = guild.getRoleByID(input); // ID
else
for(IRole i : guild.getRoles()) // Name
if(input.equalsIgnoreCase(i.getName()))
{
foundRole = i;
break;
}
return Optional.ofNullable(foundRole);
}
/**
* Thrown whenever any parse operation failed.
*/