Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional invite info #678

Merged
merged 3 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/java/net/dv8tion/jda/core/entities/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.dv8tion.jda.core.audit.ActionType;
import net.dv8tion.jda.core.audit.AuditLogChange;
import net.dv8tion.jda.core.audit.AuditLogEntry;
import net.dv8tion.jda.core.entities.Guild.VerificationLevel;
import net.dv8tion.jda.core.entities.MessageEmbed.*;
import net.dv8tion.jda.core.entities.impl.*;
import net.dv8tion.jda.core.exceptions.AccountTypeException;
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(Helpers.optInt(guildObject, "verification_level", -1));
final int presenceCount = Helpers.optInt(object, "approximate_presence_count", -1);
final int memberCount = Helpers.optInt(object, "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 Expand Up @@ -1289,7 +1293,9 @@ public Invite createInvite(JSONObject object)
timeCreated = null;
}

return new InviteImpl(api, code, expanded, inviter, maxAge, maxUses, temporary, timeCreated, uses, channel, guild);
return new InviteImpl(api, code, expanded, inviter,
maxAge, maxUses, temporary,
timeCreated, uses, channel, guild);
}

public void clearCache()
Expand Down
55 changes: 54 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 @@ -17,6 +17,7 @@
package net.dv8tion.jda.core.entities;

import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Guild.VerificationLevel;
import net.dv8tion.jda.core.entities.impl.InviteImpl;
import net.dv8tion.jda.core.requests.RestAction;
import net.dv8tion.jda.core.requests.restaction.AuditableRestAction;
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)
{
return InviteImpl.resolve(api, code, withCounts);
}

/**
Expand Down Expand Up @@ -296,5 +322,32 @@ 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. If the online member count was not included in the
* invite, this will return -1. Counts will usually only be returned when resolving the invite via the
* {@link #resolve(net.dv8tion.jda.core.JDA, java.lang.String, boolean) Invite.resolve()} method with the
* withCounts boolean set to {@code true}
*
* @return the approximate count of online members in the guild, or -1 if not present in the invite
*/
int getOnlineCount();

/**
* Returns the approximate count of total members in the guild. If the total member count was not included in the
* invite, this will return -1. Counts will usually only be returned when resolving the invite via the
* {@link #resolve(net.dv8tion.jda.core.JDA, java.lang.String, boolean) Invite.resolve()} method with the
* withCounts boolean set to {@code true}
*
* @return the approximate count of total members in the guild, or -1 if not present in the invite
*/
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 @@ -19,6 +19,7 @@
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.entities.Guild.VerificationLevel;
import net.dv8tion.jda.core.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.core.requests.Request;
import net.dv8tion.jda.core.requests.Response;
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;
}
}

}