Search in sources :

Example 1 with Route

use of net.dv8tion.jda.internal.requests.Route in project JDA by DV8FromTheWorld.

the class BaseGuildMessageChannelMixin method retrieveWebhooks.

@Nonnull
@Override
default RestAction<List<Webhook>> retrieveWebhooks() {
    checkPermission(Permission.MANAGE_WEBHOOKS);
    Route.CompiledRoute route = Route.Channels.GET_WEBHOOKS.compile(getId());
    JDAImpl jda = (JDAImpl) getJDA();
    return new RestActionImpl<>(jda, route, (response, request) -> {
        DataArray array = response.getArray();
        List<Webhook> webhooks = new ArrayList<>(array.length());
        EntityBuilder builder = jda.getEntityBuilder();
        for (int i = 0; i < array.length(); i++) {
            try {
                webhooks.add(builder.createWebhook(array.getObject(i)));
            } catch (UncheckedIOException | NullPointerException e) {
                JDAImpl.LOG.error("Error while creating websocket from json", e);
            }
        }
        return Collections.unmodifiableList(webhooks);
    });
}
Also used : ArrayList(java.util.ArrayList) JDAImpl(net.dv8tion.jda.internal.JDAImpl) UncheckedIOException(java.io.UncheckedIOException) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) Webhook(net.dv8tion.jda.api.entities.Webhook) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Example 2 with Route

use of net.dv8tion.jda.internal.requests.Route 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 3 with Route

use of net.dv8tion.jda.internal.requests.Route in project JDA by DV8FromTheWorld.

the class InviteImpl method resolve.

public static RestAction<Invite> resolve(final JDA api, final String code, final boolean withCounts) {
    Checks.notNull(code, "code");
    Checks.notNull(api, "api");
    Route.CompiledRoute route = Route.Invites.GET_INVITE.compile(code);
    if (withCounts)
        route = route.withQueryParams("with_counts", "true");
    JDAImpl jda = (JDAImpl) api;
    return new RestActionImpl<>(api, route, (response, request) -> jda.getEntityBuilder().createInvite(response.getObject()));
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Route(net.dv8tion.jda.internal.requests.Route)

Example 4 with Route

use of net.dv8tion.jda.internal.requests.Route in project JDA by DV8FromTheWorld.

the class NewsChannelImpl method follow.

@Nonnull
@Override
public RestAction<Webhook.WebhookReference> follow(@Nonnull String targetChannelId) {
    Checks.notNull(targetChannelId, "Target Channel ID");
    Route.CompiledRoute route = Route.Channels.FOLLOW_CHANNEL.compile(getId());
    DataObject body = DataObject.empty().put("webhook_channel_id", targetChannelId);
    return new RestActionImpl<>(getJDA(), route, body, (response, request) -> {
        DataObject json = response.getObject();
        return new Webhook.WebhookReference(request.getJDA(), json.getUnsignedLong("webhook_id"), json.getUnsignedLong("channel_id"));
    });
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Example 5 with Route

use of net.dv8tion.jda.internal.requests.Route in project JDA by DV8FromTheWorld.

the class MessageChannelMixin method bulkDeleteMessages.

// ---- Helpers -----
default RestActionImpl<Void> bulkDeleteMessages(Collection<String> messageIds) {
    DataObject body = DataObject.empty().put("messages", messageIds);
    Route.CompiledRoute route = Route.Messages.DELETE_MESSAGES.compile(getId());
    return new RestActionImpl<>(getJDA(), route, body);
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(net.dv8tion.jda.internal.requests.Route)

Aggregations

Route (net.dv8tion.jda.internal.requests.Route)31 Nonnull (javax.annotation.Nonnull)24 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)24 DataObject (net.dv8tion.jda.api.utils.data.DataObject)12 JDAImpl (net.dv8tion.jda.internal.JDAImpl)12 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)10 AuditableRestActionImpl (net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl)10 CheckReturnValue (javax.annotation.CheckReturnValue)9 DataArray (net.dv8tion.jda.api.utils.data.DataArray)8 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)5 JDA (net.dv8tion.jda.api.JDA)3 HierarchyException (net.dv8tion.jda.api.exceptions.HierarchyException)3 MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 DeferredRestAction (net.dv8tion.jda.internal.requests.DeferredRestAction)2 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)1 WebhookEmbedBuilder (club.minnced.discord.webhook.send.WebhookEmbedBuilder)1 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1