Skip to content

Commit

Permalink
Add Guild#retrieveMetaData (#1171)
Browse files Browse the repository at this point in the history
* Lazy load member limits for guilds
* Improve implementation of update handler
* Add Guild#retrieveMetaData
  • Loading branch information
MinnDevelopment committed May 1, 2020
1 parent d60596f commit bad9bed
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
78 changes: 78 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/Guild.java
Expand Up @@ -519,6 +519,8 @@ default int getMaxEmotes()
* @return The maximum amount of members
*
* @since 4.0.0
*
* @see #retrieveMetaData()
*/
int getMaxMembers();

Expand All @@ -530,9 +532,22 @@ default int getMaxEmotes()
* @return The maximum amount of connected members this guild can have
*
* @since 4.0.0
*
* @see #retrieveMetaData()
*/
int getMaxPresences();

/**
* Loads {@link MetaData} for this guild instance.
*
* @return {@link RestAction} - Type: {@link MetaData}
*
* @since 4.2.0
*/
@Nonnull
@CheckReturnValue
RestAction<MetaData> retrieveMetaData();

/**
* Provides the {@link net.dv8tion.jda.api.entities.VoiceChannel VoiceChannel} that has been set as the channel
* which {@link net.dv8tion.jda.api.entities.Member Members} will be moved to after they have been inactive in a
Expand Down Expand Up @@ -4607,4 +4622,67 @@ public String toString()
return "GuildBan:" + user + (reason == null ? "" : '(' + reason + ')');
}
}

/**
* Meta-Data for a Guild
*
* @since 4.2.0
*/
class MetaData
{
private final int memberLimit;
private final int presenceLimit;
private final int approximatePresences;
private final int approximateMembers;

public MetaData(int memberLimit, int presenceLimit, int approximatePresences, int approximateMembers)
{
this.memberLimit = memberLimit;
this.presenceLimit = presenceLimit;
this.approximatePresences = approximatePresences;
this.approximateMembers = approximateMembers;
}

/**
* The active member limit for this guild.
* <br>This limit restricts how many users can be member for this guild at once.
*
* @return The member limit
*/
public int getMemberLimit()
{
return memberLimit;
}

/**
* The active presence limit for this guild.
* <br>This limit restricts how many users can be connected/online for this guild at once.
*
* @return The presence limit
*/
public int getPresenceLimit()
{
return presenceLimit;
}

/**
* The approximate number of online members in this guild.
*
* @return The approximate presence count
*/
public int getApproximatePresences()
{
return approximatePresences;
}

/**
* The approximate number of members in this guild.
*
* @return The approximate member count
*/
public int getApproximateMembers()
{
return approximateMembers;
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/entities/GuildImpl.java
Expand Up @@ -289,6 +289,24 @@ public int getMaxPresences()
return maxPresences;
}

@Nonnull
@Override
public RestAction<MetaData> retrieveMetaData()
{
Route.CompiledRoute route = Route.Guilds.GET_GUILD.compile(getId());
route = route.withQueryParams("with_counts", "true");
return new RestActionImpl<>(getJDA(), route, (response, request) -> {
DataObject json = response.getObject();
int memberLimit = json.getInt("max_members", 0);
int presenceLimit = json.getInt("max_presences", 5000);
this.maxMembers = memberLimit;
this.maxPresences = presenceLimit;
int approxMembers = json.getInt("approximate_member_count", this.memberCount);
int approxPresence = json.getInt("approximate_presence_count", 0);
return new MetaData(memberLimit, presenceLimit, approxPresence, approxMembers);
});
}

@Override
public VoiceChannel getAfkChannel()
{
Expand Down
Expand Up @@ -55,13 +55,17 @@ protected Long handleInternally(DataObject content)
return null;
}

//////////////
// WARNING //
//Do not rely on allContent past this point, this method is also called from GuildCreateHandler!
//////////////
long ownerId = content.getLong("owner_id");
//When member limits aren't initialized we don't fire an update event for them
int maxMembers = content.getInt("max_members", 0);
int maxPresences = content.getInt("max_presences", 5000);
if (guild.getMaxMembers() == 0)
{
// Initialize member limits to avoid unwanted update events
guild.setMaxPresences(maxPresences);
guild.setMaxMembers(maxMembers);
}

long ownerId = content.getLong("owner_id");
int boostCount = content.getInt("premium_subscription_count", 0);
int boostTier = content.getInt("premium_tier", 0);
String description = content.getString("description", null);
Expand Down

0 comments on commit bad9bed

Please sign in to comment.