Skip to content

Commit

Permalink
feat: Add the Friend InviteType
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed Jul 15, 2023
1 parent 3d5f088 commit 54db46a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
49 changes: 29 additions & 20 deletions src/main/java/net/dv8tion/jda/api/entities/Invite.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
* An {@link Invite.Channel Invite.Channel} object
* containing information about this invite's origin channel.
*
* @return Information about this invite's origin channel or null in case of a group invite
* @return Information about this invite's origin channel or null in case of a non-guild invite
*
* @see Invite.Channel
*/
Expand All @@ -154,7 +154,7 @@ static RestAction<Invite> resolve(@Nonnull final JDA api, @Nonnull final String
* An {@link Invite.Group Invite.Group} object
* containing information about this invite's origin group.
*
* @return Information about this invite's origin group or null in case of a guild invite
* @return Information about this invite's origin group or null in case of a non-group invite
*
* @see Invite.Group
*/
Expand Down Expand Up @@ -197,17 +197,17 @@ default String getUrl()
* An {@link Invite.Guild Invite.Guild} object
* containing information about this invite's origin guild.
*
* @return Information about this invite's origin guild or null in case of a group invite
* @return Information about this invite's origin guild or null in case of a non-guild invite
*
* @see Invite.Guild
*/
@Nullable
Guild getGuild();

/**
* The user who created this invite. For not expanded invites this may be null.
* The user who created this invite.
*
* @return The user who created this invite
* @return The user who created this invite. If this is a {@link Invite.InviteType#FRIEND Friend invite} this is the target.
*/
@Nullable
User getInviter();
Expand All @@ -230,10 +230,10 @@ default String getUrl()
/**
* The max uses of this invite. If there is no limit thus will return {@code 0}.
*
* <p>This works only for guild invites and will throw a {@link java.lang.IllegalStateException} otherwise!</p>
* <p>This works only for guild and friend invites and will throw a {@link java.lang.IllegalStateException} otherwise!</p>
*
* @throws java.lang.IllegalStateException
* if this is not a guild invite
* if this is not a guild or friend invite
* @return The max uses of this invite or {@code 0} if there is no limit
*/
int getMaxUses();
Expand All @@ -257,29 +257,23 @@ default String getUrl()
/**
* How often this invite has been used.
*
* <p>This works only for guild invites and will throw a {@link java.lang.IllegalStateException} otherwise!</p>
* <p>This works only for guild and friend invites and will throw a {@link java.lang.IllegalStateException} otherwise!</p>
*
* @throws java.lang.IllegalStateException
* if this is not a guild invite
* if this is not a guild or friend invite
*
* @return The uses of this invite
*/
int getUses();

/**
* Whether this Invite is expanded or not. Expanded invites contain more information, but they can only be
* obtained by {@link net.dv8tion.jda.api.entities.Guild#retrieveInvites() Guild#retrieveInvites()} (requires
* {@link net.dv8tion.jda.api.Permission#MANAGE_SERVER Permission.MANAGE_SERVER}) or
* {@link net.dv8tion.jda.api.entities.channel.attribute.IInviteContainer#retrieveInvites() IInviteContainer#retrieveInvites()} (requires
* {@link net.dv8tion.jda.api.Permission#MANAGE_CHANNEL Permission.MANAGE_CHANNEL}).
*
* <p>There is a convenience method {@link #expand()} to get the expanded invite for an unexpanded one.
* Every invite is considered expanded.
* Discord no longer has a distinction between expanded and unexpanded invites.
*
* @return Whether this invite is expanded or not
*
* @see #expand()
* @deprecated All properties for guild invites are now available without expanding
*/
@Deprecated
@ForRemoval
boolean isExpanded();

/**
Expand Down Expand Up @@ -659,7 +653,22 @@ enum InviteType
{
GUILD,
GROUP,
UNKNOWN
FRIEND,
UNKNOWN;

/**
* Static accessor for retrieving a invite type based on its Discord id key.
*
* @param id
* The id key of the requested invite type.
*
* @return The InviteType that is referred to by the provided key. If the id key is unknown, {@link #UNKNOWN} is returned.
*/
@Nonnull
public static InviteType fromId(int id)
{
return id < UNKNOWN.ordinal() ? values()[id] : UNKNOWN;
}
}

