Skip to content

Commit

Permalink
Add MissingEntityInteractionPermissionsException
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Apr 26, 2024
1 parent 02fd62b commit 2b296d4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 15 deletions.
30 changes: 20 additions & 10 deletions src/main/java/net/dv8tion/jda/api/entities/IPermissionHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ public interface IPermissionHolder extends ISnowflake
* If the channel is null
* @throws net.dv8tion.jda.api.exceptions.DetachedEntityException
* if this is a role and the bot {@link Guild#isDetached() isn't in the guild}.
* @throws UnsupportedOperationException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild}, and the channel isn't the current interaction's channel.
* @throws net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild},
* and the combination of Member and GuildChannel doesn't have permission data,
* see {@link net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException MissingEntityInteractionPermissionsException}.
*
* @return Set of Permissions granted to this Permission Holder in the specified channel.
*/
Expand Down Expand Up @@ -104,8 +106,10 @@ public interface IPermissionHolder extends ISnowflake
* If the channel is null
* @throws net.dv8tion.jda.api.exceptions.DetachedEntityException
* if this is a role and the bot {@link Guild#isDetached() isn't in the guild}.
* @throws UnsupportedOperationException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild}, and the channel isn't the current interaction's channel.
* @throws net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild},
* and the combination of Member and GuildChannel doesn't have permission data,
* see {@link net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException MissingEntityInteractionPermissionsException}.
*
* @return EnumSet of the explicitly granted permissions in the specified channel
*/
Expand Down Expand Up @@ -161,8 +165,10 @@ default boolean hasPermission(@Nonnull Collection<Permission> permissions)
* If null is provided
* @throws net.dv8tion.jda.api.exceptions.DetachedEntityException
* if this is a role and the bot {@link Guild#isDetached() isn't in the guild}.
* @throws UnsupportedOperationException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild}, and the channel isn't the current interaction's channel.
* @throws net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild},
* and the combination of Member and GuildChannel doesn't have permission data,
* see {@link net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException MissingEntityInteractionPermissionsException}.
*
* @return True, if all of the specified Permissions are granted to this PermissionHolder in the provided GuildChannel.
*
Expand All @@ -183,8 +189,10 @@ default boolean hasPermission(@Nonnull Collection<Permission> permissions)
* If null is provided
* @throws net.dv8tion.jda.api.exceptions.DetachedEntityException
* if this is a role and the bot {@link Guild#isDetached() isn't in the guild}.
* @throws UnsupportedOperationException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild}, and the channel isn't the current interaction's channel.
* @throws net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild},
* and the combination of Member and GuildChannel doesn't have permission data,
* see {@link net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException MissingEntityInteractionPermissionsException}.
*
* @return True, if all of the specified Permissions are granted to this PermissionHolder in the provided GuildChannel.
*/
Expand All @@ -205,8 +213,10 @@ default boolean hasPermission(@Nonnull GuildChannel channel, @Nonnull Collection
* If null is provided
* @throws net.dv8tion.jda.api.exceptions.DetachedEntityException
* if this is a role and the bot {@link Guild#isDetached() isn't in the guild}.
* @throws UnsupportedOperationException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild}, and the channel isn't the current interaction's channel.
* @throws net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException
* if this is a member, the bot {@link Guild#isDetached() isn't in the guild},
* and the combination of Member and GuildChannel doesn't have permission data,
* see {@link net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException MissingEntityInteractionPermissionsException}.
*
* @return True, if the PermissionHolder has access
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ default boolean canTalk()
* @throws net.dv8tion.jda.api.exceptions.DetachedEntityException
* If this channel is a thread,
* and the bot {@link net.dv8tion.jda.api.entities.Guild#isDetached() isn't in the guild}.
* @throws UnsupportedOperationException
* @throws net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException
* if the bot {@link net.dv8tion.jda.api.entities.Guild#isDetached() isn't in the guild},
* and this channel isn't the current interaction's channel,
* and the member isn't the current interaction's caller.
* and the combination of the provided Member and this GuildChannel doesn't have permission data,
* see {@link net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException MissingEntityInteractionPermissionsException}.
*
* @param member
* The Member to check
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.exceptions;

/**
* Exception occurring on {@link net.dv8tion.jda.api.entities.detached.IDetachableEntity#isDetached() detached entities},
* indicating that the permissions could not be checked on this combination of channel and member.
*
* <p>Getting/Checking the permissions of a {@link net.dv8tion.jda.api.entities.Member Member} in a given {@link net.dv8tion.jda.api.entities.channel.middleman.GuildChannel GuildChannel},
* will only work when either:
* <ul>
* <li>The member is the {@link net.dv8tion.jda.api.interactions.Interaction#getMember() interaction caller},
* and the channel is the {@link net.dv8tion.jda.api.interactions.Interaction#getGuildChannel() interaction channel}
* </li>
* <li>The member is an interaction option (such as slash command option or a member {@link net.dv8tion.jda.api.interactions.components.selections.EntitySelectMenu EntitySelectMenu} value)
* and the channel is the {@link net.dv8tion.jda.api.interactions.Interaction#getGuildChannel() interaction channel}
* </li>
* <li>The member is the {@link net.dv8tion.jda.api.interactions.Interaction#getMember() interaction caller}, and the channel is an interaction option</li>
* </ul>
*/
public class MissingEntityInteractionPermissionsException extends RuntimeException
{
/**
* Creates a new MissingEntityInteractionPermissionsException
*/
public MissingEntityInteractionPermissionsException()
{
this("Cannot perform action as the bot is not in the guild");
}

/**
* Creates a new MissingEntityInteractionPermissionsException
*
* @param reason
* The reason for this Exception
*/
public MissingEntityInteractionPermissionsException(String reason)
{
super(reason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.channel.unions.DefaultGuildChannelUnion;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.exceptions.MissingEntityInteractionPermissionsException;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IInteractionPermissionMixin;
Expand Down Expand Up @@ -270,8 +271,10 @@ private long getRawInteractionPermissions(@Nonnull GuildChannel channel)
return channelInteractionPermissions.getPermissions();
}

throw new UnsupportedOperationException("Detached member permissions can only be retrieved in the interaction channel, " +
"and channels only contain the permissions of the interaction caller");
throw new MissingEntityInteractionPermissionsException(
"Detached member permissions can only be retrieved in the interaction channel, " +
"and channels only contain the permissions of the interaction caller"
);
}

@Override
Expand Down

0 comments on commit 2b296d4

Please sign in to comment.