From 54db46a5b9d398fdc1c51164073321404561a6c0 Mon Sep 17 00:00:00 2001 From: My-Name-Is-Jeff <37018278+My-Name-Is-Jeff@users.noreply.github.com> Date: Fri, 14 Jul 2023 22:30:52 -0400 Subject: [PATCH] feat: Add the Friend InviteType --- .../net/dv8tion/jda/api/entities/Invite.java | 49 +++++++++++-------- .../jda/internal/entities/EntityBuilder.java | 30 +++++------- .../jda/internal/entities/InviteImpl.java | 14 +++--- 3 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/entities/Invite.java b/src/main/java/net/dv8tion/jda/api/entities/Invite.java index 279eb3134a..b71f273d99 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/Invite.java +++ b/src/main/java/net/dv8tion/jda/api/entities/Invite.java @@ -143,7 +143,7 @@ static RestAction 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 */ @@ -154,7 +154,7 @@ static RestAction 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 */ @@ -197,7 +197,7 @@ 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 */ @@ -205,9 +205,9 @@ default String getUrl() 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(); @@ -230,10 +230,10 @@ default String getUrl() /** * The max uses of this invite. If there is no limit thus will return {@code 0}. * - *

This works only for guild invites and will throw a {@link java.lang.IllegalStateException} otherwise!

+ *

This works only for guild and friend invites and will throw a {@link java.lang.IllegalStateException} otherwise!

* * @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(); @@ -257,29 +257,23 @@ default String getUrl() /** * How often this invite has been used. * - *

This works only for guild invites and will throw a {@link java.lang.IllegalStateException} otherwise!

+ *

This works only for guild and friend invites and will throw a {@link java.lang.IllegalStateException} otherwise!

* * @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}). - * - *

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(); /** @@ -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; + } } /** diff --git a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java index 816738fc53..a0cc4327fb 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java @@ -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); @@ -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); @@ -2225,8 +2223,6 @@ else if (channelType.isGuild()) else { // Unknown channel type for invites - - type = Invite.InviteType.UNKNOWN; guild = null; channel = null; group = null; @@ -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; } diff --git a/src/main/java/net/dv8tion/jda/internal/entities/InviteImpl.java b/src/main/java/net/dv8tion/jda/internal/entities/InviteImpl.java index 902b6e4177..cd7be40dbe 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/InviteImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/InviteImpl.java @@ -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; } @@ -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; } @@ -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; }