Search in sources :

Example 1 with MissingAccessException

use of net.dv8tion.jda.api.exceptions.MissingAccessException in project JDA by DV8FromTheWorld.

the class PermissionOverrideImpl method checkPermissions.

private void checkPermissions() {
    Member selfMember = getGuild().getSelfMember();
    IPermissionContainer channel = getChannel();
    if (!selfMember.hasPermission(channel, Permission.VIEW_CHANNEL))
        throw new MissingAccessException(channel, Permission.VIEW_CHANNEL);
    if (!selfMember.hasAccess(channel))
        throw new MissingAccessException(channel, Permission.VOICE_CONNECT);
    if (!selfMember.hasPermission(channel, Permission.MANAGE_PERMISSIONS))
        throw new InsufficientPermissionException(channel, Permission.MANAGE_PERMISSIONS);
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) MissingAccessException(net.dv8tion.jda.api.exceptions.MissingAccessException)

Example 2 with MissingAccessException

use of net.dv8tion.jda.api.exceptions.MissingAccessException in project JDA by DV8FromTheWorld.

the class MessageHistory method checkArguments.

private static void checkArguments(MessageChannel channel, String messageId) {
    Checks.isSnowflake(messageId, "Message ID");
    Checks.notNull(channel, "Channel");
    if (channel.getType() == ChannelType.TEXT) {
        TextChannel t = (TextChannel) channel;
        Member selfMember = t.getGuild().getSelfMember();
        if (!selfMember.hasAccess(t))
            throw new MissingAccessException(t, Permission.VIEW_CHANNEL);
        if (!selfMember.hasPermission(t, Permission.MESSAGE_HISTORY))
            throw new InsufficientPermissionException(t, Permission.MESSAGE_HISTORY);
    }
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) MissingAccessException(net.dv8tion.jda.api.exceptions.MissingAccessException)

Example 3 with MissingAccessException

use of net.dv8tion.jda.api.exceptions.MissingAccessException in project JDA by DV8FromTheWorld.

the class MessageReference method checkPermission.

private void checkPermission(Permission permission) {
    if (guild == null || !(channel instanceof IPermissionContainer))
        return;
    Member selfMember = guild.getSelfMember();
    IPermissionContainer permChannel = (IPermissionContainer) channel;
    if (!selfMember.hasAccess(permChannel))
        throw new MissingAccessException(permChannel, Permission.VIEW_CHANNEL);
    if (!selfMember.hasPermission(permChannel, permission))
        throw new InsufficientPermissionException(permChannel, permission);
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) MissingAccessException(net.dv8tion.jda.api.exceptions.MissingAccessException)

Example 4 with MissingAccessException

use of net.dv8tion.jda.api.exceptions.MissingAccessException 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 5 with MissingAccessException

use of net.dv8tion.jda.api.exceptions.MissingAccessException in project JDA by DV8FromTheWorld.

the class ReceivedMessage method delete.

@Nonnull
@Override
public AuditableRestAction<Void> delete() {
    if (isEphemeral())
        throw new IllegalStateException("Cannot delete ephemeral messages.");
    if (!getJDA().getSelfUser().equals(getAuthor())) {
        if (isFromType(ChannelType.PRIVATE))
            throw new IllegalStateException("Cannot delete another User's messages in a PrivateChannel.");
        GuildMessageChannel gChan = getGuildChannel();
        Member sMember = getGuild().getSelfMember();
        if (!sMember.hasAccess(gChan))
            throw new MissingAccessException(gChan, Permission.VIEW_CHANNEL);
        else if (!sMember.hasPermission(gChan, Permission.MESSAGE_MANAGE))
            throw new InsufficientPermissionException(gChan, Permission.MESSAGE_MANAGE);
    }
    return channel.deleteMessageById(getIdLong());
}
Also used : InsufficientPermissionException(net.dv8tion.jda.api.exceptions.InsufficientPermissionException) MissingAccessException(net.dv8tion.jda.api.exceptions.MissingAccessException) Nonnull(javax.annotation.Nonnull)

Aggregations

MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)8 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)7 Nonnull (javax.annotation.Nonnull)3 CheckReturnValue (javax.annotation.CheckReturnValue)2 IPermissionContainer (net.dv8tion.jda.api.entities.IPermissionContainer)1 Member (net.dv8tion.jda.api.entities.Member)1 PermissionOverride (net.dv8tion.jda.api.entities.PermissionOverride)1 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)1 Route (net.dv8tion.jda.internal.requests.Route)1