use of discord4j.core.spec.EmbedCreateSpec 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 discord4j.core.spec.EmbedCreateSpec in project S-argo by Expugn.
the class Profile method bannerInfoMenu.
/**
* Displays if the user is missing characters or weapons and if they do happen
* to have the characters or weapons it will tell them that they obtained them
* and for weapons it will tell them how much they have obtained of that weapon.
*
* @param bannerIDString The banner ID in string form.
*/
private void bannerInfoMenu(String bannerIDString) {
int bannerID = Integer.parseInt(bannerIDString) - 1;
Consumer<EmbedCreateSpec> ecsTemplate;
// OPEN BANNERS FILE
List<Banner> banners = BannerParser.getBanners();
if (bannerID < banners.size() && bannerID >= 0) {
Banner requestedBanner = banners.get(bannerID);
boolean characterFound = false;
int characterCounter = 0;
List<Character> obtainedCharacters = new ArrayList<>();
List<Character> unobtainedCharacters = new ArrayList<>();
for (Character c : requestedBanner.getCharacters()) {
characterCounter++;
// TRY AND FIND CHARACTER IN USER BOX
for (Character oC : user.getCharacterBox()) {
if (c.getPrefix().equals(oC.getPrefix()) && c.getName().equals(oC.getName()) && (c.getRarity() == oC.getRarity())) {
// CHARACTER IS SAME
characterFound = true;
break;
}
}
// ADD CHARACTER TO LIST
if (!characterFound) {
unobtainedCharacters.add(c);
} else {
obtainedCharacters.add(c);
}
characterFound = false;
}
String obtainedList = "";
int obtainedCounter = 0;
if (obtainedCharacters.size() > 0) {
for (Character c : obtainedCharacters) {
obtainedList += c.toString() + "\n";
obtainedCounter++;
}
}
String noHaveList = "";
int noHaveCounter = 0;
if (unobtainedCharacters.size() > 0) {
for (Character c : unobtainedCharacters) {
noHaveList += c.toString() + "\n";
noHaveCounter++;
}
}
/* WEAPON STATS */
boolean weaponFound = false;
List<Weapon> obtainedWeapons = new ArrayList<>();
List<Weapon> unobtainedWeapons = new ArrayList<>();
for (Weapon w : requestedBanner.getWeapons()) {
/* TRY AND FIND WEAPON IN USER BOX */
for (Weapon oW : user.getWeaponBox()) {
if (w.getName().equals(oW.getName())) {
/* WEAPON IS SAME */
w.setCount(oW.getCount());
weaponFound = true;
break;
}
}
/* ADD WEAPON TO LIST */
if (!weaponFound) {
unobtainedWeapons.add(w);
} else {
obtainedWeapons.add(w);
}
weaponFound = false;
}
String obtainedList_wep = "";
int obtainedCounter_wep = 0;
if (obtainedWeapons.size() > 0) {
for (Weapon w : obtainedWeapons) {
obtainedList_wep += w.toStringWithCount() + "\n";
obtainedCounter_wep++;
}
}
String noHaveList_wep = "";
int noHaveCounter_wep = 0;
if (unobtainedWeapons.size() > 0) {
for (Weapon w : unobtainedWeapons) {
noHaveList_wep += w.toString() + "\n";
noHaveCounter_wep++;
}
}
int characterCounter_final = characterCounter;
int obtainedCounter_final = obtainedCounter;
String obtainedList_final = obtainedList;
int noHaveCounter_final = noHaveCounter;
String noHaveList_final = noHaveList;
int obtainedCounter_wep_final = obtainedCounter_wep;
String obtainedList_wep_final = obtainedList_wep;
int noHaveCounter_wep_final = noHaveCounter_wep;
String noHaveList_wep_final = noHaveList_wep;
ecsTemplate = s -> {
s.setAuthor(userName + "'s Profile", "", member.getAvatarUrl());
s.setColor(new Color(255, 86, 91));
s.setThumbnail(new GitHubImage("images/System/Profile_Icon.png").getURL());
s.addField(requestedBanner.getBannerName(), characterCounter_final + " Characters Available", false);
if (obtainedCharacters.size() > 0)
s.addField(obtainedCounter_final + " Characters Obtained", obtainedList_final, false);
if (unobtainedCharacters.size() > 0)
s.addField(noHaveCounter_final + " Characters Missing", noHaveList_final, false);
if (obtainedWeapons.size() > 0)
s.addField(obtainedCounter_wep_final + " Weapons Obtained", obtainedList_wep_final, false);
if (unobtainedWeapons.size() > 0)
s.addField(noHaveCounter_wep_final + " Weapons Missing", noHaveList_wep_final, false);
};
} else {
Sargo.replyToMessage_Warning(CHANNEL, "UNKNOWN BANNER ID", "Use '" + CommandManager.getCommandPrefix() + "**scout**' for a list of banners.");
return;
}
Sargo.sendEmbed(CHANNEL, ecsTemplate);
}
use of discord4j.core.spec.EmbedCreateSpec in project S-argo by Expugn.
the class Sargo method sendEmbed.
public static Message sendEmbed(TextChannel tc, Consumer<EmbedCreateSpec> ecsTemplate, File image) {
try {
InputStream is = new FileInputStream(image);
Message sentMessage = tc.createMessage(s -> s.setEmbed(ecsTemplate.andThen(es -> es.setImage("attachment://" + image.getName()))).addFile(image.getName(), is)).block();
is.close();
return sentMessage;
} catch (FileNotFoundException e) {
return tc.createMessage(ms -> ms.setEmbed(ecsTemplate.andThen(es -> es.addField("MISSING SCOUT RESULT", "Scout result image is missing.", true)))).block();
} catch (IOException e) {
// IGNORED
}
return null;
}
use of discord4j.core.spec.EmbedCreateSpec in project KaellyBot by Kaysoro.
the class Almanax method decorateGroupedObject.
public static EmbedCreateSpec decorateGroupedObject(Language lg, Date day, int occurrence) throws IOException {
Date firstDate = DateUtils.addDays(day, 1);
Date lastDate = DateUtils.addDays(day, occurrence);
String title = Translator.getLabel(lg, "almanax.embed.title.1") + " " + discordToBot.format(firstDate) + (occurrence > 1 ? " " + Translator.getLabel(lg, "almanax.embed.title.2") + " " + discordToBot.format(lastDate) : "");
EmbedCreateSpec.Builder builder = EmbedCreateSpec.builder().title(title);
for (int i = 1; i <= occurrence; i++) {
firstDate = DateUtils.addDays(new Date(), i);
Almanax almanax = Almanax.get(lg, firstDate);
builder.addField(discordToBot.format(firstDate), Translator.getLabel(lg, "almanax.embed.bonus") + " " + almanax.getBonus() + "\n" + Translator.getLabel(lg, "almanax.embed.offrande") + " " + almanax.getOffrande(), true);
}
return builder.build();
}
Aggregations