Search in sources :

Example 61 with DataArray

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;
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray) CheckReturnValue(javax.annotation.CheckReturnValue)

Example 62 with DataArray

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;
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray) CheckReturnValue(javax.annotation.CheckReturnValue)

Example 63 with DataArray

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;
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray) RoleReward(at.xirado.bean.misc.objects.RoleReward) CheckReturnValue(javax.annotation.CheckReturnValue)

Example 64 with DataArray

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();
}
Also used : GuildManager(at.xirado.bean.data.GuildManager) DataArray(net.dv8tion.jda.api.utils.data.DataArray) ObjectBuilder(at.xirado.bean.backend.ObjectBuilder) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) DiscordCredentials(at.xirado.bean.backend.DiscordCredentials) Authenticator(at.xirado.bean.backend.Authenticator) Permission(net.dv8tion.jda.api.Permission) ISnowflake(net.dv8tion.jda.api.entities.ISnowflake) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) GuildData(at.xirado.bean.data.GuildData) Guild(net.dv8tion.jda.api.entities.Guild) WebServer(at.xirado.bean.backend.WebServer) Request(spark.Request) Response(spark.Response) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(spark.Route) Bean(at.xirado.bean.Bean) GuildData(at.xirado.bean.data.GuildData) DiscordCredentials(at.xirado.bean.backend.DiscordCredentials) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Guild(net.dv8tion.jda.api.entities.Guild) DataArray(net.dv8tion.jda.api.utils.data.DataArray) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Authenticator(at.xirado.bean.backend.Authenticator) ISnowflake(net.dv8tion.jda.api.entities.ISnowflake)

Example 65 with DataArray

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;
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) Guild(net.dv8tion.jda.api.entities.Guild) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Aggregations

DataArray (net.dv8tion.jda.api.utils.data.DataArray)67 DataObject (net.dv8tion.jda.api.utils.data.DataObject)40 Nonnull (javax.annotation.Nonnull)21 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)14 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)10 JDAImpl (net.dv8tion.jda.internal.JDAImpl)9 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)8 ArrayList (java.util.ArrayList)8 Route (net.dv8tion.jda.internal.requests.Route)8 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)7 Collectors (java.util.stream.Collectors)6 CheckReturnValue (javax.annotation.CheckReturnValue)6 TemplateRole (net.dv8tion.jda.api.entities.templates.TemplateRole)5 ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)5 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)5 Bean (at.xirado.bean.Bean)4 java.util (java.util)4 OnlineStatus (net.dv8tion.jda.api.OnlineStatus)4 Template (net.dv8tion.jda.api.entities.templates.Template)4 CacheView (net.dv8tion.jda.api.utils.cache.CacheView)4