Search in sources :

Example 11 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class GuildImpl method updateCommandPrivileges.

@Nonnull
@Override
public RestAction<Map<String, List<CommandPrivilege>>> updateCommandPrivileges(@Nonnull Map<String, ? extends Collection<CommandPrivilege>> privileges) {
    Checks.notNull(privileges, "Privileges");
    privileges.forEach((key, value) -> {
        Checks.isSnowflake(key, "Map Key");
        Checks.noneNull(value, "Privilege List for Command");
        Checks.check(value.size() <= 10, "Cannot have more than 10 privileges for a command!");
    });
    DataArray array = DataArray.empty();
    privileges.forEach((commandId, list) -> {
        DataObject entry = DataObject.empty();
        entry.put("id", commandId);
        entry.put("permissions", DataArray.fromCollection(list));
        array.add(entry);
    });
    Route.CompiledRoute route = Route.Interactions.EDIT_ALL_COMMAND_PERMISSIONS.compile(getJDA().getSelfUser().getApplicationId(), getId());
    return new RestActionImpl<>(getJDA(), route, RequestBody.create(Requester.MEDIA_TYPE_JSON, array.toJson()), (response, request) -> {
        Map<String, List<CommandPrivilege>> map = new HashMap<>();
        response.getArray().stream(DataArray::getObject).forEach(obj -> {
            String id = obj.getString("id");
            List<CommandPrivilege> list = parsePrivilegesList(obj);
            map.put(id, list);
        });
        return map;
    });
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) CommandPrivilege(net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Nonnull(javax.annotation.Nonnull)

Example 12 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class GuildImpl method retrieveEmotes.

@Nonnull
@Override
public RestAction<List<ListedEmote>> retrieveEmotes() {
    Route.CompiledRoute route = Route.Emotes.GET_EMOTES.compile(getId());
    return new RestActionImpl<>(getJDA(), route, (response, request) -> {
        EntityBuilder builder = GuildImpl.this.getJDA().getEntityBuilder();
        DataArray emotes = response.getArray();
        List<ListedEmote> list = new ArrayList<>(emotes.length());
        for (int i = 0; i < emotes.length(); i++) {
            DataObject emote = emotes.getObject(i);
            list.add(builder.createEmote(GuildImpl.this, emote));
        }
        return Collections.unmodifiableList(list);
    });
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Nonnull(javax.annotation.Nonnull)

Example 13 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class InviteImpl method expand.

@Nonnull
@Override
public RestAction<Invite> expand() {
    if (this.expanded)
        return new CompletedRestAction<>(getJDA(), this);
    if (this.type != Invite.InviteType.GUILD)
        throw new IllegalStateException("Only guild invites can be expanded");
    final net.dv8tion.jda.api.entities.Guild guild = this.api.getGuildById(this.guild.getIdLong());
    if (guild == null)
        throw new UnsupportedOperationException("You're not in the guild this invite points to");
    final Member member = guild.getSelfMember();
    Route.CompiledRoute route;
    // TODO-v5: There are more than Text and Voice channels now. Revisit this.
    final IPermissionContainer channel = this.channel.getType() == ChannelType.TEXT ? guild.getTextChannelById(this.channel.getIdLong()) : guild.getVoiceChannelById(this.channel.getIdLong());
    if (member.hasPermission(channel, Permission.MANAGE_CHANNEL)) {
        route = Route.Invites.GET_CHANNEL_INVITES.compile(channel.getId());
    } else if (member.hasPermission(Permission.MANAGE_SERVER)) {
        route = Route.Invites.GET_GUILD_INVITES.compile(guild.getId());
    } else {
        throw new InsufficientPermissionException(channel, Permission.MANAGE_CHANNEL, "You don't have the permission to view the full invite info");
    }
    return new RestActionImpl<>(this.api, route, (response, request) -> {
        final EntityBuilder entityBuilder = this.api.getEntityBuilder();
        final DataArray array = response.getArray();
        for (int i = 0; i < array.length(); i++) {
            final DataObject object = array.getObject(i);
            if (InviteImpl.this.code.equals(object.getString("code"))) {
                return entityBuilder.createInvite(object);
            }
        }
        throw new IllegalStateException("Missing the invite in the channel/guild invite list");
    });
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) DataArray(net.dv8tion.jda.api.utils.data.DataArray) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Example 14 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class GuildUpdateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long id = content.getLong("id");
    if (getJDA().getGuildSetupController().isLocked(id))
        return id;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(id);
    if (guild == null) {
        EventCache.LOG.debug("Caching GUILD_UPDATE for guild with id: {}", id);
        getJDA().getEventCache().cache(EventCache.Type.GUILD, id, responseNumber, allContent, this::handle);
        return null;
    }
    // When member limits aren't initialized we don't fire an update event for them
    int maxMembers = content.getInt("max_members", 0);
    int maxPresences = content.getInt("max_presences", 5000);
    if (guild.getMaxMembers() == 0) {
        // Initialize member limits to avoid unwanted update events
        guild.setMaxPresences(maxPresences);
        guild.setMaxMembers(maxMembers);
    }
    long ownerId = content.getLong("owner_id");
    int boostCount = content.getInt("premium_subscription_count", 0);
    int boostTier = content.getInt("premium_tier", 0);
    String description = content.getString("description", null);
    String vanityCode = content.getString("vanity_url_code", null);
    String bannerId = content.getString("banner", null);
    String name = content.getString("name");
    String iconId = content.getString("icon", null);
    String splashId = content.getString("splash", null);
    Guild.VerificationLevel verificationLevel = Guild.VerificationLevel.fromKey(content.getInt("verification_level"));
    Guild.NotificationLevel notificationLevel = Guild.NotificationLevel.fromKey(content.getInt("default_message_notifications"));
    Guild.MFALevel mfaLevel = Guild.MFALevel.fromKey(content.getInt("mfa_level"));
    Guild.NSFWLevel nsfwLevel = Guild.NSFWLevel.fromKey(content.getInt("nsfw_level", -1));
    Guild.ExplicitContentLevel explicitContentLevel = Guild.ExplicitContentLevel.fromKey(content.getInt("explicit_content_filter"));
    Guild.Timeout afkTimeout = Guild.Timeout.fromKey(content.getInt("afk_timeout"));
    Locale locale = Locale.forLanguageTag(content.getString("preferred_locale", "en-US"));
    VoiceChannel afkChannel = content.isNull("afk_channel_id") ? null : guild.getVoiceChannelsView().get(content.getLong("afk_channel_id"));
    TextChannel systemChannel = content.isNull("system_channel_id") ? null : guild.getTextChannelsView().get(content.getLong("system_channel_id"));
    TextChannel rulesChannel = content.isNull("rules_channel_id") ? null : guild.getTextChannelsView().get(content.getLong("rules_channel_id"));
    TextChannel communityUpdatesChannel = content.isNull("public_updates_channel_id") ? null : guild.getTextChannelsView().get(content.getLong("public_updates_channel_id"));
    Set<String> features;
    if (!content.isNull("features")) {
        DataArray featureArr = content.getArray("features");
        features = StreamSupport.stream(featureArr.spliterator(), false).map(String::valueOf).collect(Collectors.toSet());
    } else {
        features = Collections.emptySet();
    }
    if (ownerId != guild.getOwnerIdLong()) {
        long oldOwnerId = guild.getOwnerIdLong();
        Member oldOwner = guild.getOwner();
        Member newOwner = guild.getMembersView().get(ownerId);
        if (newOwner == null)
            WebSocketClient.LOG.debug("Received {} with owner not in cache. UserId: {} GuildId: {}", allContent.get("t"), ownerId, id);
        guild.setOwner(newOwner);
        guild.setOwnerId(ownerId);
        getJDA().handleEvent(new GuildUpdateOwnerEvent(getJDA(), responseNumber, guild, oldOwner, oldOwnerId, ownerId));
    }
    if (!Objects.equals(description, guild.getDescription())) {
        String oldDescription = guild.getDescription();
        guild.setDescription(description);
        getJDA().handleEvent(new GuildUpdateDescriptionEvent(getJDA(), responseNumber, guild, oldDescription));
    }
    if (!Objects.equals(bannerId, guild.getBannerId())) {
        String oldBanner = guild.getBannerId();
        guild.setBannerId(bannerId);
        getJDA().handleEvent(new GuildUpdateBannerEvent(getJDA(), responseNumber, guild, oldBanner));
    }
    if (!Objects.equals(vanityCode, guild.getVanityCode())) {
        String oldCode = guild.getVanityCode();
        guild.setVanityCode(vanityCode);
        getJDA().handleEvent(new GuildUpdateVanityCodeEvent(getJDA(), responseNumber, guild, oldCode));
    }
    if (maxMembers != guild.getMaxMembers()) {
        int oldMax = guild.getMaxMembers();
        guild.setMaxMembers(maxMembers);
        getJDA().handleEvent(new GuildUpdateMaxMembersEvent(getJDA(), responseNumber, guild, oldMax));
    }
    if (maxPresences != guild.getMaxPresences()) {
        int oldMax = guild.getMaxPresences();
        guild.setMaxPresences(maxPresences);
        getJDA().handleEvent(new GuildUpdateMaxPresencesEvent(getJDA(), responseNumber, guild, oldMax));
    }
    if (boostCount != guild.getBoostCount()) {
        int oldCount = guild.getBoostCount();
        guild.setBoostCount(boostCount);
        getJDA().handleEvent(new GuildUpdateBoostCountEvent(getJDA(), responseNumber, guild, oldCount));
    }
    if (Guild.BoostTier.fromKey(boostTier) != guild.getBoostTier()) {
        Guild.BoostTier oldTier = guild.getBoostTier();
        guild.setBoostTier(boostTier);
        getJDA().handleEvent(new GuildUpdateBoostTierEvent(getJDA(), responseNumber, guild, oldTier));
    }
    if (!Objects.equals(name, guild.getName())) {
        String oldName = guild.getName();
        guild.setName(name);
        getJDA().handleEvent(new GuildUpdateNameEvent(getJDA(), responseNumber, guild, oldName));
    }
    if (!Objects.equals(iconId, guild.getIconId())) {
        String oldIconId = guild.getIconId();
        guild.setIconId(iconId);
        getJDA().handleEvent(new GuildUpdateIconEvent(getJDA(), responseNumber, guild, oldIconId));
    }
    if (!features.equals(guild.getFeatures())) {
        Set<String> oldFeatures = guild.getFeatures();
        guild.setFeatures(features);
        getJDA().handleEvent(new GuildUpdateFeaturesEvent(getJDA(), responseNumber, guild, oldFeatures));
    }
    if (!Objects.equals(splashId, guild.getSplashId())) {
        String oldSplashId = guild.getSplashId();
        guild.setSplashId(splashId);
        getJDA().handleEvent(new GuildUpdateSplashEvent(getJDA(), responseNumber, guild, oldSplashId));
    }
    if (!Objects.equals(verificationLevel, guild.getVerificationLevel())) {
        Guild.VerificationLevel oldVerificationLevel = guild.getVerificationLevel();
        guild.setVerificationLevel(verificationLevel);
        getJDA().handleEvent(new GuildUpdateVerificationLevelEvent(getJDA(), responseNumber, guild, oldVerificationLevel));
    }
    if (!Objects.equals(notificationLevel, guild.getDefaultNotificationLevel())) {
        Guild.NotificationLevel oldNotificationLevel = guild.getDefaultNotificationLevel();
        guild.setDefaultNotificationLevel(notificationLevel);
        getJDA().handleEvent(new GuildUpdateNotificationLevelEvent(getJDA(), responseNumber, guild, oldNotificationLevel));
    }
    if (!Objects.equals(mfaLevel, guild.getRequiredMFALevel())) {
        Guild.MFALevel oldMfaLevel = guild.getRequiredMFALevel();
        guild.setRequiredMFALevel(mfaLevel);
        getJDA().handleEvent(new GuildUpdateMFALevelEvent(getJDA(), responseNumber, guild, oldMfaLevel));
    }
    if (!Objects.equals(explicitContentLevel, guild.getExplicitContentLevel())) {
        Guild.ExplicitContentLevel oldExplicitContentLevel = guild.getExplicitContentLevel();
        guild.setExplicitContentLevel(explicitContentLevel);
        getJDA().handleEvent(new GuildUpdateExplicitContentLevelEvent(getJDA(), responseNumber, guild, oldExplicitContentLevel));
    }
    if (!Objects.equals(afkTimeout, guild.getAfkTimeout())) {
        Guild.Timeout oldAfkTimeout = guild.getAfkTimeout();
        guild.setAfkTimeout(afkTimeout);
        getJDA().handleEvent(new GuildUpdateAfkTimeoutEvent(getJDA(), responseNumber, guild, oldAfkTimeout));
    }
    if (!Objects.equals(locale, guild.getLocale())) {
        Locale oldLocale = guild.getLocale();
        guild.setLocale(locale.toLanguageTag());
        getJDA().handleEvent(new GuildUpdateLocaleEvent(getJDA(), responseNumber, guild, oldLocale));
    }
    if (!Objects.equals(afkChannel, guild.getAfkChannel())) {
        VoiceChannel oldAfkChannel = guild.getAfkChannel();
        guild.setAfkChannel(afkChannel);
        getJDA().handleEvent(new GuildUpdateAfkChannelEvent(getJDA(), responseNumber, guild, oldAfkChannel));
    }
    if (!Objects.equals(systemChannel, guild.getSystemChannel())) {
        TextChannel oldSystemChannel = guild.getSystemChannel();
        guild.setSystemChannel(systemChannel);
        getJDA().handleEvent(new GuildUpdateSystemChannelEvent(getJDA(), responseNumber, guild, oldSystemChannel));
    }
    if (!Objects.equals(rulesChannel, guild.getRulesChannel())) {
        TextChannel oldRulesChannel = guild.getRulesChannel();
        guild.setRulesChannel(rulesChannel);
        getJDA().handleEvent(new GuildUpdateRulesChannelEvent(getJDA(), responseNumber, guild, oldRulesChannel));
    }
    if (!Objects.equals(communityUpdatesChannel, guild.getCommunityUpdatesChannel())) {
        TextChannel oldCommunityUpdatesChannel = guild.getCommunityUpdatesChannel();
        guild.setCommunityUpdatesChannel(communityUpdatesChannel);
        getJDA().handleEvent(new GuildUpdateCommunityUpdatesChannelEvent(getJDA(), responseNumber, guild, oldCommunityUpdatesChannel));
    }
    if (content.hasKey("nsfw_level") && nsfwLevel != guild.getNSFWLevel()) {
        Guild.NSFWLevel oldNSFWLevel = guild.getNSFWLevel();
        guild.setNSFWLevel(nsfwLevel);
        getJDA().handleEvent(new GuildUpdateNSFWLevelEvent(getJDA(), responseNumber, guild, oldNSFWLevel));
    }
    return null;
}
Also used : Locale(java.util.Locale) Guild(net.dv8tion.jda.api.entities.Guild) DataArray(net.dv8tion.jda.api.utils.data.DataArray) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) TextChannel(net.dv8tion.jda.api.entities.TextChannel) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Member(net.dv8tion.jda.api.entities.Member)

