net.dv8tion.jda.internal.utils.PermissionUtil Java Examples

The following examples show how to use net.dv8tion.jda.internal.utils.PermissionUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AudioManagerImpl.java    From JDA with Apache License 2.0 6 votes vote down vote up
private void checkChannel(VoiceChannel channel, Member self)
{
    EnumSet<Permission> perms = Permission.getPermissions(PermissionUtil.getEffectivePermission(channel, self));
    if (!perms.contains(Permission.VOICE_CONNECT))
        throw new InsufficientPermissionException(channel, Permission.VOICE_CONNECT);
    final int userLimit = channel.getUserLimit(); // userLimit is 0 if no limit is set!
    if (userLimit > 0 && !perms.contains(Permission.ADMINISTRATOR))
    {
        // Check if we can actually join this channel
        // - If there is a userlimit
        // - If that userlimit is reached
        // - If we don't have voice move others permissions
        // VOICE_MOVE_OTHERS allows access because you would be able to move people out to
        // open up a slot anyway
        if (userLimit <= channel.getMembers().size()
            && !perms.contains(Permission.VOICE_MOVE_OTHERS))
        {
            throw new InsufficientPermissionException(channel, Permission.VOICE_MOVE_OTHERS,
                "Unable to connect to VoiceChannel due to userlimit! Requires permission VOICE_MOVE_OTHERS to bypass");
        }
    }
}
 
Example #2
Source File: RoleManagerImpl.java    From JDA with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
@CheckReturnValue
public RoleManagerImpl setPermissions(long perms)
{
    long selfPermissions = PermissionUtil.getEffectivePermission(getGuild().getSelfMember());
    setupPermissions();
    long missingPerms = perms;         // include permissions we want to set to
    missingPerms &= ~selfPermissions;  // exclude permissions we have
    missingPerms &= ~this.permissions; // exclude permissions the role has
    // if any permissions remain, we have an issue
    if (missingPerms != 0 && isPermissionChecksEnabled())
    {
        EnumSet<Permission> permissionList = Permission.getPermissions(missingPerms);
        if (!permissionList.isEmpty())
            throw new InsufficientPermissionException(getGuild(), permissionList.iterator().next());
    }
    this.permissions = perms;
    set |= PERMISSION;
    return this;
}
 
Example #3
Source File: RoleImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public AuditableRestAction<Void> delete()
{
    Guild guild = getGuild();
    if (!guild.getSelfMember().hasPermission(Permission.MANAGE_ROLES))
        throw new InsufficientPermissionException(guild, Permission.MANAGE_ROLES);
    if(!PermissionUtil.canInteract(guild.getSelfMember(), this))
        throw new HierarchyException("Can't delete role >= highest self-role");
    if (managed)
        throw new UnsupportedOperationException("Cannot delete a Role that is managed. ");

    Route.CompiledRoute route = Route.Roles.DELETE_ROLE.compile(guild.getId(), getId());
    return new AuditableRestActionImpl<>(getJDA(), route);
}
 
Example #4
Source File: RoleImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasPermission(@Nonnull GuildChannel channel, @Nonnull Permission... permissions)
{
    long effectivePerms = PermissionUtil.getEffectivePermission(channel, this);
    for (Permission perm : permissions)
    {
        final long rawValue = perm.getRawValue();
        if ((effectivePerms & rawValue) != rawValue)
            return false;
    }
    return true;
}
 
Example #5
Source File: MemberImpl.java    From JDA with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public EnumSet<Permission> getPermissions(@Nonnull GuildChannel channel)
{
    Checks.notNull(channel, "Channel");
    if (!getGuild().equals(channel.getGuild()))
        throw new IllegalArgumentException("Provided channel is not in the same guild as this member!");

    return Permission.getPermissions(PermissionUtil.getEffectivePermission(channel, this));
}
 
Example #6
Source File: MemberImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public EnumSet<Permission> getPermissionsExplicit(@Nonnull GuildChannel channel)
{
    return Permission.getPermissions(PermissionUtil.getExplicitPermission(channel, this));
}
 
Example #7
Source File: RoleImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canInteract(@Nonnull Role role)
{
    return PermissionUtil.canInteract(this, role);
}
 
Example #8
Source File: RoleImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public EnumSet<Permission> getPermissionsExplicit(@Nonnull GuildChannel channel)
{
    return Permission.getPermissions(PermissionUtil.getExplicitPermission(channel, this));
}
 
