Skip to content

Commit

Permalink
Fix GuildChannel#createCopy for uncached members (#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Mar 13, 2020
1 parent 6c7768f commit f1becef
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 23 deletions.
Expand Up @@ -18,6 +18,7 @@

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.internal.utils.Checks;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -247,7 +248,147 @@ default ChannelAction<T> addPermissionOverride(@Nonnull IPermissionHolder target
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> addPermissionOverride(@Nonnull IPermissionHolder target, long allow, long deny);
default ChannelAction<T> addPermissionOverride(@Nonnull IPermissionHolder target, long allow, long deny)
{
Checks.notNull(target, "Override Role/Member");
if (target instanceof Role)
return addRolePermissionOverride(target.getIdLong(), allow, deny);
else if (target instanceof Member)
return addMemberPermissionOverride(target.getIdLong(), allow, deny);
throw new IllegalArgumentException("Cannot add override for " + target.getClass().getSimpleName());
}

/**
* Adds a new Member {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride}
* for the new GuildChannel.
*
* <p>Example:
* <pre>{@code
* long userId = user.getIdLong();
* EnumSet<Permission> allow = EnumSet.of(Permission.MESSAGE_READ);
* EnumSet<Permission> deny = EnumSet.of(Permission.MESSAGE_WRITE);
* channelAction.addMemberPermissionOverride(userId, allow, deny);
* }</pre>
*
* @param memberId
* The id for the member
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
*
* @return The current ChannelAction, for chaining convenience
*
* @see java.util.EnumSet
*/
@Nonnull
@CheckReturnValue
default ChannelAction<T> addMemberPermissionOverride(long memberId, @Nullable Collection<Permission> allow, @Nullable Collection<Permission> deny)
{
final long allowRaw = allow != null ? Permission.getRaw(allow) : 0;
final long denyRaw = deny != null ? Permission.getRaw(deny) : 0;

return addMemberPermissionOverride(memberId, allowRaw, denyRaw);
}

/**
* Adds a new Role {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride}
* for the new GuildChannel.
*
* <p>Example:
* <pre>{@code
* long roleId = role.getIdLong();
* EnumSet<Permission> allow = EnumSet.of(Permission.MESSAGE_READ);
* EnumSet<Permission> deny = EnumSet.of(Permission.MESSAGE_WRITE);
* channelAction.addRolePermissionOverride(roleId, allow, deny);
* }</pre>
*
* @param roleId
* The id for the role
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override or null
*
* @return The current ChannelAction, for chaining convenience
*
* @see java.util.EnumSet
*/
@Nonnull
@CheckReturnValue
default ChannelAction<T> addRolePermissionOverride(long roleId, @Nullable Collection<Permission> allow, @Nullable Collection<Permission> deny)
{
final long allowRaw = allow != null ? Permission.getRaw(allow) : 0;
final long denyRaw = deny != null ? Permission.getRaw(deny) : 0;

return addRolePermissionOverride(roleId, allowRaw, denyRaw);
}

/**
* Adds a new Member {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride} for the new GuildChannel.
*
* <p>Example:
* <pre>{@code
* long userId = user.getIdLong();
* long allow = Permission.MESSAGE_READ.getRawValue();
* long deny = Permission.MESSAGE_WRITE.getRawValue() | Permission.MESSAGE_ADD_REACTION.getRawValue();
* channelAction.addMemberPermissionOverride(userId, allow, deny);
* }</pre>
*
* @param memberId
* The id for the member
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
*
* @throws java.lang.IllegalArgumentException
* If one of the provided Permission values is invalid
*
* @return The current ChannelAction, for chaining convenience
*
* @see net.dv8tion.jda.api.Permission#getRawValue()
* @see net.dv8tion.jda.api.Permission#getRaw(java.util.Collection)
* @see net.dv8tion.jda.api.Permission#getRaw(net.dv8tion.jda.api.Permission...)
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> addMemberPermissionOverride(long memberId, long allow, long deny);

/**
* Adds a new Role {@link net.dv8tion.jda.api.entities.PermissionOverride PermissionOverride} for the new GuildChannel.
*
* <p>Example:
* <pre>{@code
* long roleId = role.getIdLong();
* long allow = Permission.MESSAGE_READ.getRawValue();
* long deny = Permission.MESSAGE_WRITE.getRawValue() | Permission.MESSAGE_ADD_REACTION.getRawValue();
* channelAction.addMemberPermissionOverride(roleId, allow, deny);
* }</pre>
*
* @param roleId
* The id for the role
* @param allow
* The granted {@link net.dv8tion.jda.api.Permission Permissions} for the override
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
* @param deny
* The denied {@link net.dv8tion.jda.api.Permission Permissions} for the override
* Use {@link net.dv8tion.jda.api.Permission#getRawValue()} to retrieve these Permissions.
*
* @throws java.lang.IllegalArgumentException
* If one of the provided Permission values is invalid
*
* @return The current ChannelAction, for chaining convenience
*
* @see net.dv8tion.jda.api.Permission#getRawValue()
* @see net.dv8tion.jda.api.Permission#getRaw(java.util.Collection)
* @see net.dv8tion.jda.api.Permission#getRaw(net.dv8tion.jda.api.Permission...)
*/
@Nonnull
@CheckReturnValue
ChannelAction<T> addRolePermissionOverride(long roleId, long allow, long deny);

/**
* Sets the bitrate for the new VoiceChannel
Expand Down
Expand Up @@ -93,9 +93,9 @@ public ChannelAction<Category> createCopy(@Nonnull Guild guild)
for (PermissionOverride o : overrides.valueCollection())
{
if (o.isMemberOverride())
action.addPermissionOverride(o.getMember(), o.getAllowedRaw(), o.getDeniedRaw());
action.addMemberPermissionOverride(o.getIdLong(), o.getAllowedRaw(), o.getDeniedRaw());
else
action.addPermissionOverride(o.getRole(), o.getAllowedRaw(), o.getDeniedRaw());
action.addRolePermissionOverride(o.getIdLong(), o.getAllowedRaw(), o.getDeniedRaw());
}
}
return action;
Expand Down
Expand Up @@ -314,9 +314,9 @@ public ChannelAction<TextChannel> createCopy(@Nonnull Guild guild)
for (PermissionOverride o : overrides.valueCollection())
{
if (o.isMemberOverride())
action.addPermissionOverride(o.getMember(), o.getAllowedRaw(), o.getDeniedRaw());
action.addMemberPermissionOverride(o.getIdLong(), o.getAllowedRaw(), o.getDeniedRaw());
else
action.addPermissionOverride(o.getRole(), o.getAllowedRaw(), o.getDeniedRaw());
action.addRolePermissionOverride(o.getIdLong(), o.getAllowedRaw(), o.getDeniedRaw());
}
}
return action;
Expand Down
Expand Up @@ -97,9 +97,9 @@ public ChannelAction<VoiceChannel> createCopy(@Nonnull Guild guild)
for (PermissionOverride o : overrides.valueCollection())
{
if (o.isMemberOverride())
action.addPermissionOverride(o.getMember(), o.getAllowedRaw(), o.getDeniedRaw());
action.addMemberPermissionOverride(o.getIdLong(), o.getAllowedRaw(), o.getDeniedRaw());
else
action.addPermissionOverride(o.getRole(), o.getAllowedRaw(), o.getDeniedRaw());
action.addRolePermissionOverride(o.getIdLong(), o.getAllowedRaw(), o.getDeniedRaw());
}
}
return action;
Expand Down
Expand Up @@ -156,27 +156,27 @@ public ChannelActionImpl<T> setSlowmode(int slowmode)
@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> addPermissionOverride(@Nonnull IPermissionHolder target, long allow, long deny)
public ChannelActionImpl<T> addMemberPermissionOverride(long userId, long allow, long deny)
{
return addOverride(userId, PermOverrideData.MEMBER_TYPE, allow, deny);
}

@Nonnull
@Override
@CheckReturnValue
public ChannelActionImpl<T> addRolePermissionOverride(long roleId, long allow, long deny)
{
return addOverride(roleId, PermOverrideData.ROLE_TYPE, allow, deny);
}

private ChannelActionImpl<T> addOverride(long targetId, int type, long allow, long deny)
{
Checks.notNull(target, "Override Role");
Checks.notNegative(allow, "Granted permissions value");
Checks.notNegative(deny, "Denied permissions value");
Checks.check(allow <= Permission.ALL_PERMISSIONS, "Specified allow value may not be greater than a full permission set");
Checks.check(deny <= Permission.ALL_PERMISSIONS, "Specified deny value may not be greater than a full permission set");
Checks.check(target.getGuild().equals(guild), "Specified Role is not in the same Guild!");
Checks.check(deny <= Permission.ALL_PERMISSIONS, "Specified deny value may not be greater than a full permission set");

if (target instanceof Role)
{
Role r = (Role) target;
long id = r.getIdLong();
overrides.add(new PermOverrideData(PermOverrideData.ROLE_TYPE, id, allow, deny));
}
else
{
Member m = (Member) target;
long id = m.getUser().getIdLong();
overrides.add(new PermOverrideData(PermOverrideData.MEMBER_TYPE, id, allow, deny));
}
overrides.add(new PermOverrideData(type, targetId, allow, deny));
return this;
}

Expand Down

0 comments on commit f1becef

Please sign in to comment.