Skip to content

Commit

Permalink
added more info for invites, no exceptions yet
Browse files Browse the repository at this point in the history
  • Loading branch information
jagrosh committed Jan 12, 2018
1 parent caaec21 commit c7db029
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import net.dv8tion.jda.core.entities.Guild.VerificationLevel;

public class EntityBuilder
{
Expand Down Expand Up @@ -1260,8 +1261,11 @@ public Invite createInvite(JSONObject object)
final long guildId = guildObject.getLong("id");
final String guildName = guildObject.getString("name");
final String guildSplashId = guildObject.optString("splash", null);
final VerificationLevel guildVerificationLevel = VerificationLevel.fromKey(guildObject.optInt("verification_level", -1));
final int presenceCount = object.optInt("approximate_presence_count", -1);
final int memberCount = object.optInt("approximate_member_count", -1);

final Invite.Guild guild = new InviteImpl.GuildImpl(guildId, guildIconId, guildName, guildSplashId);
final Invite.Guild guild = new InviteImpl.GuildImpl(guildId, guildIconId, guildName, guildSplashId, guildVerificationLevel, presenceCount, memberCount);

final int maxAge;
final int maxUses;
Expand Down
49 changes: 48 additions & 1 deletion src/main/java/net/dv8tion/jda/core/entities/Invite.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import javax.annotation.CheckReturnValue;
import java.time.OffsetDateTime;
import net.dv8tion.jda.core.entities.Guild.VerificationLevel;

/**
* Representation of a Discord Invite.
Expand Down Expand Up @@ -53,7 +54,32 @@ public interface Invite
*/
static RestAction<Invite> resolve(final JDA api, final String code)
{
return InviteImpl.resolve(api, code);
return resolve(api, code, false);
}

/**
* Retrieves a new {@link net.dv8tion.jda.core.entities.Invite Invite} instance for the given invite code.
* <br><b>You cannot resolve invites if you were banned from the origin Guild!</b>
*
* <p>Possible {@link net.dv8tion.jda.core.requests.ErrorResponse ErrorResponses} include:
* <ul>
* <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#UNKNOWN_INVITE Unknown Invite}
* <br>The Invite did not exist (possibly deleted) or the account is banned in the guild.</li>
* </ul>
*
* @param api
* The JDA instance
* @param code
* A valid invite code
* @param withCounts
* Whether or not to include online and member counts
*
* @return {@link net.dv8tion.jda.core.requests.RestAction RestAction} - Type: {@link net.dv8tion.jda.core.entities.Invite Invite}
* <br>The Invite object
*/
static RestAction<Invite> resolve(final JDA api, final String code, final boolean withCounts)

This comment has been minimized.

Copy link
@Almighty-Alpaca

Almighty-Alpaca Jan 13, 2018

Collaborator

Is there any reason you'd ever want to disable presence and member counts?

This comment has been minimized.

Copy link
@MinnDevelopment

MinnDevelopment Jan 13, 2018

Member

Better question, why did discord make this opt-in in the first place?

This comment has been minimized.

Copy link
@jagrosh

jagrosh Jan 13, 2018

Author Collaborator

It's slightly more data, and it is possible that it takes longer if Discord needs to calculate the numbers as well (although I don't know how their system works). If someone is resolving a lot of invites (like for a moderation bot or other similar feature), they might not want any extra overhead if possible.

{
return InviteImpl.resolve(api, code, withCounts);
}

/**
Expand Down Expand Up @@ -296,5 +322,26 @@ interface Guild extends ISnowflake
* @see #getSplashId()
*/
String getSplashUrl();

/**
* Returns the {@link net.dv8tion.jda.core.entities.Guild.VerificationLevel VerificationLevel} of this guild.
*
* @return the verification level of the guild
*/
VerificationLevel getVerificationLevel();

/**
* Returns the approximate count of online members in the guild.
*
* @return TODO
*/
int getOnlineCount();

/**
* Returns the approximate count of total members in the guild.
*
* @return TODO
*/
int getMemberCount();
}
}
34 changes: 30 additions & 4 deletions src/main/java/net/dv8tion/jda/core/entities/impl/InviteImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.json.JSONObject;

import java.time.OffsetDateTime;
import net.dv8tion.jda.core.entities.Guild.VerificationLevel;

public class InviteImpl implements Invite
{
Expand Down Expand Up @@ -62,12 +63,15 @@ public InviteImpl(final JDAImpl api, final String code, final boolean expanded,
this.guild = guild;
}

public static RestAction<Invite> resolve(final JDA api, final String code)
public static RestAction<Invite> resolve(final JDA api, final String code, final boolean withCounts)
{
Checks.notNull(code, "code");
Checks.notNull(api, "api");

final Route.CompiledRoute route = Route.Invites.GET_INVITE.compile(code);
Route.CompiledRoute route = Route.Invites.GET_INVITE.compile(code);

if(withCounts)
route = route.withQueryParams("with_counts", "true");

return new RestAction<Invite>(api, route)
{
Expand Down Expand Up @@ -282,16 +286,21 @@ public ChannelType getType()

public static class GuildImpl implements Guild
{

private final String iconId, name, splashId;
private final int presenceCount, memberCount;
private final long id;
private final VerificationLevel verificationLevel;

public GuildImpl(final long id, final String iconId, final String name, final String splashId)
public GuildImpl(final long id, final String iconId, final String name, final String splashId,
final VerificationLevel verificationLevel, final int presenceCount, final int memberCount)
{
this.id = id;
this.iconId = iconId;
this.name = name;
this.splashId = splashId;
this.verificationLevel = verificationLevel;
this.presenceCount = presenceCount;
this.memberCount = memberCount;
}

@Override
Expand Down Expand Up @@ -332,6 +341,23 @@ public String getSplashUrl()
: "https://cdn.discordapp.com/splashes/" + this.id + "/" + this.splashId + ".jpg";
}

@Override
public VerificationLevel getVerificationLevel()
{
return verificationLevel;
}

@Override
public int getOnlineCount()
{
return presenceCount;
}

@Override
public int getMemberCount()
{
return memberCount;
}
}

}

0 comments on commit c7db029

Please sign in to comment.