use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.
the class GuildData method addModeratorRoles.
@CheckReturnValue
public GuildData addModeratorRoles(Role... roles) {
Checks.notEmpty(roles, "Roles");
List<Role> modRoles = getModeratorRoles();
modRoles.addAll(Arrays.asList(roles));
DataArray array = DataArray.empty();
modRoles.stream().map(ISnowflake::getIdLong).forEach(array::add);
dataObject.put("moderator_roles", array);
return this;
}
use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.
the class GuildData method removeModeratorRoles.
@CheckReturnValue
public GuildData removeModeratorRoles(Role... roles) {
Checks.notEmpty(roles, "Roles");
List<Role> modRoles = getModeratorRoles();
Arrays.asList(roles).forEach(modRoles::remove);
DataArray array = DataArray.empty();
modRoles.stream().map(ISnowflake::getIdLong).forEach(array::add);
dataObject.put("moderator_roles", modRoles);
return this;
}
use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.
the class GuildData method removeRoleReward.
@CheckReturnValue
public GuildData removeRoleReward(int level) {
if (!hasRoleReward(level))
return this;
List<RoleReward> currentRewards = new ArrayList<>(getRoleRewards());
currentRewards.removeIf(reward -> reward.getLevel() == level);
DataArray array = DataArray.empty();
for (RoleReward reward : currentRewards) {
array.add(DataObject.empty().put("persistant", reward.isPersistant()).put("level", reward.getLevel()).put("role_id", reward.getRoleId()).put("remove_on_next_reward", reward.doesRemoveOnNextReward()));
}
dataObject.put("role_rewards", array);
return this;
}
use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.
the class GuildPageRoute method handle.
@Override
public Object handle(Request request, Response response) throws Exception {
String authHeader = request.headers("authorization");
if (authHeader == null || !authHeader.startsWith("Token ")) {
response.status(401);
return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
}
String guildIdString = request.headers("guild_id");
if (guildIdString == null) {
response.status(400);
return DataObject.empty().put("code", 400).put("message", "Missing guild id!").toString();
}
String token = authHeader.substring(7);
byte[] tokenBytes = token.getBytes(StandardCharsets.UTF_8);
Authenticator authenticator = Bean.getInstance().getAuthenticator();
if (!authenticator.isAuthenticated(tokenBytes)) {
response.status(401);
return DataObject.empty().put("code", 401).put("message", "Invalid token (Try logging out and in again)").toString();
}
if (authenticator.isAccessTokenExpired(tokenBytes))
authenticator.refreshAccessToken(tokenBytes);
DiscordCredentials credentials = authenticator.getCredentials(tokenBytes);
String accessToken = credentials.getAccessToken();
DataObject guilds = WebServer.retrieveGuilds(accessToken);
if (guilds.isNull("guilds")) {
DataObject object = DataObject.empty();
object.put("http_code", guilds.getInt("http_code"));
if (!guilds.isNull("code"))
object.put("code", guilds.getInt("code"));
if (!guilds.isNull("message"))
object.put("message", guilds.getString("message"));
return object.toString();
}
ShardManager shardManager = Bean.getInstance().getShardManager();
List<Guild> mutualGuilds = guilds.getArray("guilds").stream(DataArray::getObject).filter(obj -> shardManager.getGuildById(obj.getLong("id")) != null).filter(obj -> Permission.getPermissions(obj.getLong("permissions")).contains(Permission.ADMINISTRATOR) || obj.getBoolean("owner")).map(obj -> shardManager.getGuildById(obj.getLong("id"))).collect(Collectors.toList());
long guildId = Long.parseUnsignedLong(guildIdString);
Guild guild = mutualGuilds.stream().map(ISnowflake::getIdLong).filter(id -> id.equals(guildId)).map(shardManager::getGuildById).findFirst().orElse(null);
if (guild == null) {
response.status(401);
return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
}
GuildData guildData = GuildManager.getGuildData(guild);
DataObject data = guildData.toData();
DataObject returnObject = DataObject.empty().put("guild", ObjectBuilder.serializeJDAGuild(guild)).put("http_code", guilds.getInt("http_code")).put("data", data);
return returnObject.toString();
}
use of net.dv8tion.jda.api.utils.data.DataArray in project Bean by Xirado.
the class LeaderboardRoute method handle.
@Override
public Object handle(Request request, Response response) throws Exception {
String guildId = request.params("guild");
if (!Helpers.isNumeric(guildId)) {
response.status(400);
return DataObject.empty().put("code", 400).put("message", "Invalid guild id!").toString();
}
long guildIdLong = Long.parseLong(guildId);
int totalAmount = RankingSystem.getDataCount(guildIdLong);
if (totalAmount == 0) {
response.status(404);
return DataObject.empty().put("code", 404).put("message", "The requested data could not be found!").toString();
}
Guild guild = Bean.getInstance().getShardManager().getGuildById(guildIdLong);
if (guild == null) {
response.status(404);
return DataObject.empty().put("code", 404).put("message", "I am no longer in this guild!").toString();
}
DataObject data = DataObject.empty().put("name", guild.getName()).put("icon", guild.getIconUrl());
String pageHeader = request.queryParams("page");
int availablePages = (int) Math.ceil((double) totalAmount / 100);
int page = 1;
if (Helpers.isNumeric(pageHeader)) {
int pageHeaderParsed = Integer.parseInt(pageHeader);
if (pageHeaderParsed > 1)
page = pageHeaderParsed;
}
DataArray result = RankingSystem.getLeaderboard(guildIdLong, page, 100);
DataObject obj = DataObject.empty().put("guild", data).put("users", result);
if (availablePages > page)
obj.put("paging", DataObject.empty().put("next", (request.url().replace("http://", "https://")) + "?page=" + (page + 1)));
return obj;
}
Aggregations