Search in sources :

Example 6 with RestActionImpl

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

the class ThreadChannelImpl method retrieveThreadMembers.

@Override
public RestAction<List<ThreadMember>> retrieveThreadMembers() {
    // TODO-threads: This interacts with GUILD_MEMBERS in some way. Need to test and document how.
    Route.CompiledRoute route = Route.Channels.LIST_THREAD_MEMBERS.compile(getId());
    return new RestActionImpl<>(getJDA(), route, (response, request) -> {
        EntityBuilder builder = api.getEntityBuilder();
        List<ThreadMember> threadMembers = new LinkedList<>();
        DataArray memberArr = response.getArray();
        for (int i = 0; i < memberArr.length(); i++) {
            final DataObject object = memberArr.getObject(i);
            // Very possible this is gonna break because we don't get user/member info with threadmembers
            // TODO revisit the @Nonnull annotations in ThreadMember due to the lack of user/member info. Might be a time to introduce RestX entities?
            threadMembers.add(builder.createThreadMember((GuildImpl) this.getGuild(), this, object));
        }
        return Collections.unmodifiableList(threadMembers);
    });
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(net.dv8tion.jda.internal.requests.Route) LinkedList(java.util.LinkedList) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 7 with RestActionImpl

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

the class MessageReaction method removeReaction.

/**
 * Removes this Reaction from the Message.
 * <br>This will remove the reaction of the {@link net.dv8tion.jda.api.entities.User User}
 * provided.
 *
 * <p>If the provided User did not react with this Reaction this does nothing.
 *
 * <p>Possible ErrorResponses include:
 * <ul>
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_MESSAGE UNKNOWN_MESSAGE}
 *     <br>If the message this reaction was attached to got deleted.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_CHANNEL UNKNOWN_CHANNEL}
 *     <br>If the channel this reaction was used in got deleted.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
 *     <br>If we were removed from the channel/guild</li>
 * </ul>
 *
 * @param  user
 *         The User of which to remove the reaction
 *
 * @throws java.lang.IllegalArgumentException
 *         If the provided {@code user} is null.
 * @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
 *         If the provided User is not us and we do not have permission to
 *         {@link net.dv8tion.jda.api.Permission#MESSAGE_MANAGE manage messages}
 *         in the channel this reaction was used in
 * @throws net.dv8tion.jda.api.exceptions.PermissionException
 *         If the message is from another user in a {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel}
 *
 * @return {@link net.dv8tion.jda.api.requests.RestAction RestAction}
 *         Nothing is returned on success
 */
@Nonnull
@CheckReturnValue
public RestAction<Void> removeReaction(@Nonnull User user) {
    Checks.notNull(user, "User");
    boolean self = user.equals(getJDA().getSelfUser());
    if (!self) {
        if (!channel.getType().isGuild()) {
            throw new PermissionException("Unable to remove Reaction of other user in non-guild channels!");
        }
        IPermissionContainer permChannel = (IPermissionContainer) this.channel;
        if (!permChannel.getGuild().getSelfMember().hasPermission(permChannel, Permission.MESSAGE_MANAGE))
            throw new InsufficientPermissionException(permChannel, Permission.MESSAGE_MANAGE);
    }
    String code = getReactionCode();
    String target = self ? "@me" : user.getId();
    Route.CompiledRoute route = Route.Messages.REMOVE_REACTION.compile(channel.getId(), getMessageId(), code, target);
    return new RestActionImpl<>(getJDA(), route);
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) Route(net.dv8tion.jda.internal.requests.Route) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Example 8 with RestActionImpl

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

the class NewsChannel method crosspostMessageById.

/**
 * Attempts to crosspost the provided message.
 *
 * <p>The following {@link net.dv8tion.jda.api.requests.ErrorResponse ErrorResponses} are possible:
 * <ul>
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#ALREADY_CROSSPOSTED ALREADY_CROSSPOSTED}
 *     <br>The target message has already been crossposted.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS}
 *     <br>The request was attempted after the account lost access to the
 *         {@link net.dv8tion.jda.api.entities.Guild Guild}
 *         typically due to being kicked or removed, or after {@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL Permission.VIEW_CHANNEL}
 *         was revoked in the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS}
 *     <br>The request was attempted after the account lost
 *         {@link net.dv8tion.jda.api.Permission#MESSAGE_MANAGE Permission.MESSAGE_MANAGE} in the TextChannel.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_MESSAGE UNKNOWN_MESSAGE}
 *     <br>The provided {@code messageId} is unknown in this MessageChannel, either due to the id being invalid, or
 *         the message it referred to has already been deleted.</li>
 *
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_CHANNEL UNKNOWN_CHANNEL}
 *     <br>The request was attempted after the channel was deleted.</li>
 * </ul>
 *
 * @param  messageId
 *         The messageId to crosspost
 *
 * @throws java.lang.IllegalArgumentException
 *         If provided {@code messageId} is {@code null} or empty.
 * @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
 *         If the currently logged in account does not have
 *         {@link net.dv8tion.jda.api.Permission#VIEW_CHANNEL Permission.VIEW_CHANNEL} in this channel.
 *
 * @return {@link net.dv8tion.jda.api.requests.RestAction} - Type: {@link Message}
 *
 * @since  4.2.1
 */
@Nonnull
@CheckReturnValue
default RestAction<Message> crosspostMessageById(@Nonnull String messageId) {
    Checks.isSnowflake(messageId);
    if (!getGuild().getSelfMember().hasAccess(this))
        throw new MissingAccessException(this, Permission.VIEW_CHANNEL);
    Route.CompiledRoute route = Route.Messages.CROSSPOST_MESSAGE.compile(getId(), messageId);
    return new RestActionImpl<>(getJDA(), route, (response, request) -> request.getJDA().getEntityBuilder().createMessageWithChannel(response.getObject(), this, false));
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) MissingAccessException(net.dv8tion.jda.api.exceptions.MissingAccessException) Route(net.dv8tion.jda.internal.requests.Route) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Example 9 with RestActionImpl

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

the class ShardManager method retrieveUserById.

/**
 * Attempts to retrieve a {@link net.dv8tion.jda.api.entities.User User} object based on the provided id.
 * <br>This first calls {@link #getUserById(long)}, and if the return is {@code null} then a request
 * is made to the Discord servers.
 *
 * <p>The returned {@link net.dv8tion.jda.api.requests.RestAction RestAction} can encounter the following Discord errors:
 * <ul>
 *     <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_USER ErrorResponse.UNKNOWN_USER}
 *     <br>Occurs when the provided id does not refer to a {@link net.dv8tion.jda.api.entities.User User}
 *     known by Discord. Typically occurs when developers provide an incomplete id (cut short).</li>
 * </ul>
 *
 * @param  id
 *         The id of the requested {@link net.dv8tion.jda.api.entities.User User}.
 *
 * @throws java.lang.IllegalStateException
 *         If there isn't any active shards.
 *
 * @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link net.dv8tion.jda.api.entities.User User}
 *         <br>On request, gets the User with id matching provided id from Discord.
 */
@Nonnull
@CheckReturnValue
default RestAction<User> retrieveUserById(long id) {
    JDA api = null;
    for (JDA shard : getShardCache()) {
        api = shard;
        EnumSet<GatewayIntent> intents = shard.getGatewayIntents();
        User user = shard.getUserById(id);
        boolean isUpdated = intents.contains(GatewayIntent.GUILD_PRESENCES) || intents.contains(GatewayIntent.GUILD_MEMBERS);
        if (user != null && isUpdated)
            return new CompletedRestAction<>(shard, user);
    }
    if (api == null)
        throw new IllegalStateException("no shards active");
    JDAImpl jda = (JDAImpl) api;
    Route.CompiledRoute route = Route.Users.GET_USER.compile(Long.toUnsignedString(id));
    return new RestActionImpl<>(jda, route, (response, request) -> jda.getEntityBuilder().createUser(response.getObject()));
}
Also used : RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) JDA(net.dv8tion.jda.api.JDA) GatewayIntent(net.dv8tion.jda.api.requests.GatewayIntent) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Route(net.dv8tion.jda.internal.requests.Route) CheckReturnValue(javax.annotation.CheckReturnValue) Nonnull(javax.annotation.Nonnull)