Example 15 with DataArray

use of net.dv8tion.jda.api.utils.data.DataArray in project JDA by DV8FromTheWorld.

the class MessageBulkDeleteHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    if (!content.isNull("guild_id")) {
        long guildId = content.getLong("guild_id");
        if (getJDA().getGuildSetupController().isLocked(guildId))
            return guildId;
    }
    final long channelId = content.getLong("channel_id");
    if (getJDA().isBulkDeleteSplittingEnabled()) {
        SocketHandler handler = getJDA().getClient().getHandlers().get("MESSAGE_DELETE");
        content.getArray("ids").forEach(id -> {
            handler.handle(responseNumber, DataObject.empty().put("t", "MESSAGE_DELETE").put("d", DataObject.empty().put("channel_id", Long.toUnsignedString(channelId)).put("id", id)));
        });
    } else {
        // TODO-v5-unified-channel-cache
        GuildMessageChannel channel = getJDA().getTextChannelById(channelId);
        if (channel == null)
            channel = getJDA().getNewsChannelById(channelId);
        if (channel == null)
            channel = getJDA().getThreadChannelById(channelId);
        if (channel == null) {
            getJDA().getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
            EventCache.LOG.debug("Received a Bulk Message Delete for a GuildMessageChannel that is not yet cached.");
            return null;
        }
        if (getJDA().getGuildSetupController().isLocked(channel.getGuild().getIdLong()))
            return channel.getGuild().getIdLong();
        DataArray array = content.getArray("ids");
        List<String> messages = array.stream(DataArray::getString).collect(Collectors.toList());
        getJDA().handleEvent(new MessageBulkDeleteEvent(getJDA(), responseNumber, channel, messages));
    }
    return null;
}
Also used : MessageBulkDeleteEvent(net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent) DataArray(net.dv8tion.jda.api.utils.data.DataArray) GuildMessageChannel(net.dv8tion.jda.api.entities.GuildMessageChannel)

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