Skip to content

Commit

Permalink
Disable owner caching when guild subs are off (#1156)
Browse files Browse the repository at this point in the history
* Disable owner caching when guild subs are off
* Add retrieveOwner for convenience
  • Loading branch information
MinnDevelopment committed Dec 14, 2019
1 parent a82f8ed commit 77b787b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
29 changes: 29 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Guild.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,14 @@ default int getMaxEmotes()
* <br>This is null when the owner is no longer in this guild or not yet loaded (lazy loading).
* Sometimes owners of guilds delete their account or get banned by Discord.
*
* <p>If lazy-loading is used it is recommended to use {@link #retrieveOwner()} instead.
*
* <p>Ownership can be transferred using {@link net.dv8tion.jda.api.entities.Guild#transferOwnership(Member)}.
*
* @return Possibly-null Member object for the Guild owner.
*
* @see #getOwnerIdLong()
* @see #retrieveOwner()
*/
@Nullable
Member getOwner();
Expand Down Expand Up @@ -2133,6 +2136,32 @@ default RestAction<Member> retrieveMemberById(@Nonnull String id)
@Nonnull
RestAction<Member> retrieveMemberById(long id);

/**
* Shortcut for {@code guild.retrieveMemberById(guild.getOwnerIdLong())}.
* <br>This will retrieve the current owner of the guild.
* It is possible that the owner of a guild is no longer a registered discord user in which case this will fail.
*
* <p>Possible {@link net.dv8tion.jda.api.exceptions.ErrorResponseException ErrorResponseExceptions} include:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_MEMBER}
* <br>The specified user is not a member of this guild</li>
*
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_USER}
* <br>The specified user does not exist</li>
* </ul>
*
* @return {@link RestAction} - Type: {@link Member}
*
* @see #getOwner()
* @see #getOwnerIdLong()
* @see #retrieveMemberById(long)
*/
@Nonnull
default RestAction<Member> retrieveOwner()
{
return retrieveMemberById(getOwnerIdLong());
}

/* From GuildController */

/**
Expand Down
31 changes: 16 additions & 15 deletions src/main/java/net/dv8tion/jda/internal/entities/GuildImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,19 @@ public CompletableFuture<Void> retrieveMembers()
return chunkingCallback;
}

@Nonnull
@Override
public RestAction<Member> retrieveMemberById(long id)
{
Member member = getMemberById(id);
if (member != null)
return new EmptyRestAction<>(getJDA(), member);

Route.CompiledRoute route = Route.Guilds.GET_MEMBER.compile(getId(), Long.toUnsignedString(id));
return new RestActionImpl<>(getJDA(), route, (resp, req) ->
getJDA().getEntityBuilder().createMember(this, resp.getObject()));
}

@Override
public long getIdLong()
{
Expand Down Expand Up @@ -1288,7 +1301,9 @@ public GuildImpl setAvailable(boolean available)

public GuildImpl setOwner(Member owner)
{
this.owner = owner;
// Only cache owner if user cache is enabled
if (getJDA().isGuildSubscriptions())
this.owner = owner;
return this;
}

Expand Down Expand Up @@ -1528,20 +1543,6 @@ public void updateCachedOverrides(AbstractChannelImpl<?, ?> channel, DataArray n
overrideMap.keySet().removeAll(toRemove);
}

@Nonnull
@Override
public RestAction<Member> retrieveMemberById(long id)
{
Member member = getMemberById(id);
// If guild subscriptions are disabled this member might not be up-to-date
if (member != null && getJDA().isGuildSubscriptions())
return new EmptyRestAction<>(getJDA(), member);

Route.CompiledRoute route = Route.Guilds.GET_MEMBER.compile(getId(), Long.toUnsignedString(id));
return new RestActionImpl<>(getJDA(), route, (resp, req) ->
getJDA().getEntityBuilder().createMember(this, resp.getObject()));
}

public void startChunking()
{
if (isLoaded())
Expand Down

0 comments on commit 77b787b

Please sign in to comment.