use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.
the class RandomCommand method request.
@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
Random r = new Random();
Matcher tmp;
if (m.group(1) == null || m.group(1).trim().isEmpty()) {
boolean value = r.nextBoolean();
message.getChannel().flatMap(chan -> chan.createMessage(value ? Translator.getLabel(lg, "random.request.1") : Translator.getLabel(lg, "random.request.2") + " !")).subscribe();
} else if (m.group(1).matches("\\s+-?\\d+")) {
try {
int limit = Integer.parseInt(m.group(1).trim());
if (limit > 0)
message.getChannel().flatMap(chan -> chan.createMessage(r.nextInt(limit) + " !")).subscribe();
else
message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "random.request.3"))).subscribe();
} catch (NumberFormatException e) {
message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "random.request.4") + " " + Integer.MAX_VALUE + ").")).subscribe();
}
} else if ((tmp = Pattern.compile("\\s+-dj(\\s+\\d{1,3})?(\\s+\\d{1,3})?").matcher(m.group(1))).matches()) {
int level = DEFAULT_LEVEL;
int tolerance = DEFAULT_TOLERANCE;
if (tmp.group(1) != null) {
level = Integer.parseInt(tmp.group(1).trim());
level = level - level % 10;
if (level > DEFAULT_LEVEL)
level = DEFAULT_LEVEL;
if (level < MIN_LEVEL)
level = MIN_LEVEL;
}
if (tmp.group(2) != null)
tolerance = Integer.parseInt(tmp.group(2).trim());
else if (tmp.group(1) == null)
tolerance = MAX_TOLERANCE;
final int finalLevel = level;
final int finalTolerance = tolerance;
List<Dungeon> djs = Arrays.stream(Dungeon.values()).filter(dj -> dj.filterByLevel(finalLevel, finalTolerance)).collect(Collectors.toList());
Dungeon selected = djs.get(r.nextInt(djs.size()));
if (selected.isEvent() && djs.size() > 1) {
Dungeon selected2 = selected;
while (selected == selected2 || selected2.isEvent()) selected2 = djs.get(r.nextInt(djs.size()));
final Dungeon SELECTED2 = selected2;
message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "random.request.6").replace("{dj}", selected.getLabel(lg)).replace("{dj2}", SELECTED2.getLabel(lg)).replace("{size}", String.valueOf(djs.size())))).subscribe();
} else
message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "random.request.5").replace("{dj}", selected.getLabel(lg)).replace("{size}", String.valueOf(djs.size())))).subscribe();
} else if (m.group(1).matches("\\s+-dj(\\s+\\d+)?(\\s+\\d+)?"))
badUse.throwException(message, this, lg);
else {
String value = m.group(1).trim();
String[] values = value.split("\\s+");
message.getChannel().flatMap(chan -> chan.createMessage(values[r.nextInt(values.length)] + " !")).subscribe();
}
}
use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.
the class WhoisCommand method request.
@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
String pseudo = m.group(2).trim().toLowerCase();
ServerDofus server = null;
StringBuilder url;
try {
url = new StringBuilder(Translator.getLabel(lg, "game.url")).append(Translator.getLabel(lg, "character.url")).append("?").append(forPseudo).append(URLEncoder.encode(pseudo, "UTF-8"));
} catch (UnsupportedEncodingException e) {
ExceptionManager.manageException(e, message, this, lg);
return;
}
if (m.group(3) != null) {
String serverName = m.group(3).trim().toLowerCase();
ServerUtils.ServerQuery serverQuery = ServerUtils.getServerDofusFromName(serverName, lg);
if (serverQuery.hasSucceed()) {
url.append("&").append(forServer).append(serverQuery.getServer().getId());
server = serverQuery.getServer();
} else {
serverQuery.getExceptions().forEach(e -> e.throwException(message, this, lg, serverQuery.getServersFound()));
return;
}
}
try {
Document doc = JSoupManager.getDocument(url.toString());
Elements elements = doc.getElementsByClass("ak-bg-odd");
elements.addAll(doc.getElementsByClass("ak-bg-even"));
if (!elements.isEmpty()) {
// loop until finding the nearest character
List<CharacterQuery> result = elements.stream().filter(e -> pseudo.equals(e.child(1).text().trim().toLowerCase())).map(e -> new CharacterQuery().withUrl(e.child(1).select("a").attr("href")).withServer(e.child(e.children().size() - 2).text())).collect(Collectors.toList());
// Ankama bug workaround
if (server != null && result.size() > 1) {
List<CharacterQuery> filteredResult = new ArrayList<>();
for (CharacterQuery query : result) try {
JSoupManager.getResponse(Translator.getLabel(lg, "game.url") + query.getUrl());
filteredResult.add(query);
} catch (IOException e) {
LOG.warn("Not distinct character for same server: " + query.getUrl());
}
result = filteredResult;
if (result.isEmpty()) {
BasicDiscordException.CHARACTERPAGE_INACCESSIBLE.throwException(message, this, lg);
return;
}
}
if (result.size() == 1) {
Connection.Response response = JSoupManager.getResponse(Translator.getLabel(lg, "game.url") + result.get(0).getUrl());
final List<CharacterQuery> RESULT = result;
if (!response.url().getPath().endsWith(Translator.getLabel(lg, "whois.request"))) {
if (m.group(1) == null) {
try {
EmbedCreateSpec embed = Character.getCharacter(Translator.getLabel(lg, "game.url") + RESULT.get(0).getUrl(), lg).decorateEmbedObject(lg);
message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
} catch (IOException e) {
ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.CHARACTERPAGE_INACCESSIBLE);
}
} else {
try {
EmbedCreateSpec embed = Character.getCharacterStuff(Translator.getLabel(lg, "game.url") + RESULT.get(0).getUrl() + Translator.getLabel(lg, "character.stuff.url"), lg).decorateMoreEmbedObject(lg);
message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
} catch (IOException e) {
ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.CHARACTERPAGE_INACCESSIBLE);
}
}
} else
BasicDiscordException.CHARACTER_TOO_OLD.throwException(message, this, lg);
} else if (result.size() > 1)
tooMuchCharacters.throwException(message, this, lg, result.stream().map(CharacterQuery::getServer).distinct().collect(Collectors.toList()));
else
notFoundCharacter.throwException(message, this, lg);
} else
notFoundCharacter.throwException(message, this, lg);
} catch (IOException e) {
ExceptionManager.manageIOException(e, message, this, lg, BasicDiscordException.CHARACTERPAGE_INACCESSIBLE);
} catch (Exception e) {
ExceptionManager.manageException(e, message, this, lg);
}
}
use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.
the class SendNudeCommand method request.
@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
if (message.getChannel().blockOptional().map(chan -> chan instanceof PrivateChannel).orElse(false) || message.getChannel().blockOptional().map(chan -> ((TextChannel) chan).isNsfw()).orElse(false)) {
int position = RANDOM.nextInt(Nude.values().length);
Nude nude = Nude.values()[position];
message.getChannel().flatMap(chan -> chan.createEmbed(spec -> spec.setTitle(Translator.getLabel(lg, "sendnude.title")).setDescription(Translator.getLabel(lg, "sendnude.author").replace("{author}", nude.getAuthor()).replace("{url}", nude.getUrl())).setColor(discord4j.rest.util.Color.PINK).setFooter(Translator.getLabel(lg, "sendnude.footer").replace("{position}", String.valueOf(position + 1)).replace("{number}", String.valueOf(Nude.values().length)), null).setImage(nude.getImage()))).subscribe();
} else
// Exception NSFW
BasicDiscordException.NO_NSFW_CHANNEL.throwException(message, this, lg);
}
use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.
the class AboutCommand method request.
@Override
public void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
Optional<ApplicationInfo> appInfo = message.getClient().getApplicationInfo().blockOptional();
if (appInfo.isPresent()) {
message.getChannel().flatMap(chan -> chan.createEmbed(spec -> {
spec.setTitle(Translator.getLabel(lg, "about.title").replace("{name}", Constants.name).replace("{version}", Constants.version)).setImage(Constants.changelog).setThumbnail(appInfo.get().getIconUrl(Image.Format.PNG).orElse(null)).addField(Translator.getLabel(lg, "about.invite.title"), Translator.getLabel(lg, "about.invite.desc").replace("{name}", Constants.name).replace("{invite}", Constants.invite), false).addField(Translator.getLabel(lg, "about.support.title"), Translator.getLabel(lg, "about.support.desc").replace("{name}", Constants.name).replace("{discordInvite}", Constants.discordInvite), false).addField(Translator.getLabel(lg, "about.twitter.title"), Translator.getLabel(lg, "about.twitter.desc").replace("{name}", Constants.name).replace("{twitter}", Constants.twitterAccount), false).addField(Translator.getLabel(lg, "about.opensource.title"), Translator.getLabel(lg, "about.opensource.desc").replace("{git}", Constants.git), false).addField(Translator.getLabel(lg, "about.free.title"), Translator.getLabel(lg, "about.free.desc").replace("{paypal}", Constants.paypal), false).addField(Translator.getLabel(lg, "about.privacy.title"), Translator.getLabel(lg, "about.privacy.desc").replace("{paypal}", Constants.paypal), false).addField(Translator.getLabel(lg, "about.graphist.title"), Translator.getLabel(lg, "about.graphist.desc").replace("{graphist}", Graphist.ELYCANN.toMarkdown()), false);
StringBuilder st = new StringBuilder();
for (Donator donator : Donator.values()) st.append(donator.getName()).append(", ");
st.setLength(st.length() - 2);
spec.addField(Translator.getLabel(lg, "about.donators.title"), st.toString() + ".", false);
})).subscribe();
}
}
use of ai.elimu.model.v2.enums.Language in project KaellyBot by Kaysoro.
the class TooMuchDiscordException method throwException.
@Override
public void throwException(Message message, Command command, Language lg, Object... arguments) {
AnkamaBug bug = null;
String gender = Translator.getLabel(lg, "exception.object." + objectKey + ".gender");
StringBuilder st = new StringBuilder(Translator.getLabel(lg, "exception.toomuch.toomuch." + gender)).append(" ").append(Translator.getLabel(lg, "exception.object." + objectKey + ".plural")).append(" ").append(Translator.getLabel(lg, "exception.toomuch.found." + gender));
if (arguments.length > 0) {
List<Object> objects = (List<Object>) arguments[0];
long similarOcc = objects.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting())).values().stream().max(Long::compareTo).orElse(0L);
if (similarOcc > 1) {
if (command instanceof WhoisCommand)
bug = AnkamaBug.GHOST_CHARACTER;
else if (command instanceof GuildCommand)
bug = AnkamaBug.GHOST_GUILD;
else if (command instanceof AllianceCommand)
bug = AnkamaBug.GHOST_ALLY;
}
if (objects.size() <= ITEM_LIMIT) {
st.append(": ");
for (Object object : objects) if (isTranslatable)
st.append(Translator.getLabel(lg, object.toString())).append(", ");
else
st.append(object.toString()).append(", ");
st.delete(st.length() - 2, st.length()).append(".");
} else
st.append(". ").append(Translator.getLabel(lg, "exception.toomuch.items"));
} else {
if (st.substring(st.length() - 1, st.length()).matches("\\s+"))
st.delete(st.length() - 1, st.length());
st.append(".");
}
if (bug != null) {
final AnkamaBug BUG = bug;
message.getChannel().flatMap(channel -> channel.createEmbed(BUG.decorateEmbed(st.toString(), lg))).subscribe();
} else
message.getChannel().flatMap(channel -> channel.createMessage(st.toString())).subscribe();
}
Aggregations