use of data.JobUser in project KaellyBot by Kaysoro.
the class JobCommand method request.
@Override
protected void request(MessageCreateEvent event, Message message, Matcher m, Language lg) {
String content = m.group(1).trim().replaceAll(",", "");
// Filter Initialisation
Optional<discord4j.core.object.entity.Guild> guild = message.getGuild().blockOptional();
Optional<Member> user = message.getAuthorAsMember().blockOptional();
if (guild.isPresent() && user.isPresent()) {
ServerDofus server = ServerUtils.getDofusServerFrom(Guild.getGuild(guild.get()), message.getChannel().block());
// Concerned user is the author?
if (Pattern.compile("^<@[!|&]?\\d+>").matcher(content).find()) {
content = content.replaceFirst("<@[!|&]?\\d+>", "").trim();
Optional<Snowflake> memberId = message.getUserMentionIds().stream().findFirst();
if (!memberId.isPresent()) {
BasicDiscordException.USER_NEEDED.throwException(message, this, lg);
return;
}
user = guild.get().getMemberById(memberId.get()).blockOptional();
}
// user data consultation
ServerUtils.ServerQuery serverQuery = ServerUtils.getServerDofusFromName(content, lg);
if (!serverQuery.getServersFound().isEmpty() && Pattern.compile("(.+)").matcher(content).matches() || content.isEmpty()) {
if (serverQuery.hasSucceed())
server = serverQuery.getServer();
else if (server == null) {
if (!content.isEmpty())
serverQuery.getExceptions().forEach(e -> e.throwException(message, this, lg, serverQuery.getServersFound()));
else
notFoundServer.throwException(message, this, lg);
return;
}
if (user.isPresent()) {
List<EmbedCreateSpec> embeds = JobUser.getJobsFromUser(user.get(), server, lg);
for (EmbedCreateSpec embed : embeds) message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
}
} else // Data recording
if ((m = Pattern.compile("-list|(-all|\\p{L}+(?:\\s+\\p{L}+)*)\\s+(\\d{1,3})(\\s+.+)?").matcher(content)).matches()) {
if (user.isPresent() && message.getAuthor().isPresent() && user.get().getId().equals(message.getAuthor().get().getId())) {
if (!m.group(0).equals("-list")) {
// Data Parsing and exceptions processing
Set<Job> jobs;
StringBuilder found = new StringBuilder();
StringBuilder notFound = new StringBuilder();
StringBuilder tooMuch = new StringBuilder();
if (!m.group(1).equals("-all")) {
jobs = new HashSet<>();
String[] proposals = m.group(1).split("\\s+");
for (String proposal : proposals) if (!proposal.trim().isEmpty()) {
List<Job> tmp = getJob(lg, proposal);
if (tmp.size() == 1) {
jobs.add(tmp.get(0));
found.append(tmp.get(0).getLabel(lg)).append(", ");
} else if (tmp.isEmpty())
notFound.append("*").append(proposal).append("*, ");
else
tooMuch.append("*").append(proposal).append("*, ");
}
} else
jobs = new HashSet<>(Arrays.asList(Job.values()));
// Check existing jobs
if (jobs.isEmpty()) {
message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "job.noone"))).subscribe();
return;
}
if (found.length() > 0)
found.setLength(found.length() - 2);
if (notFound.length() > 0)
notFound.setLength(notFound.length() - 2);
if (tooMuch.length() > 0)
tooMuch.setLength(tooMuch.length() - 2);
int level = Integer.parseInt(m.group(2));
if (m.group(3) != null) {
ServerUtils.ServerQuery query = ServerUtils.getServerDofusFromName(m.group(3), lg);
if (serverQuery.hasSucceed())
server = serverQuery.getServer();
else
serverQuery.getExceptions().forEach(e -> e.throwException(message, this, lg, query.getServersFound()));
} else if (server == null) {
notFoundServer.throwException(message, this, lg);
return;
}
for (Job job : jobs) if (JobUser.containsKeys(user.get().getId().asLong(), server, job))
JobUser.get(user.get().getId().asLong(), server, job).get(0).setLevel(level);
else
new JobUser(user.get().getId().asLong(), server, job, level).addToDatabase();
StringBuilder sb = new StringBuilder();
if (jobs.size() < Job.values().length)
sb.append(Translator.getLabel(lg, level > 0 ? "job.save" : "job.reset").replace("{jobs}", found.toString()));
else
sb.append(Translator.getLabel(lg, level > 0 ? "job.all_save" : "job.all_reset"));
if (notFound.length() > 0)
sb.append("\n").append(Translator.getLabel(lg, "job.not_found").replace("{jobs}", notFound.toString()));
if (tooMuch.length() > 0)
sb.append("\n").append(Translator.getLabel(lg, "job.too_much").replace("{jobs}", tooMuch.toString()));
final String CONTENT = sb.toString();
message.getChannel().flatMap(chan -> chan.createMessage(CONTENT)).subscribe();
} else {
String sb = Translator.getLabel(lg, "job.list") + "\n" + getJobsList(lg);
message.getChannel().flatMap(chan -> chan.createMessage(sb)).subscribe();
}
} else
badUse.throwException(message, this, lg);
} else if ((m = Pattern.compile("(?:>\\s*(\\d{1,3})\\s+)?(\\p{L}+(?:\\s+\\p{L}+)*)").matcher(content)).matches()) {
List<String> proposals = new LinkedList<>(Arrays.asList(m.group(2).split("\\s+")));
if (proposals.size() > 1) {
String potentialServer = proposals.get(proposals.size() - 1);
ServerUtils.ServerQuery query = ServerUtils.getServerDofusFromName(potentialServer, lg);
if (query.hasSucceed()) {
server = query.getServer();
proposals.remove(potentialServer);
} else {
query.getExceptions().forEach(e -> e.throwException(message, this, lg, query.getServersFound()));
return;
}
} else if (server == null) {
notFoundServer.throwException(message, this, lg);
return;
}
Set<Job> jobs = new HashSet<>();
for (String proposal : proposals) if (jobs.size() < MAX_JOB_DISPLAY) {
if (!proposal.trim().isEmpty()) {
List<Job> tmp = getJob(lg, proposal);
if (tmp.size() == 1)
jobs.add(tmp.get(0));
}
}
// Check if a job is found
if (jobs.isEmpty()) {
message.getChannel().flatMap(chan -> chan.createMessage(Translator.getLabel(lg, "job.noone"))).subscribe();
return;
}
int level = -1;
if (m.group(1) != null)
level = Integer.parseInt(m.group(1));
List<EmbedCreateSpec> embeds = JobUser.getJobsFromFilters(guild.get().getMembers().collectList().blockOptional().orElse(Collections.emptyList()), server, jobs, level, guild.get(), lg);
for (EmbedCreateSpec embed : embeds) message.getChannel().flatMap(chan -> chan.createEmbed(embed)).subscribe();
} else
badUse.throwException(message, this, lg);
}
}
Aggregations