use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.
the class CommandsRoute method handle.
@Override
public Object handle(Request request, Response response) throws Exception {
List<SlashCommand> commands = Bean.getInstance().isDebug() ? Bean.getInstance().getInteractionCommandHandler().getRegisteredGuildCommands().get(DEV_GUILD_ID).stream().filter(cmd -> cmd instanceof SlashCommand).map(cmd -> (SlashCommand) cmd).toList() : Bean.getInstance().getInteractionCommandHandler().getRegisteredSlashCommands();
DataArray commandArray = DataArray.empty();
for (SlashCommand command : commands) {
DataObject commandObject = DataObject.empty();
SlashCommandData slashCommandData = command.getData();
DataArray options = DataArray.empty();
DataArray subCommands = DataArray.empty();
for (OptionData option : slashCommandData.getOptions()) {
DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
options.add(optionObject);
}
for (SubcommandData subcommandData : slashCommandData.getSubcommands()) {
DataObject subCommandObject = DataObject.empty();
DataArray subCommandOptions = DataArray.empty();
for (OptionData option : subcommandData.getOptions()) {
DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
subCommandOptions.add(optionObject);
}
subCommandObject.put("name", subcommandData.getName()).put("description", subcommandData.getDescription());
subCommandObject.put("options", subCommandOptions);
subCommands.add(subCommandObject);
}
commandObject.put("name", slashCommandData.getName()).put("description", slashCommandData.getDescription()).put("options", options).put("sub_commands", subCommands);
commandArray.add(commandObject);
}
return commandArray.toString();
}
use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.
the class MEE6Queue method makeCall.
private void makeCall(MEE6Request request) {
currentRequestGuildId = request.getGuildId();
try {
URI uri = new URIBuilder().setScheme("https").setHost("mee6.xyz").setPath("/api/plugins/levels/leaderboard/" + request.getGuildId()).addParameter("page", String.valueOf(request.getPage())).build();
Request httpRequest = new Request.Builder().url(uri.toURL()).get().build();
Call call = client.newCall(httpRequest);
Response response = call.execute();
if (response.code() == 429) {
String retryAfter = response.header("retry-after");
if (retryAfter != null)
LOGGER.warn("Encountered MEE6 Cloudflare Rate-limit! Retrying after {} seconds", retryAfter);
else
LOGGER.warn("Encountered MEE6 Cloudflare Rate-limit without \"retry-after\" header! Retrying after 1 hour");
isRateLimit = true;
int retryAfterMillis = retryAfter == null ? 60 * 60 * 1000 : Integer.parseInt(retryAfter) * 1000;
this.rateLimitExpiry = System.currentTimeMillis() + retryAfterMillis;
queue.offer(request);
response.close();
return;
}
if (response.code() == 404) {
Guild guild = Bean.getInstance().getShardManager().getGuildById(request.getGuildId());
if (guild != null)
Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We tried to migrate MEE6 experience for your guild **" + guild.getName() + "**, but sadly we could not find anything!\n\nAdding MEE6 to your server again often fixes this issue!"));
response.close();
currentRequestGuildId = 0L;
return;
}
if (response.code() == 401) {
Guild guild = Bean.getInstance().getShardManager().getGuildById(request.getGuildId());
if (guild != null)
Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We tried to migrate MEE6 experience for your guild **" + guild.getName() + "**, but your servers MEE6 leaderboard is [set to private](https://mee6.xyz/dashboard/" + guild.getIdLong() + "/leaderboard)!\n\nPlease set it to public and try again!"));
response.close();
currentRequestGuildId = 0L;
return;
}
if (String.valueOf(response.code()).charAt(0) == '5') {
Guild guild = Bean.getInstance().getShardManager().getGuildById(request.getGuildId());
if (guild != null)
Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We tried to migrate MEE6 experience for your guild **" + guild.getName() + "**, but the MEE6 server appears to be having issues!\n\nPlease try again later."));
response.close();
currentRequestGuildId = 0L;
return;
}
if (!response.isSuccessful()) {
LOGGER.error("MEE6 returned unhandled error {}\n{}", response.code(), response.body().string());
response.close();
currentRequestGuildId = 0L;
return;
}
DataObject object = DataObject.fromJson(response.body().string());
response.close();
DataArray playersArray = object.getArray("players");
int entries = playersArray.length();
try (Connection connection = Database.getConnectionFromPool()) {
playersArray.stream(DataArray::getObject).map(MEE6Player::fromData).forEach(player -> updateXP(connection, player, request.getGuildId()));
} catch (SQLException e) {
LOGGER.error("SQLException occurred!", e);
}
if (entries == 100) {
addRequest(new MEE6Request(request.getGuildId(), request.getAuthorId()).setPage(request.getPage() + 1));
currentRequestGuildId = 0L;
return;
}
currentRequestGuildId = 0L;
int entriesTotal = ((request.getPage()) * 100) + entries;
LOGGER.debug("Finished transferring xp for guild {}! Migrated {}users.", request.getGuildId(), entriesTotal);
Guild guild = Bean.getInstance().getShardManager().getGuildById(request.getGuildId());
if (guild != null && entriesTotal > 1)
Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We'd like you to know that we have finished migrating MEE6 xp for all users on your guild **" + guild.getName() + "**! (" + entriesTotal + " Users)"));
else if (guild != null && entriesTotal == 0)
Util.sendDM(request.getAuthorId(), EmbedUtil.defaultEmbed("Hey! We tried to migrate MEE6 xp for all users on your guild **" + guild.getName() + "**, but we couldn't find any!"));
currentRequestGuildId = 0L;
} catch (URISyntaxException | IOException exception) {
LOGGER.error("Error occurred in MEE6Queue!", exception);
}
}
Aggregations