/**
Expand Down
30 changes: 12 additions & 18 deletions src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2162,22 +2162,22 @@ public Invite createInvite(DataObject object)
final String code = object.getString("code");
final User inviter = object.hasKey("inviter") ? createUser(object.getObject("inviter")) : null;

final DataObject channelObject = object.getObject("channel");
final ChannelType channelType = ChannelType.fromId(channelObject.getInt("type"));
final DataObject channelObject = object.optObject("channel").orElse(null);
final ChannelType channelType = channelObject != null ? ChannelType.fromId(channelObject.getInt("type")) : null;
final Invite.TargetType targetType = Invite.TargetType.fromId(object.getInt("target_type", 0));

final Invite.InviteType type;
final Invite.InviteType type = Invite.InviteType.fromId(object.getInt("type"));
final Invite.Guild guild;
final Invite.Channel channel;
final Invite.Group group;
final Invite.InviteTarget target;

if (channelType == ChannelType.GROUP)
if (type == Invite.InviteType.GROUP)
{
type = Invite.InviteType.GROUP;
guild = null;
channel = null;

assert channelObject != null;
final String groupName = channelObject.getString("name", "");
final long groupId = channelObject.getLong("id");
final String groupIconId = channelObject.getString("icon", null);
Expand All @@ -2190,10 +2190,8 @@ public Invite createInvite(DataObject object)

group = new InviteImpl.GroupImpl(groupIconId, groupName, groupId, usernames);
}
else if (channelType.isGuild())
else if (type == Invite.InviteType.GUILD)
{
type = Invite.InviteType.GUILD;

final DataObject guildObject = object.getObject("guild");

final String guildIconId = guildObject.getString("icon", null);
Expand Down Expand Up @@ -2225,8 +2223,6 @@ else if (channelType.isGuild())
else
{
// Unknown channel type for invites

type = Invite.InviteType.UNKNOWN;
guild = null;
channel = null;
group = null;
Expand Down Expand Up @@ -2254,23 +2250,21 @@ else if (channelType.isGuild())
target = new InviteImpl.InviteTargetImpl(targetType, null, null);
}

final int maxAge;
final int maxAge = object.getInt("max_age", -1);
final int maxUses;
final boolean temporary;
final boolean temporary = object.getBoolean("temporary", false);
final int uses;

if (type == Invite.InviteType.GUILD)
if (object.hasKey("max_uses"))
{
maxAge = object.getInt("max_age");
maxUses = object.getInt("max_uses");
temporary = object.getBoolean("temporary");
// this information is available on both guild and friend invites, but not group invites
maxUses = object.getInt("max_uses", -1);
// this field is only present on guilds when uses > 0, so default it to 0
uses = object.getInt("uses", 0);
}
else
{
maxAge = -1;
maxUses = -1;
temporary = false;
uses = -1;
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/net/dv8tion/jda/internal/entities/InviteImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,14 @@ public JDAImpl getJDA()
@Override
public int getMaxAge()
{
if (this.type != Invite.InviteType.GUILD)
throw new IllegalStateException("Only valid for guild invites");
return this.maxAge;
}

@Override
public int getMaxUses()
{
if (this.type != Invite.InviteType.GUILD)
throw new IllegalStateException("Only valid for guild invites");
if (this.maxUses == -1)
throw new IllegalStateException("Only valid for guild and friend invites");
return this.maxUses;
}

Expand All @@ -204,8 +202,8 @@ public OffsetDateTime getTimeExpires()
@Override
public int getUses()
{
if (this.type != Invite.InviteType.GUILD)
throw new IllegalStateException("Only valid for guild invites");
if (this.uses == -1)
throw new IllegalStateException("Only valid for guild and friend invites");
return this.uses;
}

Expand All @@ -214,13 +212,13 @@ public int getUses()
@ForRemoval
public boolean isExpanded()
{
return this.type == InviteType.GUILD;
return true;
}

@Override
public boolean isTemporary()
{
if (this.type != Invite.InviteType.GUILD)
if (this.type != InviteType.GUILD)
throw new IllegalStateException("Only valid for guild invites");
return this.temporary;
}
Expand Down

0 comments on commit 54db46a

Please sign in to comment.