use of data.Alliance in project KaellyBot by Kaysoro.
the class AllianceCommand method request.
@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
String pseudo = m.group(1).trim().toLowerCase();
String serverName = null;
if (Pattern.compile("\\s+-serv\\s+").matcher(pseudo).find()) {
String[] split = pseudo.split("\\s+-serv\\s+");
pseudo = split[0];
serverName = split[1];
}
StringBuilder url;
try {
url = new StringBuilder(Translator.getLabel(lg, "game.url")).append(Translator.getLabel(lg, "alliance.url")).append("?").append(forPseudo).append(URLEncoder.encode(pseudo, "UTF-8"));
} catch (UnsupportedEncodingException e) {
ExceptionManager.manageException(e, message, this, lg);
return;
}
if (serverName != null) {
ServerUtils.ServerQuery serverQuery = ServerUtils.getServerDofusFromName(serverName, lg);
if (serverQuery.hasSucceed()) {
url.append("&").append(forServer).append(serverQuery.getServer());
} else {
serverQuery.getExceptions().stream().forEach(e -> e.throwException(message, this, lg, serverQuery.getServersFound()));
return;
}
}
try {
Document doc = JSoupManager.getDocument(url.toString());
Elements elems = doc.getElementsByClass("ak-bg-odd");
elems.addAll(doc.getElementsByClass("ak-bg-even"));
if (!elems.isEmpty()) {
// on boucle jusqu'à temps de trouver la bonne alliance (ie la plus proche du nom donnée)
List<String> result = new ArrayList<>();
List<String> servers = new ArrayList<>();
for (Element element : elems) if (pseudo.equals(element.child(1).text().trim().toLowerCase())) {
result.add(element.child(1).select("a").attr("href"));
servers.add(element.child(2).text());
}
if (result.size() == 1) {
Alliance alliancePage = Alliance.getAlliance(Translator.getLabel(lg, "game.url") + result.get(0), lg);
message.getChannel().flatMap(chan -> chan.createEmbed(alliancePage.decorateEmbedObject(lg))).subscribe();
} else if (result.size() > 1)
tooMuchAlliances.throwException(message, this, lg, servers);
else
notFoundAlliance.throwException(message, this, lg);
} else
notFoundAlliance.throwException(message, this, lg);
} catch (IOException e) {
ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.ALLIANCEPAGE_INACCESSIBLE);
} catch (Exception e) {
ExceptionManager.manageException(e, message, this, lg);
}
}
Aggregations