Example #9
Source File: RoleImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public EnumSet<Permission> getPermissions(@Nonnull GuildChannel channel)
{
    return Permission.getPermissions(PermissionUtil.getEffectivePermission(channel, this));
}
 
Example #10
Source File: MemberImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canInteract(@Nonnull Emote emote)
{
    return PermissionUtil.canInteract(this, emote);
}
 
Example #11
Source File: MemberImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canInteract(@Nonnull Role role)
{
    return PermissionUtil.canInteract(this, role);
}
 
Example #12
Source File: MemberImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canInteract(@Nonnull Member member)
{
    return PermissionUtil.canInteract(this, member);
}
 
Example #13
Source File: MemberImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Override
public boolean hasPermission(@Nonnull GuildChannel channel, @Nonnull Permission... permissions)
{
    return PermissionUtil.checkPermission(channel, this, permissions);
}
 
Example #14
Source File: MemberImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Override
public boolean hasPermission(@Nonnull Permission... permissions)
{
    return PermissionUtil.checkPermission(this, permissions);
}
 
Example #15
Source File: MemberImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public EnumSet<Permission> getPermissionsExplicit()
{
    return Permission.getPermissions(PermissionUtil.getExplicitPermission(this));
}
 
Example #16
Source File: MemberImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public EnumSet<Permission> getPermissions()
{
    return Permission.getPermissions(PermissionUtil.getEffectivePermission(this));
}
 
Example #17
Source File: ChatSoundBoardListener.java    From DiscordSoundboard with Apache License 2.0 4 votes vote down vote up
private boolean userIsAdmin(MessageReceivedEvent event) {
    if (event.getMember() == null) {
        return false;
    }
    return PermissionUtil.checkPermission(event.getMember(), Permission.MANAGE_SERVER);
}
 
Example #18
Source File: UChannel.java    From Arraybot with Apache License 2.0 2 votes vote down vote up
/**
 * Whether or not the bot can talk in the text channel.
 * @param channel The channel.
 * @return True if they can, false otherwise.
 */
public static boolean cantTalk(TextChannel channel) {
    Member self = channel.getGuild().getSelfMember();
    return !PermissionUtil.checkPermission(channel, self, Permission.MESSAGE_READ) || !PermissionUtil.checkPermission(channel, self, Permission.MESSAGE_WRITE);
}
 
Example #19
Source File: Emote.java    From JDA with Apache License 2.0 2 votes vote down vote up
/**
 * Whether the specified User can interact with this Emote within the provided MessageChannel
 * <br>Special override to exclude elevated bot permissions in case of (for instance) reacting to messages.
 *
 * @param  issuer
 *         The User to test
 * @param  channel
 *         The MessageChannel to test
 * @param  botOverride
 *         Whether bots can use non-managed emotes in other guilds
 *
 * @return True, if the provided Member can use this Emote
 */
default boolean canInteract(User issuer, MessageChannel channel, boolean botOverride)
{
    return PermissionUtil.canInteract(issuer, this, channel, botOverride);
}
 
Example #20
Source File: Emote.java    From JDA with Apache License 2.0 2 votes vote down vote up
/**
 * Whether the specified User can interact with this Emote within the provided MessageChannel
 * <br>Same logic as {@link #canInteract(User, MessageChannel, boolean) canInteract(issuer, channel, true)}!
 *
 * @param  issuer
 *         The User to test
 * @param  channel
 *         The MessageChannel to test
 *
 * @return True, if the provided Member can use this Emote
 */
default boolean canInteract(User issuer, MessageChannel channel)
{
    return PermissionUtil.canInteract(issuer, this, channel);
}
 
Example #21
Source File: Emote.java    From JDA with Apache License 2.0 2 votes vote down vote up
/**
 * Whether the specified Member can interact with this Emote
 *
 * @param  issuer
 *         The User to test
 *
 * @return True, if the provided Member can use this Emote
 */
default boolean canInteract(Member issuer)
{
    return PermissionUtil.canInteract(issuer, this);
}
 
Example #22
Source File: UPunishment.java    From Arraybot with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether the specified member is banned in the guild.
 * @param guild The guild.
 * @param user The user.
 * @return True if they are, false otherwise.
 * @throws PermissionException If the bot has no permissions.
 */
public static boolean isBan(Guild guild, long user)
        throws PermissionException {
    return PermissionUtil.checkPermission(guild.getSelfMember(), Permission.BAN_MEMBERS) && guild.retrieveBanList().complete().stream().anyMatch(ban -> ban.getUser().getIdLong() == user);
}