Example 10 with RestActionImpl

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

the class JDA method getRestPing.

/**
 * The time in milliseconds that discord took to respond to a REST request.
 * <br>This will request the current user from the API and calculate the time the response took.
 *
 * <h4>Example</h4>
 * <pre><code>
 * jda.getRestPing().queue( (time) {@literal ->}
 *     channel.sendMessageFormat("Ping: %d ms", time).queue()
 * );
 * </code></pre>
 *
 * @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: long
 *
 * @since 4.0.0
 *
 * @see    #getGatewayPing()
 */
@Nonnull
default RestAction<Long> getRestPing() {
    AtomicLong time = new AtomicLong();
    Route.CompiledRoute route = Route.Self.GET_SELF.compile();
    RestActionImpl<Long> action = new RestActionImpl<>(this, route, (response, request) -> System.currentTimeMillis() - time.get());
    action.setCheck(() -> {
        time.set(System.currentTimeMillis());
        return true;
    });
    return action;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Aggregations

RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)25 Route (net.dv8tion.jda.internal.requests.Route)24 Nonnull (javax.annotation.Nonnull)20 DataObject (net.dv8tion.jda.api.utils.data.DataObject)11 JDAImpl (net.dv8tion.jda.internal.JDAImpl)10 CheckReturnValue (javax.annotation.CheckReturnValue)7 DataArray (net.dv8tion.jda.api.utils.data.DataArray)7 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)6 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)5 AuditableRestActionImpl (net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl)5 MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)3 ArrayList (java.util.ArrayList)2 net.dv8tion.jda.api.entities (net.dv8tion.jda.api.entities)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 WebhookMessageBuilder (club.minnced.discord.webhook.send.WebhookMessageBuilder)1 ErrorCategory (com.mongodb.ErrorCategory)1 MongoWriteException (com.mongodb.MongoWriteException)1